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


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: '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(
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
        at java.lang.reflect.Constructor.newInstance(
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(
        at org.openqa.selenium.remote.RemoteWebDriver.execute(
        at org.openqa.selenium.remote.RemoteWebDriver$RemoteWebDriverOptions$RemoteWindow.setSize(
        at com.domo.automation.framework.utility.WebDriverUtil.startWebDriver(
        at com.domo.automation.tests.DomoWebDriverTestCase.setUp(
        at junit.framework.TestCase.runBare(
        at junit.framework.TestResult$1.protect(
        at junit.framework.TestResult.runProtected(
        at junit.framework.TestSuite.runTest(
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(
        at com.intellij.rt.execution.junit.JUnitStarter.main(

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);

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

Related Question