Sunday, February 21, 2016

Selenium - Browser Capabilities explained (1)


In my first post about Selenium (FluentLenium series will focus on extension features only) I decided to tackle Chrome & Firefox capabilities which allow us to preconfigure browser settings before tests. This seemed quite easy at the beginning, but I quickly realised that huge amount of information available via Google is outdated. Hopefully this post will make things straight.

Prerequisite - you need to understand which method initialises WebDriver in your project in order to override it.

Part I - Chrome

Only this piece of documentation tell us how to preconfigure browser before running tests. It's done via Chrome Options. Due to legacy reasons desiredCapabilities still has to be initialised, which makes the code somehow weird. Here it is (with my formatting):


As you can see above two methods preconfigure our Chrome (description via Javadoc):
1. addArguments() which adds additional command line arguments to be used when starting Chrome 
2. addExperimentalSettings() which sets an experimental option. Useful for new ChromeDriver options not yet exposed through the ChromeOptions API
We may also add extensions via addExtensions() method

This is when the easy part ends and Googling begins. For unknown reason lists of available arguments (called very often switches) and experimental settings aren't linked on official chromedriver site. With some determination I was able to obtain them:
1. Arguments (switches) - peter.sh
2. Experimental options - src.chromium.org

My implementation with example values - feel free to do some code review :) 


Demo:
Comment this line & Try running my FluentLenium test (or just log in on Facebook via Selenium). 


You should see this popup/notification and test failure. Experimental option from above solves this problem.


Part II - Firefox

Update 16.09.2017

Handling FirefoxDriver changed in 2017. For non-deprecated code take a look at my new post: