【问题标题】:Unable to execute Headless Geckodriver with TestNG无法使用 TestNG 执行 Headless Geckodriver
【发布时间】:2019-11-14 22:56:10
【问题描述】:

自从我在我的测试中实现了无头配置后,我收到了这个问题:java.lang.NullPointerException

我尝试为 Gecko Headless 切换到其他类型的实现,但它们都不起作用

@BeforeTest
public static void OpenBrowser () {
    System.setProperty("webdriver.gecko.driver","binaries/geckodriver"); 
    FirefoxBinary firefoxBinary = new FirefoxBinary();
    firefoxBinary.addCommandLineOptions("-headless");

    FirefoxOptions firefoxOptions = new FirefoxOptions();
    firefoxOptions.setBinary(firefoxBinary);

    FirefoxDriver driver = new FirefoxDriver(firefoxOptions);
    driver.get(...

}

执行测试后,我收到以下错误:java.lang.NullPointerException

【问题讨论】:

    标签: java selenium-webdriver automation testng


    【解决方案1】:

    让它在无头模式下运行的命令如下:

    FirefoxOptions options = new FirefoxOptions();
    options.setHeadless(true);
    WebDriver driver = new FirefoxDriver(options);
    

    您可能希望在测试之外定义WebDriver driver 部分,以便您可以在@BeforeTest 中执行以下操作,如下所示:

    WebDriver driver;
    
    @BeforeTest 
    public static void OpenBrowser() {
        FirefoxOptions options = new FirefoxOptions();
        options.setHeadless(true);
        driver = new FirefoxDriver(options);
    }
    

    【讨论】:

    • 如果这回答了您的问题,请接受它作为正确答案/点赞。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-24
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    相关资源
    最近更新 更多