Maximizing browser window for all Geb tests

There might be different reasons for setting a browser window size for your Geb tests. For us it was simply the fact that some floating elements where overlapping with other elements rendering them unclickable when the browser window was set to occupy only the half of the screen and that’s the default size of a Firefox window started by Webdriver.

There were two requirements for the solution – it had to work on every machine the tests were run without any configuration (so we had to discard all of the solutions involving changing the Firefox profile) and also the window had to be maximized for each of our tests. Unfortunately WebDriver doesn’t allow to easily control browser window size, at least as long as this issue isn’t solved. Therefore we had to revert to resizing the window via Javascript which we don’t like, but it seems to be the only solution for now. Thanks to putting the code into our GebConfig.groovy we got the window maximized for every test. Following is the driver closure we needed to put in our Geb config:

driver = {
    def driver = new FirefoxDriver()
    driver.executeScript("window.resizeTo(screen.width, screen.height)")
    return driver
}

Note that you can use a similar technique to not only maximize but also set the window to a certain size and to do it on a per test basis if you put the resizing code into your test instead of the GebConfig.groovy.

Related posts:

  1. Testing Grails applications with Geb and JsTestDriver on a headless Bamboo CI server

Leave a Reply