Whether you use Webdriver to test a web app or for learning, you will most likely use Chrome for running test cases. The Chrome is a natural choice over other browsers. It acquires a 65% market share whereas Firefox has just 10% amongst the popular browsers.
How to Use Chrome for Running Webdriver Test Cases?
To use Firefox, you only add the Selenium Jar, but you would need an additional driver module for running test cases with Chrome. Hence, in this tutorial, we thought to address this issue. So we added a practical example for running Webdriver test cases in Chrome.
Also Read – Selenium TestNG interview questions
Simple steps for running Selenium tests with Chrome
As you have read above barring Firefox, Selenium requires other browsers to provide their driver modules for supporting Webdriver. These drivers are executable files that work as a server for the respective browser. Let’s set up the Chrome driver for running web driver tests. Check the follow-up instructions below.
Step 1: Set up the Chrome driver
To run WebDriver in the Chrome browser, we require ChromeDriver, a separate executable that Selenium WebDriver uses to control Chrome. ChromeDriver gets support from the Chromium team. It’s a standalone server that implements the WebDriver’s wire protocol for Chromium.
Step 2: Select the correct Chrome driver
You have to select the Chrome driver based on your working environment. If you are working on Windows, choose the <Chromedriver_win32.zip>. Save the webdriver file to your local machine.
So, firstly, download the latest version of the ChromeDriver server. You can download it by clicking the below link. Next, you must set the Chrome driver path to run the test cases.
Step 3: Set the Chrome driver path
Next, you’ve to set the property for Chrome driver in the WebDriver code. Here, you need to provide the location of the ChromeDriver executable. See the code given below.
System.setProperty("webdriver.chrome.driver", "pathofChromeDriveronlocalsystem\\chromedriver.exe");
Let’s see an example program written in Java. It’ll help you in running Webdriver test cases.
Sample code to run Webdriver tests in Chrome
In this example, the sample code would do the following.
1- While executing the below code, the Google Home Page will open in Chrome browser.
2- Then, we’ll validate the page title in our example.
Running Webdriver test cases
This example runs a simple webdriver test case and demonstrates its execution in the Chrome browser.
package com.techbeamers.exercises; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class RunWebdriverInChrome { static String driverPath = "C:\\workspace\\tools\\selenium\\"; public WebDriver driver; @BeforeClass public void setUp() { System.out.println("*******************"); System.out.println("launching chrome browser"); System.setProperty("webdriver.chrome.driver", driverPath + "chromedriver.exe"); driver = new ChromeDriver(); driver.manage().window().maximize(); } @Test public void testGooglePageTitleInIEBrowser() { driver.navigate().to("http://www.google.com"); String strPageTitle = driver.getTitle(); System.out.println("Page title: - " + strPageTitle); Assert.assertTrue(strPageTitle.equalsIgnoreCase("Google"), "Page title doesn't match"); } @AfterClass public void tearDown() { if (driver != null) { System.out.println("Closing chrome browser"); driver.quit(); } } }
Execution summary
After you run the above Selenium Webdriver tests in Chrome, it should give you the following output. Please refer to the below screenshot.
Before You Leave
Today, you learned how to set up Selenium Webdriver for running test cases in Chrome. Hopefully, you would have copied the code from our example, and run it successfully. However, if you face any runtime issues, do write us for a solution.
If you want more posts like “How to Use Chrome for Running Webdriver Test Cases“, please visit our site more often.
Lastly, our site needs your support to remain free. Share this post on social media (Linkedin/Twitter) if you gained some knowledge from this tutorial.
Enjoy coding,
TechBeamers.