public void afterTestMethod(TestContext testContext) throws Exception {
if (testContext.getTestException() == null) {
return;
}
File screenshot = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.FILE);
String testName = testContext.getTestClass().getSimpleName();
String methodName = testContext.getTestMethod().getName();
Files.copy(screenshot.toPath(),
Paths.get("C:\\Users\\user\\git\\ufe-360\\UFE-TESTS\\screenshots\test.png", testName + "_" + methodName + "_" + screenshot.getName()));
}
}
我的项目中有上面的代码,用于在测试执行后截图。 我怀疑我的代码中缺少某些内容。当我运行每个测试屏幕截图时,不会保存在指定的路径中。我没有任何错误。每个测试都正确执行,但没有屏幕截图。
最佳答案
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.OutputType;
private static void takeScreenshot() throws IOException, InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "chromedriver");
driver = new ChromeDriver();
driver.get("https://www.google.com/");
Thread.sleep(2);
TakesScreenshot scrShot =((TakesScreenshot)driver);
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
File DestFile=new File("/home/XXXX/Desktop/test.png");
FileUtils.copyFile(SrcFile, DestFile);
}
上面的代码将打开“google.com”,它将截取屏幕截图并将其存储在桌面上,就像我给定的桌面路径一样。
关于java - 适用于 Chrome 的 Selenium Webdriver 未将屏幕截图保存在指定路径中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53003986/