是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
当前回答
JAVA
嗨,你可以在selenium中截图,这里是下面给出的代码,你可以在任何selenium项目中创建截图,我们也可以在aws中为失败的场景生成截图。
public boolean takeScreenshot(final String name) {
String screenshotDirectory = System.getenv("WORKING_DIRECTORY");
File screenshot =
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
return screenshot.renameTo(new File(screenshotDirectory,
String.format("%s.png", name)));
}
其他回答
我使用下面的c#代码来获取整个页面或仅一个浏览器截图
public void screenShot(string tcName)
{
try
{
string dateTime = string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now);
string screenShotName = @"D:\Selenium\Project\VAM\VAM\bin" + "\\" + tcName + dateTime + ".png";
ITakesScreenshot screen = driverScript.driver as ITakesScreenshot;
Screenshot screenshot = screen.GetScreenshot();
screenshot.SaveAsFile(screenShotName, System.Drawing.Imaging.ImageFormat.Png);
if (driverScript.last == 1)
this.writeResult("Sheet1", "Fail see Exception", "Status", driverScript.resultRowID);
}
catch (Exception ex)
{
driverScript.writeLog.writeLogToFile(ex.ToString(), "inside screenShot");
}
}
public void fullPageScreenShot(string tcName)
{
try
{
string dateTime = string.Format("{0:yyyy-MM-dd_hh-mm-ss-tt}", DateTime.Now);
string screenShotName = @"D:\Selenium\Project\VAM\VAM\bin" + "\\" + tcName + dateTime + ".png";
Rectangle bounds = Screen.GetBounds(Point.Empty);
using (Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
bitmap.Save(screenShotName, System.Drawing.Imaging.ImageFormat.Png);
}
if (driverScript.last == 1)
this.writeResult("Sheet1", "Pass", "Status", driverScript.resultRowID);
}
catch (Exception ex)
{
driverScript.writeLog.writeLogToFile(ex.ToString(), "inside fullPageScreenShot");
}
}
C#
public Bitmap TakeScreenshot(By by) {
// 1. Make screenshot of all screen
var screenshotDriver = _selenium as ITakesScreenshot;
Screenshot screenshot = screenshotDriver.GetScreenshot();
var bmpScreen = new Bitmap(new MemoryStream(screenshot.AsByteArray));
// 2. Get screenshot of specific element
IWebElement element = FindElement(by);
var cropArea = new Rectangle(element.Location, element.Size);
return bmpScreen.Clone(cropArea, bmpScreen.PixelFormat);
}
塞勒尼斯
captureEntirePageScreenshot | /path/to/filename.png | background=#ccffdd
public static void getSnapShot(WebDriver driver, String event) {
try {
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage originalImage = ImageIO.read(scrFile);
//int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
BufferedImage resizedImage = CommonUtilities.resizeImage(originalImage, IMG_HEIGHT, IMG_WIDTH);
ImageIO.write(resizedImage, "jpg", new File(path + "/"+ testCaseId + "/img/" + index + ".jpg"));
Image jpeg = Image.getInstance(path + "/" + testCaseId + "/img/"+ index + ".jpg");
jpeg.setAlignment(Image.MIDDLE);
PdfPTable table = new PdfPTable(1);
PdfPCell cell1 = new PdfPCell(new Paragraph("\n"+event+"\n"));
PdfPCell cell2 = new PdfPCell(jpeg, false);
table.addCell(cell1);
table.addCell(cell2);
document.add(table);
document.add(new Phrase("\n\n"));
//document.add(new Phrase("\n\n" + event + "\n\n"));
//document.add(jpeg);
fileWriter.write("<pre> " + event + "</pre><br>");
fileWriter.write("<pre> " + Calendar.getInstance().getTime() + "</pre><br><br>");
fileWriter.write("<img src=\".\\img\\" + index + ".jpg\" height=\"460\" width=\"300\" align=\"middle\"><br><hr><br>");
++index;
}
catch (IOException | DocumentException e) {
e.printStackTrace();
}
}
你可以使用Webdriverclass对象创建一个webdriver支持的selenium对象,然后你可以截屏。