java - 适用于 Chrome 的 Selenium Webdriver 未将屏幕截图保存在指定路径中

标签 java selenium google-chrome selenium-webdriver webpage-screenshot

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/

相关文章:

java - 清除 chromedriver 缓存 - java

java - 无法使用 kaaes Spotify Web api 获取播放列表的轨道

python - Selenium webdriver python 通过 xpath 查找元素 - 找不到元素

python - Selenium 的 XPath 问题

javascript - 无法将参数传递给 chrome.declarativeContent.SetIcon

html - 图片之间有多余的空白吗?

java - 类 "name"中的构造函数 "Class name"无法应用于房间持久性库中的给定类型

java - 什么是 Java 中的单词边界元字符的等价物?

python - 如何使用python Selenium 转到youtube播放列表中的下一个视频

javascript - chrome.downloads.download 给出 "Error during downloads.download: Invalid URL."