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:

blogpost1

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

In your ‘test > java’ folder on the project hierarchy, right click the folder and select ‘New > Java Class’. Call the class ‘MyFirstChromeTest’:

blogpost2Add the following code to the new class:

    @BeforeClass
    public static void setupChromeDriverLocation()
    {
        String chromeDriverLocation = "C:/Drivers_automation/ChromeDriver/chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", chromeDriverLocation);
    }

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:

    @Test
    public void basicChromeUsage()
    {
        WebDriver chrome = new ChromeDriver();

        chrome.get("http://the-internet.herokuapp.com/");

        assertTrue(chrome.getTitle().equals("The Internet"));

        chrome.quit();
    }

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:

blogpost3Although 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:

    @Test
    public void basicChromeDriverOptions()
    {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("test-type");
        options.addArguments("disable-plugins");
        options.addArguments("disable-extensions");

        WebDriver chrome = new ChromeDriver(options);

        chrome.get("http://the-internet.herokuapp.com/");

        assertTrue(chrome.getTitle().equals("The Internet"));

        chrome.quit();
    }

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!

  • Abdul

    Hi James,

    Im trying to run this code

    import org.junit.BeforeClass;
    import org.junit.Test;

    @BeforeClass
    public static void setupChromeDriverLocation()
    {
    String chromeDriverLocation = “C:/Drivers_automation/ChromeDriver/chromedriver.exe”;
    System.setProperty(“webdriver.chrome.driver”, chromeDriverLocation);
    }
    @Test
    public void basicChromeUsage()
    {
    WebDriver chrome = new ChromeDriver();
    chrome.get(“http://the-internet.herokuapp.com/”);
    assertTrue(chrome.getTitle().equals(“The Internet”));
    chrome.quit();

    AssertTrue is currently in blue so I can’t statically import it I am getting

    Error:(5, 15) java: class, interface, or enum expected
    Error:(8, 9) java: class, interface, or enum expected
    Error:(9, 9) java: class, interface, or enum expected
    Error:(11, 16) java: class, interface, or enum expected
    Error:(14, 17) java: class, interface, or enum expected
    Error:(15, 17) java: class, interface, or enum expected
    Error:(16, 17) java: class, interface, or enum expected
    Error:(17, 17) java: class, interface, or enum expected

    I’m obviously doing something wrong

    • http://james-willett.com James Willett

      Hi Abdul,

      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:

      import org.junit.Assert;
      import static org.junit.Assert.*;

      Hope that helps.

      James

  • Lijo Philip

    Hi James,

    This is my entire code

    import org.junit.BeforeClass;
    import org.junit.Test;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;

    import static junit.framework.TestCase.assertTrue;

    /**
    * Created by Lijo on 9/5/2016.
    */
    public class MyFirstChromeTest
    {
    @BeforeClass
    public static void setupChromeDriverLocation()
    {
    String ChromeDriverLocation= “C:/driversauto/chromedriver.exe”;
    System.setProperty(“webdriver.chrome.driver”, ChromeDriverLocation);
    ChromeOptions options = new ChromeOptions();
    options.setBinary(“C:\\Users\\Lijo\\AppData\\Local\\Google\\Chrome SxS\\Application\\chrome.exe”);
    ChromeDriver driver = new ChromeDriver(options);
    }
    @Test
    public void basicChromeUsage()
    {
    WebDriver chrome = new ChromeDriver();
    chrome.get(“http://the-internet.herokuapp.com/”);
    assertTrue(chrome.getTitle().equals(“The Internet”));
    chrome.quit();
    }}

    The problem is chrome wont quit automatically. it appears just like in your screenshot.

    am also getting test failed error
    org.openqa.selenium.WebDriverException: unknown error: cannot find Chrome binary

  • ammar

    FAILED: testing
    org.openqa.selenium.WebDriverException: unknown error: failed to write automation extension zip
    (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 713 milliseconds
    Build info: version: ‘2.53.0’, revision: ’35ae25b’, time: ‘2016-03-15 16:57:40’
    System info: host: ‘Ammar-PC’, ip: ‘192.168.100.77’, os.name: ‘Windows 7’, os.arch: ‘amd64’, os.version: ‘6.1’, java.version: ‘1.8.0_45’
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:249)
    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:131)
    at org.openqa.selenium.remote.RemoteWebDriver.(RemoteWebDriver.java:144)
    at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:170)
    at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:159)
    at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:148)
    at test1.maxxto2.testing(maxxto2.java:32)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:774)
    at org.testng.TestRunner.run(TestRunner.java:624)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
    at org.testng.SuiteRunner.run(SuiteRunner.java:261)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.run(TestNG.java:1048)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)