是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
是否可以使用Selenium WebDriver进行截图?
(注:不含硒遥控器)
当前回答
Python
def test_url(self):
self.driver.get("https://www.google.com/")
self.driver.save_screenshot("test.jpg")
它将在脚本保存的同一目录下保存一个截图。
其他回答
你可以尝试一下shot API。它在GitHub上。
测试示例。
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();
}
}
Java
使用RemoteWebDriver,在增加了节点的截图功能后,我将像这样存储截图:
void takeScreenShotMethod(){
try{
Thread.sleep(10000);
long id = Thread.currentThread().getId();
BufferedImage image = new Robot().createScreenCapture(new Rectangle(
Toolkit.getDefaultToolkit().getScreenSize()));
ImageIO.write(image, "jpg", new File("./target/surefire-reports/"
+ id + "/screenshot.jpg"));
}
catch( Exception e ) {
e.printStackTrace();
}
}
你可以在任何需要的地方使用这种方法。然后,我假设您可以在surefire-reports/html/custom.css中定制maven-surefire-report-plugin的样式表,以便您的报告包含到每个测试的正确截图的链接。
Python
def test_url(self):
self.driver.get("https://www.google.com/")
self.driver.save_screenshot("test.jpg")
它将在脚本保存的同一目录下保存一个截图。
Java
public String captureScreen() {
String path;
try {
WebDriver augmentedDriver = new Augmenter().augment(driver);
File source = ((TakesScreenshot)augmentedDriver).getScreenshotAs(OutputType.FILE);
path = "./target/screenshots/" + source.getName();
FileUtils.copyFile(source, new File(path));
}
catch(IOException e) {
path = "Failed to capture screenshot: " + e.getMessage();
}
return path;
}