Search code examples
google-chromeseleniumselenium-webdriveroperaselenium-chromedriver

How to set browser window size for Chrome and Opera launched by Selenium WebDriver in incognito/private mode?


I'm working on application which will execute tests on multiple browser types (Chrome, FireFox, Internet Explorer and Opera). I found the way how to launched them in incognito/private modes (How to open incognito/private window with Selenium WD for different browser types?) and set window size (Browser window control #174):

Window window = driver.manage().window();
window.setPosition(new Point(0, 0));
window.setSize(new Dimension(width, height));

But this code does not work in all cases:

+-------------------+----------+-------------------+
|      Browser      | Standard | Incognito/Private |
|-------------------|----------|-------------------|
|      Chrome       |  works   |   does not work   |
|-------------------|----------|-------------------|
|      FireFox      |  works   |       works       |
|-------------------|----------|-------------------|
| Internet Explorer |  works   |       works       |
|-------------------|----------|-------------------|
|      Opera        |  works   |   does not work   |
+-------------------+----------+-------------------+

How to solve this problem? I know that I can pass arguments to drivers using ChromeOptions and OperaOptions. But I would like to change size during tests executions. It will be great if I don't need to eval JavaScript.


Solution

  • The are some problems with automation testing in Chrome and Opera browsers.

    Issues:

    I temporary solved them using the code:

    • Chrome

      ChromeDriver driver = new ChromeDriver(capabilities);
      driver.get("chrome://extensions-frame");
      WebElement checkbox = driver.findElement(By.xpath("//label[@class='incognito-control']/input[@type='checkbox']"));
      if (!checkbox.isSelected()) {
          checkbox.click();
      }
      
    • Opera:

      OperaDriver driver = new OperaDriver(capabilities);
      driver.get("chrome://extensions");
      WebElement checkbox = driver.findElement(By.xpath("//div[contains(@class, 'incognito-control')]/label/input[@type='checkbox']"));
      if (!checkbox.isSelected()) {
          checkbox.click();
      }