How To Run Test Cases Using Google Chrome In Selenium Automation Script

To run google chrome in your selenium webdriver automation script is similar to the IE Driver server, you need a ChromeDriver executable file also. The theory is the same as IE, this driver also startup a server, and waiting to run the selenium commands come from the selenium webdriver scripts.

1. How To Download The Correct Google ChromeDriver Version.

Download the google ChromeDriver version that matches your google chrome version from the URL https://chromedriver.storage.googleapis.com/index.html.

If the google ChromeDriver version does not match Google Chrome version, when you run your selenium program, it will popup below error message.

Starting ChromeDriver 88.0.4324.96 (68dba2d8a0b149a1d3afac56fa74648032bcf46b-refs/branch-heads/4324@{#1784}) on port 7316
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
org.openqa.selenium.SessionNotCreatedException: session not created: This version of ChromeDriver only supports Chrome version 88
Current browser version is 90.0.4430.85 with binary path /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

Click the google chrome Customize and control Google Chrome( 3 vertical dots on the top right corner ) —> Help —> About Google Chrome menu item to get the google chrome version.

My google chrome version is 90, and I run google ChromeDriver on macOS, so my download version is 90.0.4430.24/chromedriver_mac64.zip.

Save the download file to somewhere and unzip it to get the ChromeDriver executable file. In my macOS, the ChromeDriver executable file saved path is /Users/songzhao/Documents/WorkSpace/Tools/chromedriver.

2. How To Use Google ChromeDriver To Launch Google Chrome Web browser.

You need two steps in your java code to start the google chrome web browser with ChromeDriver. Set system property webdriver.chrome.driver‘s value to your downloaded ChromeDriver executable file path.

Create an instance of the Google ChromeDriver class. Below is the example source code.

package com.dev2qa.webdriver.chrome;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class ChromeBasic {
    
    
    public static void StartChrome() {
        
        try {
            
            // Set the geckodriver.exe path to the system property webdriver.gecko.driver's value.
            System.setProperty("webdriver.chrome.driver", "/Users/songzhao/Documents/WorkSpace/Tools/chromedriver");
        
            //Create Firefox webdriver instance and launch Firefox browser. 
            WebDriver chromeDriver = new ChromeDriver(); 
            
            //Maxmize Firefox browser window 
            chromeDriver.manage().window().maximize();
            
            //Browse google home page. You should add http:// or https:// protocal before www.google.com otherwise there will have an exception occured
            //chromeDriver.get("https://www.google.com");
            chromeDriver.get("http://www.google.com");
    
            //Sleep 2 seconds to wait for the page load complete
            Thread.sleep(2000);
            
            
            // Get the google search box object by the search box name.
            WebElement searchBox = chromeDriver.findElement(By.name("q"));
            // Input search keyword text in the google search box.
            searchBox.sendKeys("selenium");
            
            // Press the keyboard Enter key to close the search tips drop-down list and search.
            searchBox.sendKeys(Keys.ENTER);
        
            // Get the google search button by the search button name.
            // WebElement searchBtn = ffDriver.findElement(By.name("btnK"));
            // Click the search button to search.
            // searchBtn.click();
    
            //Print out action success message.
            System.out.println("Open website success.");
            
            //Sleep 10 seconds to wait for the page load complete
            Thread.sleep(1000);
            
            
            // Quit and close the Firefox web browser.
            chromeDriver.quit();
        }catch(Exception ex){
            
            ex.printStackTrace();
        }
    }
    

    public static void main(String[] args) {
        

        ChromeBasic.StartChrome();

    }

}

When you use any web browser (chrome, IE or Firefox) to browse a web page in selenium webdriver you need to add http:// or https:// before the domain name, otherwise the org.openqa.selenium.InvalidArgumentException: invalid argument exception will be thrown.

Starting ChromeDriver 90.0.4430.24 (4c6d850f087da467d926e8eddb76550aed655991-refs/branch-heads/4430@{#429}) on port 35769
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Apr 26, 2021 9:47:59 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
org.openqa.selenium.InvalidArgumentException: invalid argument
  (Session info: chrome=90.0.4430.85)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'songs-MacBook-Pro.local', ip: '127.0.0.1', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.15.7', java.version: '12.0.1'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 90.0.4430.85, chrome: {chromedriverVersion: 90.0.4430.24 (4c6d850f087da..., userDataDir: /var/folders/qr/mrbn885s649...}, goog:chromeOptions: {debuggerAddress: localhost:52628}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: MAC, platformName: MAC, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webauthn:extension:largeBlob: true, webauthn:virtualAuthenticators: true}
Session ID: 02d8e832f77c1f028784e360b5a22f04
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.get(RemoteWebDriver.java:277)
    at com.dev2qa.webdriver.chrome.ChromeBasic.StartChrome(ChromeBasic.java:27)
    at com.dev2qa.webdriver.chrome.ChromeBasic.main(ChromeBasic.java:65)

3. References.

How To Create Maven Project In Eclipse

How To Add Selenium Server Standalone Jar File Into Your Java Project

How To Use Firefox Webdriver To Launch Firefox Browser Automatically In Java.

How To Run Test Cases Using Apple Safari In Selenium Automation Script.

How To Run Microsoft Internet Explorer And Edge In Selenium Webdriver Automation Script.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.