无法使用 TestNG 执行 Headless Geckodriver

Unable to execute Headless Geckodriver with TestNG

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

我尝试为 Gecko Headless 切换到其他类型的实现,但 none 成功了

@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(...

}

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

headless模式下获取运行的命令如下:

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);
}