簡體   English   中英

如何在firefox無頭(java中的selenium)中截取屏幕截圖?

[英]How to take a screenshot in firefox headless (selenium in java)?

我已經能夠在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();
        }

是否需要設置其他“選項”或“參數”?

使用無頭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();
}

在測試執行失敗時調用它:

在此輸入圖像描述

這是你仍然可以使用的方式

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM