【问题标题】:Groovy, Selenium, Cucumber, adding ChromeOptions argumentsGroovy、Selenium、Cucumber,添加 ChromeOptions 参数
【发布时间】:2020-02-12 03:46:33
【问题描述】:

我想创建一个 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 步骤定义文件

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()
    }
}

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

【问题讨论】:

  • 您能否尝试我的解决方案,如果您遇到任何问题,请告诉我
  • 您能否提供您在执行程序时遇到的错误/异常?

标签: selenium groovy webdriver cucumber chrome-options


【解决方案1】:

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

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

【讨论】:

  • 我尝试了两种方式。我收到此错误消息。 "Error:(9, 9) Groovyc: unexpected token: chromeOptions" 并且 chromeOptions 在代码中是红色的
  • 您可以尝试在实例创建结束时添加分号,如下所示 ChromeOptions chromeOptions = new ChromeOptions();
  • 因为我使用了不需要的常规分号,但我尝试了,得到了同样的错误。我认为这可能是超出范围的错误,或者我必须在另一个地方设置这些选项,但不知道应该在哪里设置这些设置
  • 只是为了更清楚你是否导入了 chromeoptions import org.openqa.selenium.chrome.ChromeOptions;
  • 我在你的 basePage 包 api 中没有看到它 import org.openqa.selenium.WebDriver import org.openqa.selenium.chrome.ChromeDriver class BaseTest{ ChromeOptions chromeOptions = new ChromeOptions() chromeOptions.addArguments([ "--headless", "--no-sandbox"]) 静态 WebDriver 驱动 = new ChromeDriver(chromeOptions) }
猜你喜欢
  • 1970-01-01
  • 2022-07-16
  • 2021-11-09
  • 1970-01-01
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
  • 2019-01-20
  • 1970-01-01
相关资源
最近更新 更多