Running Selenium Tests in Google Chrome with ChromeDriver

So if you have been following along with my recent blog posts, you should be up and running with Selenium and hopefully executing some basic tests with the Firefox browser. As I mentioned before, Firefox is a great browser to start with because there is no extra configuration required. However, in your automation career you will doubtless need to use additional browsers. Today we are going to look at getting the Google Chrome browser setup for our automation efforts.

Unlike Firefox, the ChromeDriver needs to be downloaded separately, and there is a little configuration to be done in our test setup for it to work. Fortunately it’s all pretty straightforward, and I am going to show you exactly how to do it!

To download the latest version of the ChromeDriver, head on over to the SeleniumHQ downloads page (here). Scroll down the page a bit and you should see Third Party Drivers, Bindings and Plugins. Here they have the ChromeDriver for download (as well as many others). At the time of writing, the version is 2.15:

Click on the link and download the Windows driver ‘chromedriver_win32.zip’ (or if you are on another OS, download whatever one is relevant to you!). Save the ZIP file somewhere suitable on your desk, I normally put the drivers in ‘C:/Drivers_automation‘. Now go into that folder and extract all the files from the ZIP file that you just downloaded. There should only be one file in the ZIP – ‘chromedriver.exe‘.

Now that we have the ChromeDriver downloaded, let’s look at how we can use it in our testing. Firstly, we need to write a little code that tells Selenium where the ChromeDriver is. We only want this code to be executed once, at the start of the test suite, so this is a perfect candidate for a @BeforeClass annotation (as we learned about in the previous blog post).

Selenium will now know where the ChromeDriver is located, because this code will run at the start of every test. Let’s write a very simple test to see if it works. Go ahead and add the following code right under the previous code:

When you are writing the test, you may need to statically import the ‘assertTrue’ method (check the previous blog posts if you need a refresher on how to do that). Go ahead and run the test. You should see a Chrome Browser appear, the website “The Internet” should load, the browser should close and you should get a message that all tests passed. When you run a test through the ChromeDriver, you may see an annoying error message such as below:

Although this message is mostly harmless, it is rather intrusive. We can get rid of this error message by using another class called ChromeOptions. Type out the code below underneath the last @Test you wrote:

The only additional content in the second @Test is the ChromeOptions class. Here we are creating a new ChromeOptions called options, and we are adding the arguments ‘test-type’, ‘disable-plugins’ and ‘disable-extensions’. When we now create the ChromeDriver, we are passing in ‘options’:

WebDriver chrome = new ChromeDriver(options);

Go ahead and run the second test. Everything should run as before, but you should no longer see the yellow warning banner at the top of the browser when the test is running. That should be all you need to know about ChromeOptions initially, but if you want to know more then I suggest you check the project page at: https://sites.google.com/a/chromium.org/chromedriver/capabilities

Congratulations! You now have the ability to run tests in either Chrome or Firefox, two of the most popular browsers on the web. You might be thinking it was quite a lot of effort to get the ChromeDriver working, and you might be wondering if you have to do all the steps above for every test that you write. For now, the answer is ‘yes’ – you will have to do the above for every test class that you write if you want to use the ChromeDriver (other than downloading the ChromeDriver). Once we get a bit further along in our journey, I will write a blog post on how to create a ‘Driver’ class, where we can store all the setup required for the various browsers that we will use. The obvious benefit of having a Driver class is that we will only have to update our configuration settings in one place and all of the tests we write can simply call that Driver class at setup. Stay tuned for that and lots of other exciting future posts!

Sorry, this isn’t very clear. You need to statically import the assertTrue() method to get it to work as in the code.

On the line: assertTrue(chrome.getTitle().equals(“The Internet”));

Try this instead:

Assert.assertTrue(chrome.getTitle().equals(“The Internet”));

When prompted, don’t forget to import the ‘Assert’ class. You can add the static import as well, so that you don’t need to type the word ‘Assert’ everytime. The two imports at the top of the class should look like this: