使用java运行selenium webdriver自动化脚本时,任何人都可以帮助拍摄屏幕截图并保存在Mac上的特定文件夹吗?
注意:我在代码中使用了static,所以我不能使用下面的代码
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("D:\\screenshot.jpg"));
提前致谢!!
答案 0 :(得分:4)
如果您正在使用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;
答案 1 :(得分:1)
制作文件夹
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);
答案 2 :(得分:1)
我们可以在TestNG中使用@AfterMethod将TakeScreenshot方法放置在单独的方法中。硒中的ITestResult接口为您提供测试执行的状态和测试用例的名称。
例如:
@AfterMethod
public void invokeScreenshotMethod(ItestResult res)
{
if (ItestResult.Failure==res.getStatus()){
try{
TakeScreenshot ts=(TakeScreenShot) driver;
File src= ts.getScreenshotAs(OUTPUTTYPE.FILE);
FileUtils.copyFile(src, new File("e://destination
location+"res.getName()+".png");
}
Catch(Exception e)
{
System.out.println("");
}
}
答案 3 :(得分:0)
不是为截图文件(“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,如果它不相同的话。
答案 4 :(得分:0)
您可以创建一些包装操作来解决您的问题。在这里,我为您分享一个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.
}
}
答案 5 :(得分:0)
以下是对我有用的东西,我将junit4与硒一起使用。另外,它使用场景名称作为屏幕截图,这非常有帮助。
@After
public void closeBrowser(Scenario scenario){
if(scenario.isFailed()){
try{
TakesScreenshot screenshot = (TakesScreenshot)browser;
File source = screenshot.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(source, new File("logs/screenshots/" + scenario.getName() + ".png"));
System.out.println("Screenshot taken");
} catch (Exception e){
System.out.println("Exception while taking screenshot " + e.getMessage());
}
}
browser.quit();
}
希望这会有所帮助:)