0

I wanted to create a BaseTest.groovy where i implement the Webdriver with headless mode.

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

I have a LoginSteps.groovy stepdefinitions file

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

And i have a helper.groovy file where i implement the methods

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

So my problem is, i don't know where should i set the headless mode, because i got error, when i run this.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
danikaa25
  • 11
  • 1

1 Answers1

0

Can you please try adding arguments separately as below and run it

class BaseTest{
        ChromeOptions chromeOptions = new ChromeOptions()
        chromeOptions.addArguments("--headless");
     chromeOptions.addArguments("--no-sandbox");
        static WebDriver driver = new ChromeDriver(chromeOptions)
    }
  • i tried both ways. I get this error message. "Error:(9, 9) Groovyc: unexpected token: chromeOptions" and the chromeOptions is red in the code – danikaa25 Oct 15 '19 at 13:58
  • Can you try adding semicolon at the end of instance creation as below ChromeOptions chromeOptions = new ChromeOptions(); – Elancheziyan G Oct 15 '19 at 14:00
  • since i use groovy semicolons not needed, but i tried, and got the same error. I think it might an out of scope error or i have to set these options another place, but have no clue where should i set these settings – danikaa25 Oct 15 '19 at 14:03
  • just to get more clarity have you imported chromeoptions import org.openqa.selenium.chrome.ChromeOptions; – Elancheziyan G Oct 15 '19 at 14:13
  • I dont see it in your basePage package api import org.openqa.selenium.WebDriver import org.openqa.selenium.chrome.ChromeDriver class BaseTest{ ChromeOptions chromeOptions = new ChromeOptions() chromeOptions.addArguments(["--headless", "--no-sandbox"]) static WebDriver driver = new ChromeDriver(chromeOptions) } – Elancheziyan G Oct 15 '19 at 14:30
  • it's added and i got the same error. chromeOprions is red in the code, so maybe i should take to another place, class or something like that i think – danikaa25 Oct 15 '19 at 14:42
  • without these 2 lines: """static ChromeOptions chromeOptions = new ChromeOptions() chromeOptions.addArguments(["--headless", "--no-sandbox"])""" i am able to run my test – danikaa25 Oct 15 '19 at 14:48
  • Please add driver instance creation in method and try it should work like below class BaseTest{ public void createDriver(){ ChromeOptions chromeOptions = new ChromeOptions() chromeOptions.addArguments("headless"); chromeOptions.addArguments("no-sandbox"); static WebDriver driver = new ChromeDriver(chromeOptions) } } and also setproperty for chromedriver using system.setproperty – Elancheziyan G Oct 15 '19 at 14:50