selenium Webdriver 截图

时间:2021-11-10 07:28:56

在使用Selenium 做自动化时,有的时候希望失败了进行截图,下面提供一个封装的截图方法,方便使用,代码如下:

//只需要传入文件名字即可,而这个名字大家可以直接使用测试的方法名
public void captureScreenshot(String fileName) {
String dirName = "screenshot";
if (!(new File(dirName).isDirectory())) {
new File(dirName).mkdir();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd-HHmmss");
String time = sdf.format(new Date());
TakesScreenshot tsDriver = (TakesScreenshot) driver;
File image = new File(dirName+File.separator+time+fileName+".png");
tsDriver.getScreenshotAs(OutputType.FILE).renameTo(image);
}

更多资料关注:www.kootest.com ;技术交流群:182526995