简体   繁体   English

无法在Chrome无头模式下运行测试

[英]Fail to run tests in Chrome headless mode

I am trying to run tests in Chrome headless mode but getting java.lang.NullPointerException 我试图在Chrome无头模式下运行测试,但是获取了java.lang.NullPointerException

Chrome version: Version 72.0.3626.121 (Official Build) (64-bit) Chrome版本:版本72.0.3626.121(官方版本)(64位)
Selenium version: 3.8.1 硒版:3.8.1
Chromedriver version: 2.45.615355 Chromedriver版本:2.45.615355

Here is my BaseTest: 这是我的BaseTest:

public abstract class BaseTest {

public WebDriver driver;

protected abstract String getUrl();

@Before
public void setUp() {
    Log.startLog("Test is Starting...");
    System.setProperty("webdriver.chrome.driver", "src//test//resources//chromedriver");
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.setHeadless(true);
    WebDriver driver = new ChromeDriver(chromeOptions);
    driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
    driver.get(getUrl());
}

@After
public void tearDown() {
    Log.endLog("Test is Ending...");
    driver.manage().deleteAllCookies();
    driver.close();
}
}

When I'm running tests, not in headless mode every test works good but in headless mode, I can't even run a simple test to understand if the headless mode is working or not. 当我正在运行测试时,不是在无头模式下,每个测试都运行良好,但在无头模式下,我甚至无法进行简单的测试来了解无头模式是否正常工作。

Test example: 测试示例:
@Test public void test() { System.out.println(driver.getTitle()); }

Example URL: https://www.wikipedia.org/ 示例网址: https//www.wikipedia.org/

UPDATE: I've created new sample project with this code: 更新:我用这段代码创建了新的示例项目:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class test {

public static void main(String[] args) {

    System.setProperty("webdriver.chrome.driver", "/Users/alexsomov/Desktop/chromedriver2");

    //Set Chrome Headless mode as TRUE
    ChromeOptions options = new ChromeOptions();
    options.setHeadless(true);

    //Instantiate Web Driver
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://www.google.com/");
    System.out.println("Page title is - " + driver.getTitle());

    driver.close();
}

And bingo, everything works well... Need to figure out why code above from real project doesn't work seems something wrong with BaseTest class and when I run code with debugger I'm getting driver == null , maybe anyone have a solution how I can solve this problem :/ 宾果游戏,一切运行良好......需要弄清楚为什么上面的代码从真正的项目不起作用似乎与BaseTest类有关,当我用调试器运行代码时我得到的驱动程序== null ,也许任何人都有解决方案我怎么能解决这个问题:/

ANSWER The solution was super easy, just need to change 1 string in setUp() method in BaseTest class. 答案解决方案非常简单,只需要在BaseTest类的setUp()方法中更改1个字符串。

This one: 这个:

WebDriver driver = new ChromeDriver(chromeOptions);

change to this: 改为:

driver = new ChromeDriver(chromeOptions);

and everything will work. 一切都会奏效。

If you are using linux environment, may be you have to add --no-sandbox as well and also specific window size settings. 如果您使用的是linux环境,可能还需要添加--no-sandbox以及特定的窗口大小设置。 --no-sandbox is no needed in windows if you set user container properly. 如果正确设置用户容器,则在Windows中不需要--no-sandbox。

disable-gpu Only on Windows. disable-gpu仅适用于Windows。 Other platforms no longer require it. 其他平台不再需要它。 The --disable-gpu flag is a temporary work around for a few bugs. --disable-gpu标志是一个临时解决一些错误。

if(browser.equalsIgnoreCase("HLChrome")){
            //Headless chrome browser and configure
            WebDriverManager.chromedriver().setup();
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.addArguments("--no-sandbox");
            chromeOptions.addArguments("--headless");
            chromeOptions.addArguments("disable-gpu");
//            chromeOptions.addArguments("window-size=1400,2100"); // linux should be activate
            driver = new ChromeDriver(chromeOptions);

ANSWER The solution was super easy, just need to change 1 string in setUp() method in BaseTest class. 答案解决方案非常简单,只需要在BaseTest类的setUp()方法中更改1个字符串。

This one: 这个:

WebDriver driver = new ChromeDriver(chromeOptions);

change to this: 改为:

driver = new ChromeDriver(chromeOptions);

and everything will work. 一切都会奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM