How do you maximize a Chrome window when running a Selenium test?
You can use the builtin method in Selenium WebDriver, which in e.g. Python is driver.maximize_window()
. You call this after opening the window, which opens in the default size you have set, and then resizes it to maximized state. It works, but it’s adds a small delay to your test. Not much, but enough to notice.
There is another way, however, to take control of Chrome. By using Chrome options.
You can find the full source code of the examples in this post on Github – testautomation-dev.
What are Chrome options?
Chrome offers a long list of options to gain more control with the browser, which is just want we want, when doing test automation. Some of the commonly used are:
- run in headless mode
- start maximized
- run incognito
There a many options available, most you will probably never need. You can see all the options in the source code of Chromium chrome_switches.cc class.
Before we look at how to set these options, there is another similar term that you might run into: Desired Capabilities.
Chrome Options or Desired Capabilities?
When we set an property in Chrome, like wanting the window to start in maximized state, we can pass the property using the WebDriver API with the Chrome options class. Now this, as the name implies, only applies to options available in Chrome.
There is another feature called Desired Capabilities, which can be used to set general WebDriver properties, like browser name, SSL-level etc. However, Chrome options can actually also be passed as desired capabilites.
The scope of Desired Capabilities is somewhat different tough, and just keep in mind that Chrome options applies only to ChromeDriver, where as Desired Capabilities applies to WebDriver and relates to general browser settings. You can find a few links in the resources section to learn more about Desired Capabilites.
Passing Chrome Options with Robot Framework
This is how, you can pass your Chrome options to WebDriver in using Robot Framework keywords, and indirectly using the python bindings of WebDriver as shown here in the keyword Create WebDriver With Chrome Options:
Create WebDriver With Chrome Options
${chrome_options} = Evaluate selenium.webdriver.ChromeOptions()
Call Method ${chrome_options} add_argument --start-maximized
Create WebDriver Chrome chrome_options=${chrome_options}
This code stores a reference to the WebDrivers ChromeOptions and adds one argument --start-maximized
to it, and uses this when creating the WebDriver session.
Apart from just starting the browser instance maximized, there are some other interesting options that we can use, apart from the ones mentioned earlier, that you can see here:
Create WebDriver With Chrome Options
${chrome_options} = Evaluate selenium.webdriver.ChromeOptions()
Call Method ${chrome_options} add_argument --log-level\=3
Call Method ${chrome_options} add_argument --start-maximized
Call Method ${chrome_options} add_argument --window-size\=800,600
Call Method ${chrome_options} add_argument --headless
Call Method ${chrome_options} add_argument --disable-extensions
Create WebDriver Chrome chrome_options=${chrome_options}
Notice the \
in --log-level\=3
? This is just to tell Robot Framework not to try and evaluate the expression.
One last thing…
Output the Chromedriver log
The Chromedriver.log file can be helpful in debugging the frontend. Setting the path to the log in Chromedriver, is unlike the remaing options we just looked at, not set as a Chrome option. In stead, this is a service argument. So it goes like this:
Create WebDriver With Service Arguments
@{service_args} = Create List --log-path=chromedriver.log
Create WebDriver Chrome service_args=@{service_args}
But if you want to set the --log-level
, remember to do this in the Chrome options, just because that’s where that goes.
And that’s that.
Conclusion
Selenium WebDriver provides some easy methods to control the Chrome browser running your tests eg. maximizing the browser window. But you can also pass options directly to Chrome by using Chrome options. This gives you more possibilities to control the browser and in the case of maximizing the window, you remove the small delay that Selenium causes by resizing the window, because you can tell Chrome to start maximized.
Chrome options are easily passed to the Selenium WebDriver session using a mix of Python and Robot Framework keywords.
Resources
https://chromedriver.chromium.org/logging
https://chromedriver.chromium.org/capabilities
https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities