Java Selenium webdriver for Chrome未在指定路径中保存屏幕截图
我在我的项目中有上面的代码,可以在测试执行后截图。 我怀疑我的代码中缺少什么。当我运行每个测试时,屏幕截图不会保存在指定的路径中。我没有任何错误。每个测试都正确执行,但没有屏幕截图Java Selenium webdriver for Chrome未在指定路径中保存屏幕截图,java,selenium,google-chrome,selenium-webdriver,webpage-screenshot,Java,Selenium,Google Chrome,Selenium Webdriver,Webpage Screenshot,我在我的项目中有上面的代码,可以在测试执行后截图。 我怀疑我的代码中缺少什么。当我运行每个测试时,屏幕截图不会保存在指定的路径中。我没有任何错误。每个测试都正确执行,但没有屏幕截图 public void afterTestMethod(TestContext testContext) throws Exception { if (testContext.getTestException() == null) { return; } File screen
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()));
}
}
上面的代码将打开“google.com”,它将截图并存储在桌面上,正如我给出的桌面路径一样 在方法的开头,您有一些代码,当测试没有错误/异常时,这些代码不会让屏幕截图完成。所以你说“每个测试都正确执行,但没有截图。”这是意料之中的。 从一个问题:
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);
}
根据你的补充意见
if (testContext.getTestException() == null) {
return;
}
您的逻辑如下:如果测试失败,请在失败时制作屏幕截图。尝试修改测试代码使其失败,您应该可以在给定路径中看到屏幕截图。如果你想实现一些不同的逻辑,比如“在每个测试步骤上做一个屏幕截图”,请更正一个问题,因为它会有一些不同的解决方案
如果您只是删除If
逻辑,那么在测试的最后一步之后,您的代码将生成一个屏幕截图。(但我不确定这样的截图是否有用,因为通常截图用于帮助分析“哪里出了问题”,而您的逻辑完美地覆盖了它)您……怀疑您的代码中缺少了什么。。。?
if (ITestResult.FAILURE == result.getStatus()) {