Kaip paleisti „WebDriver“ be galvos režimu? To gali prireikti, jei jūsų KI įrankis, pavyzdžiui, „Jenkins“ nepalaiko vartotojo sąsajos.
„WebDriver“ automatinių testų vykdymas be galvos režimu suteikia pranašumų testų vykdymo greičio ir lengvesnio integravimo į CI vamzdyną požiūriu.
Šioje pamokoje naudosime „PhantomJS“ ir „ChromeDriver“, kad atliktume „Selenium WebDriver“ testus bevielio režimo režimu.
Norėdami paleisti „Selenium WebDriver“ testus bevieliu režimu naudodami „PhantomJS“, pirmiausia turite atsisiųsti „PhantomJS“ vykdomasis failas ir išsaugokite jį vietoje, pvz. jūsų projekto išteklių aplanke.
Toliau pateiktame pavyzdyje aš įdėjau „PhantomJS“ vykdomąjį failą į src / test / resources / phantomjs
Jums taip pat reikės priklausomybės nuo vaiduoklio vairuotojo:
com.github.detro.ghostdriver phantomjsdriver 1.0.1
Ir jūsų „Java“ klasė:
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');
} }
Norėdami paleisti „WebDriver“ testus bevieliu režimu naudodami „ChromeDriver“, turėsite pridėti atitinkamas priklausomybes savo pom.xml faile:
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}
Toliau mes nurodome „WebDriver“ tvarkyklei paleisti „Chrome“ tvarkyklę be galvos 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');
} }