0

我想创建一个 BaseTest.groovy,在其中实现无头模式的 Webdriver。

package api

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

class BaseTest{
    ChromeOptions chromeOptions = new ChromeOptions()
    chromeOptions.addArguments(["--headless", "--no-sandbox"])
    static WebDriver driver = new ChromeDriver(chromeOptions)
}

我有一个 LoginSteps.groovy stepdefinitions 文件

package stepDefinitions

import api.Helper.helper
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions

import static cucumber.api.groovy.EN.*

Given(~/^I am on the xyz login page$/) { ->
    helper.setPage("https://xyzTestpage.com/")
}

When(~/^I sign in as "([^"]*)"$/) { String arg1 ->
    helper.signIn("username","password")
}

Then(~/^I load the homepage$/) { ->
    helper.setPreset()
}

我有一个 helper.groovy 文件,我在其中实现方法

package api.Helper

import api.BaseTest
import api.Xpaths.LoginPageXpaths
import api.Tools.tools
import org.openqa.selenium.By
import org.openqa.selenium.WebElement

class helper extends BaseTest {

    static void setPage(String url){
        driver.get(url)
    }

    static void signIn(String username, String password){

        WebElement uname = driver.findElement(By.xpath(LoginPageXpaths.userNameField()))
        uname.sendKeys(username)

        WebElement pwd = driver.findElement(By.xpath(LoginPageXpaths.passWordField()))
        pwd.sendKeys(password)

        WebElement loginButton = driver.findElement(By.xpath(LoginPageXpaths.loginButton()))
        loginButton.click()
    }

    static void setPreset(){
        WebElement multiCountry = driver.findElement(By.xpath(LoginPageXpaths.multiCountryButton()))
        multiCountry.click()

        WebElement openButton = driver.findElement(By.xpath(LoginPageXpaths.openButton()))
        openButton.click()

        String inputWindow = driver.getWindowHandle()

        for(String loggedInWindow : driver.getWindowHandles()){
            driver.switchTo().window(loggedInWindow)
        }

        WebElement lineItem = driver.findElement(By.xpath(LoginPageXpaths.calculateButtonXpath()))
        tools.waitForElementToBeClickable(driver,lineItem,25)
        driver.quit()
    }
}

所以我的问题是,我不知道应该在哪里设置无头模式,因为当我运行它时出现错误。

4

1 回答 1

0

您能否尝试如下分别添加参数并运行它

class BaseTest{
        ChromeOptions chromeOptions = new ChromeOptions()
        chromeOptions.addArguments("--headless");
     chromeOptions.addArguments("--no-sandbox");
        static WebDriver driver = new ChromeDriver(chromeOptions)
    }
于 2019-10-15T13:45:49.440 回答