1

I have worked with Selenium and TestNG to Automate my web based application,

in TestNG, we have more test methods, so I declared my chrome Driver and ChromeOptions as a public like below code,

ChromeOptions options = new ChromeOptions(); //declared options
// WebDriver driver =new ChromeDriver(); 
ChromeDriver driver = new ChromeDriver(options);  //declared driver

and also I try to use one of the chrome options _ incognito to open the browser in incognito mode by below line

options.arguments("--incognito");

it's working only in one test method only ... but I write more then 33testcases I don't know how to declare below lines in common? Worked

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-notifications");
    options.addArguments("--incognito");
    //options.addArguments("--headless");
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    options.merge(capabilities);
    ChromeDriver driver = new ChromeDriver(options);        
    driver.get("http://demo.com/test/simple_context_menu.html");
    driver.manage().window().maximize();
    String title= driver.getTitle();
    Thread.sleep(5000);
    driver.quit();

its working fine when i declared inside void method below code

package QAInstallExtentionTest;

import java.io.File;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

public class  testingImplementAreatest{

public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-notifications");
    options.addArguments("--incognito");

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    options.merge(capabilities);
    ChromeDriver driver = new ChromeDriver(options);        
    driver.get("http://demo.com/test/simple_context_menu.html");
    driver.manage().window().maximize();
    String title= driver.getTitle();
    Thread.sleep(5000);
    driver.quit();
    System.out.println("finished automation" +title);
    }
}

but when I declare same in common (outside of void method )it's not working and shows error

package QAInstallExtentionTest;

import java.io.File;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

public class  testingImplementAreatest{

    ChromeOptions options = new ChromeOptions();  // declare chromeOptions
    options.addArguments("--disable-notifications");
    options.addArguments("--incognito");
    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    options.merge(capabilities);
    ChromeDriver driver = new ChromeDriver(options); // declare Chrome driver   
public static void main(String[] args) throws InterruptedException {
    // TODO Auto-generated method stub
    driver.get("http://demo.com/test/simple_context_menu.html");
    driver.manage().window().maximize();
    String title= driver.getTitle();
    Thread.sleep(5000);
    driver.quit();
    System.out.println("finished automation" +title);
    }
}

Please help me to solve this? and where I make mistakes?

Thanks in advance pal & for more details issue image

0 Answers0