如何在Java中使用selenium webdriver截取失败的测试用例的屏幕截图?
问题描述:
任何人都可以在使用java运行selenium webdriver自动化脚本时帮助拍摄屏幕截图并保存在Mac上的特定文件夹?如何在Java中使用selenium webdriver截取失败的测试用例的屏幕截图?
注:我在代码中使用静态的,所以我不能下面的代码
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\screenshot.jpg"));
在此先感谢!使用
答
您不需要为截图文件(如“screenshot.jpg”)创建静态名称,只需在每次拍摄屏幕截图时都进行更改。你可以用许多不同的方式做什么。
一个你必须在这种情况下就可以了,创建日期和时间的字符串,将是唯一的,这样的选项:
var myUniqueScreenshot = "D:\\"+DateTime.Now.ToString("F").Replace(":", "-")+".jpg";
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(myUniqueScreenshot));
的第一行代码是C#希望你可以把它翻译成Java,如果它不一样。
答
您可以创建一些包装操作来解决您的问题。在这里,我为你分享一个java例子。希望能帮助到你。
ScreenshotUtil.java
public class ScreenshotUtil {
public static void capture(WebDriver driver) {
File src = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
StringBuffer failedPicPath = new StringBuffer();
failedPicPath.append("D:\\");
String fn = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()).toString(); //Or the name you want. I suggest don't use static name.
failedPicPath.append(fn);
failedPicPath.append(".jpg");
}
}
Click.java
public void click(WebDriver driver, String xpath) { //Assume you are using Xpath
try {
WebElement e = FindElementUtil.find(driver, xpath); //Here you can design a class named FindElementUtil to search any elements and construct your own Exception. Then catch it here and out put Screenshots.
e.click():
} catch(ANYEXCETPION e) {
ScreenshotUtil.capture(driver); //OR you want to capture pic in all situations, capture it in 'finally' block.
}
}
答
新建文件夹
File file = new File("Screenshots" + fileSeperator + "Results");
if (!file.exists()) {
file.mkdir();
}
//Now take the screens shot with name of your test method and copy to folder
File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File targetFile = new File("Screenshots" + fileSeperator + "Results" + fileSeperator + testName, screenShotName);
FileUtils.copyFile(screenshotFile, targetFile);
答
如果你正在使用JUnit 4,您可以创建一个扩展TestWatcher类类。
public class ScreenShotRule extends TestWatcher {
private WebDriver webDriver;
public ScreenShotRule(WebDriver webDriver) {
this.webDriver = webDriver;
}
@Override
protected void failed(Throwable e, Description description) {
String methodName = description.getMethodName();
String fileName = description.getTestClass().getSimpleName() + "-" + methodName + ".png";
try {
File destiny = new File(fileName);
FileUtils.copyFile(((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE), destiny);
} catch (IOException ioe) {
throw new RuntimeException(ioe);
}
}
@Override
protected void finished(Description description) {
webDriver.quit();
}
}
在测试中,添加此类的公共字段,当测试失败时,规则将采用截图。
@Rule
public ScreenShotRule screenshotRule;
+0
感谢并且工作得很好Mathias Pahlen – vamc
的[以与硒的webdriver的屏幕截图(http://stackoverflow.com/questions/3422262/take-a-screenshot-with-selenium-webdriver) –
但是代码是相同的可能重复以上可以采取只有一个屏幕截图,而不是失败时,所以需要更多的上述代码...你能帮我吗? @Mystia – vamc
老实说,我不能完全理解你的实际问题是什么? –