Checkout our demo site to practice selenium https://magento.softwaretestingboard.com/

0 like 0 dislike
4.4k views
by The go-to Tester (218 points)
edited by
I am automatating tests using Selenium Webdriver 3.0 in Java language. I want to run test in chrome browser in incognito mode. Test runs succesfully but in normal mode not in incognito mode as needed. My code is

@BeforeTest

public void setUp() {

System.setProperty("webdriver.chrome.driver", "C://Selenium driver/chromedriver.exe");

driver = new ChromeDriver();

ChromeOptions options = new ChromeOptions();

options.addArguments("incognito");

driver.manage().window().maximize();

}

@Test

public void testGooglePageTitleInIEBrowser() {

driver.navigate().to("http://www.google.com");

String strPageTitle = driver.getTitle();

System.out.println("Page title: - "+strPageTitle);

Assert.assertTrue(strPageTitle.equalsIgnoreCase("Google"), "Page title doesn't match");

}

@AfterTest

public void tearDown() {

driver.quit();

}

Browser opens in normal mode.

2 Answers

0 like 0 dislike
by The go-to Tester (181 points)
selected by
 
Best answer

You can use ChromeOptions as below to start chrome in incognito mode.

 

ChromeOptions options = new ChromeOptions();

options.addArguments("-incognito");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);

 

hope that helps!

1 like 0 dislike
by
The code will not open incognito because you are not passing ChromeOption's object while opening the Chrome Browser. The below code will work for the same :

         ChromeOptions c = new ChromeOptions();

c.addArguments("incognito");

System.setProperty("webdriver.chrome.driver","E://chromedriver.exe");

WebDriver driver = new ChromeDriver(c);

driver.get("URL");


This site is for software testing professionals, where you can ask all your questions and get answers from 1300+ masters of the profession. Click here to submit yours now!

...