【问题标题】:How to take a screenshot in firefox headless (selenium in java)?如何在 Firefox 无头(Java 中的硒)中截取屏幕截图?
【发布时间】:2018-09-11 08:41:08
【问题描述】:

我已经能够在 Firefox 无头模式下运行 selenium 测试用例,但是在截图时,截图不是网页的截图(在测试用例中测试的网页),而是截图背景(如显示的当前窗口(例如运行测试用例的 eclipse IDE))

截图功能

File screenShotFolder = new File("Screenshots");
        WebDriver driver = getDriver();
        try {
            if (!screenShotFolder.exists() && !screenShotFolder.mkdir()) {
                getLog().error(
                        "Cannot create a new file in the intended location. "
                                + "" + screenShotFolder.getAbsolutePath());
            }
            File scrFile =
                    ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            String filePath =
                    screenShotFolder.getAbsolutePath() + File.separator
                            + imageName + ".png";
            FileUtils.copyFile(scrFile, new File(filePath));

        } catch (Exception e) {
            e.printStackTrace();
        }

还有其他需要设置的“选项”或“参数”吗?

【问题讨论】:

  • 我发布的功能正常工作,问题是另一个功能正在获取背景截图并将其添加到最终报告中。

标签: java selenium firefox selenium-webdriver


【解决方案1】:

使用无头 Firefox 截屏应该像普通驱动程序一样工作。

过去我使用以下方法:

public static String makeScreenshot() {
    String fileName = System.currentTimeMillis() + "Test";
    File screenshot = Driver.driver.get().getScreenshotAs(OutputType.FILE);
    File outputFile = new File("LoggerScreenshots/" + fileName + ".png");
    System.out.println(outputFile.getAbsolutePath());
    try {
        FileUtils.copyFile(screenshot, outputFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return outputFile.getName();
}

并在测试执行失败时调用它:

【讨论】:

    【解决方案2】:

    这是您仍然可以使用的方式

    from selenium import webdriver
    from selenium.webdriver.firefox.options import Options
    
    options = Options()
    options.add_argument( "--headless" )
    # options.add_argument( "--screenshot test.jpg http://google.com/" )
    driver = webdriver.Firefox( firefox_options=options )
    driver.get('http://google.com/')
    driver.save_screenshot('test.png')
    print driver.title
    print driver.current_url
    driver.quit()
    sys.exit()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-24
      • 2011-05-28
      • 1970-01-01
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多