Java – Mac selenium webdriver chrome window always starts with a small window

javaseleniumwebdriver

I am having an issue using chrome and selenium 2 webdriver. The issue is that when I start my browser session using the chromedriver it always starts in a window that is less than half the size of the available screen width. Because I am doing tests on a page that changes when the screen size changes, my tests fail because I'm trying to drag an element from the top of the page and drop it to an element that is at the bottom of the page. I get a scrolling error. But if the window is maximized, then I don't get this error. But the problem is, every time chrome starts a new session via chrome driver it always starts in a small window. I have explored many different options to get the browser to start maximized:

  • Make a call via javascript to change the size of the window using window.resizeTo(width,height);. However, this doesn't work in later versions of chrome (or firefox for that matter).
  • Start chrome with a switch –start-maximized. This works on windows, but this does nothing on a mac.
  • Start chrome with a switch specifing a profile directory. This way the profile would define the window size. –profile-directory=~/chromeprofile This worked when I started chrome from the commandline, but if I do this from selenium webdriver/chromedriver it doesn't make any difference.
  • When I try: driver.manage().window().setSize()
    I get this exception:

    INFO: Executing: [93debf43cf70ad3557442a7e1aee4620, setWindowSize {"windowHandle":"current","width":2560,"height":1440}]
    org.openqa.selenium.UnsupportedCommandException: [GET, HEAD, DELETE]
    Command duration or timeout: 16 milliseconds
    Build info: version: '2.15.0', revision: '15105', time: '2011-12-08 09:56:25'
    System info: os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.7.2', java.version: '1.6.0_29'
    Driver info: driver.version: RemoteWebDriver
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:147)
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:113)
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:424)
        at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions$RemoteWindow.setSize(RemoteWebDriver.java:578)
        at com.domo.automation.framework.utility.WebDriverUtil.startWebDriver(WebDriverUtil.java:36)
        at com.domo.automation.tests.DomoWebDriverTestCase.setUp(DomoWebDriverTestCase.java:45)
        at junit.framework.TestCase.runBare(TestCase.java:132)
        at junit.framework.TestResult$1.protect(TestResult.java:110)
        at junit.framework.TestResult.runProtected(TestResult.java:128)
        at junit.framework.TestResult.run(TestResult.java:113)
        at junit.framework.TestCase.run(TestCase.java:124)
        at junit.framework.TestSuite.runTest(TestSuite.java:243)
        at junit.framework.TestSuite.run(TestSuite.java:238)
        at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:71)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:199)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62)
    

What other options could I explore in order to accomplish this? The issue is that the window is too small? What can I do to automaticallly maximize the window every time chrome starts via webdriver on a mac?

Best Solution

driver.manage().window().setSize() will not currently work with ChromeDriver (as of v19). There is an active chromium issue relating to this feature request.

The current workaround generally recommended is to use DesiredCapabilities to set the window size, but you're right that it doesn't seem to work on the Mac at all.

The only solution I've found is to open a new window using JavaScript and resize the new window in the same fashion. It's described deep in this thread on the topic. The relevant workaround is shown in the code below:

JavascriptExecutor js = ((JavascriptExecutor)driver);
js.executeScript("window.open('','testwindow','width=400,height=200')");
driver.close();
driver.switchTo().window("testwindow");
js.executeScript("window.moveTo(0,0);");
js.executeScript("window.resizeTo(1280,800);");

I'm afraid it's quite clunky, but it worked for me locally using chrome on the Mac (Lion).

Related Question