ChromeOptions when WebDriver is instantiated globally - Selenium Webdriver in Java

74 views Asked by At

I have a project I'm working on with several test methods. I need WebDriver to be available globally after I instantiate it and with ChromeOptions.

I'm using a @Before annotation to do a lot of setup and I thought I could pass ChromeOptions into the webdriver which is instantiated globally. 'options.addArguments()' isn't available globally and it isn't recognizing my options. Example below:

     public class classThatDoesStuff_Test {
        private static WebDriver driver = new ChromeDriver();
    
        @Before
        public void setUp() {
        System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\TestUser\\Desktop\\SeleniumWorkspace\\ABC\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--headless=new");
        options.addArguments("--window-size-1400,800");
        options.addArguments("--disable-gpu");
        }
        
        public void doStuff() {
        driver.get("http://foo.bar.com");
        
        }

Running this does not start the browser headless.

I tried creating a method with ChromeOptions and passing it to the driver. That didn't work

private static ChromeOptions options() {
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--headless=new");
            options.addArguments("window-size=1400,800");
            options.addArguments("disable-gpu");
            
            return options;

Then passing it to my instantiation. But that didn't work - as you probably guessed :)

1

There are 1 answers

1
Gulzhas Mailybayeva On
private static WebDriver driver;

@Before
public void setUp() {
    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\TestUser\\Desktop\\SeleniumWorkspace\\ABC\\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--remote-allow-origins=*", "ignore-certificate-errors");
    options.addArguments("--headless=new");
    options.addArguments("--window-size-1400,800");
    driver = new ChromeDriver(options); // missing to pass chrome options here

}