Jak spustit WebDriver v bezhlavém režimu

Jak spustit WebDriver v bezhlavém režimu? To může být potřeba, pokud váš nástroj CI například Jenkins nepodporuje uživatelské rozhraní.

Spuštění automatických testů WebDriver v bezhlavém režimu poskytuje výhody, pokud jde o rychlost provádění testů a snadnější integraci do kanálu CI.

V tomto tutoriálu použijeme PhantomJS a ChromeDriver ke spuštění testů Selenium WebDriver v bezhlavém režimu.




PhantomJS

Chcete-li spustit testy selenového WebDriveru v bezhlavém režimu pomocí PhantomJS, musíte si nejprve stáhnout Spustitelný soubor PhantomJS a uložte jej na místo, např. složku zdrojů vašeho projektu.

V níže uvedeném příkladu jsem dal spustitelný soubor PhantomJS do src / test / resources / phantomjs


Budete také potřebovat závislost na ovladači duchů:

com.github.detro.ghostdriver phantomjsdriver 1.0.1

A vaše třída Java:

import org.openqa.selenium.phantomjs.PhantomJSDriver; import org.openqa.selenium.phantomjs.PhantomJSDriverService; import org.openqa.selenium.remote.DesiredCapabilities; public class WebDriverBase {
static protected WebDriver driver;

public static void setup() {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true); // not really needed: JS enabled by default
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, 'src/test/resources/phantomjs');

driver = new PhantomJSDriver(caps);
}

public static void main(String[] args) {
WebDriverBase.setup();
driver.get('https://devqa.io');
} }


ChromeDriver

Chcete-li spustit testy WebDriver v bezhlavém režimu pomocí ChromeDriver, budete muset do souboru pom.xml přidat příslušné závislosti:


org.seleniumhq.selenium
selenium-chrome-driver
${selenium.version}
org.seleniumhq.selenium
selenium-server
${selenium.version}
org.seleniumhq.selenium
selenium-java
${selenium.version}
io.github.bonigarcia
webdrivermanager
${webdrivermanager.version}

Dále instruujeme správce WebDriver, aby spustil ovladač chrome v bezhlavém režimu


import io.github.bonigarcia.wdm.ChromeDriverManager; import org.openqa.selenium.chrome.ChromeDriver; public class WebDriverBase {
static protected WebDriver driver;
public static void setup() {
ChromeDriverManager.getInstance().setup();
ChromeOptions chromeOptions = new ChromeOptions();

chromeOptions.addArguments('--headless');
driver = new ChromeDriver(chromeOptions);
}
public static void main(String[] args) {
WebDriverBase.setup();
driver.get('https://devqa.io');
} }