上QQ阅读APP看书,第一时间看更新
Using Headless Mode
Similar to Firefox, we can run tests in headless mode with ChromeDriver. This makes Chrome tests run faster and tests run more efficiently, especially in the CI (Continuous Integration) environment.
We can run Selenium tests in headless mode by configuring the ChromeOptions class as shown in the following code snippet:
@BeforeMethod
public void setup() {
System.setProperty("webdriver.chrome.driver",
"./src/test/resources/drivers/chromedriver");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setHeadless(true);
driver = new ChromeDriver(chromeOptions);
driver.get("http://demo-store.seleniumacademy.com/");
}
In the preceding code, we first created an instance of the ChromeOptions class, called the setHeadless() method, that passes the value as true to launch the Chrome browser in headless mode. During the execution, you will not see the Chrome window on the screen but the test will be executed in headless mode.