Cara Nggawe WebDriver ing Mode Tanpa Kepala

Kepiye cara mbukak WebDriver ing mode tanpa sirah? Iki bisa uga dibutuhake yen alat CI sampeyan, kayata, Jenkins ora ndhukung UI.

Nglakoni Tes Otomatis WebDriver ing mode tanpa kepala nyedhiyakake kaluwihan babagan kacepetan eksekusi tes lan integrasi luwih gampang menyang pipa CI.

Ing tutorial iki, kita bakal nggunakake PhantomJS lan ChromeDriver kanggo mbukak tes Selenium WebDriver ing mode tanpa kepala.




PhantomJS

Kanggo mbukak tes Selenium WebDriver ing mode tanpa sirah nggunakake PhantomJS, luwih dhisik kudu ndownload File eksekusi PhantomJS lan simpen ing lokasi, e. g. folder sumber daya proyek sampeyan.

Ing conto ing ngisor iki, aku wis nyelehake PhantomJS ing src / test / sumber / phantomjs


Sampeyan uga kudu katergantungan driver hantu uga:



com.github.detro.ghostdriver phantomjsdriver 1.0.1

Lan kelas Java sampeyan:

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

Kanggo mbukak tes WebDriver ing mode tanpa sirah nggunakake ChromeDriver, sampeyan kudu nambah katergantungan sing relevan ing file pom.xml:


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}

Sabanjure, kita pitunjuk marang manajer WebDriver kanggo ngluncurake driver chrome ing mode tanpa sirah


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