Introduction to ChromeDriver for Selenium with Example

Author - Sukhpreet Singh
13.08.2018
|
0 Comments
||

Introduction to ChromeDriver for Selenium with a Code example to test a Website page title

Chrome driver is used to perform the automation testing in chrome browser. Selenium requires this driver to run your test cases in chrome browser. You can download the latest version of chrome driver from https://sites.google.com/a/chromium.org/chromedriver/downloads

After downloading the chrome driver, extract it in the current project directory. You can also set the path of this folder in the System’s PATH variable.

After extracting the chrome driver in the correct location you need to add the following lines of code to run your test cases in the chrome browser

		System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		/* 

			your can write you code here
			
		*/
		driver.quit();

After that your automated test cases will run in the chrome browser. Make sure to close the object of the chrome driver by invoking the quit() function.

Let us understand this with an example:

In the below example we are testing the title of the webpage

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class AutomationTesting{		
		System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		
		// Expected Title
String expectedTitle = "Webner | Insurance, eLearning and Salesforce Software Development";
		
		// Navigate to webpage
		driver.get("https://webnersolutions.com/");
		
		// Actual Title
		String actualTitle = driver.getTitle();
		
		// comparing expected and actual title
		if ( expectedTitle.equals(actualTitle) ) {
			System.out.println("Actual title and Expected title is same");
		}
		else {
			System.out.println("Actual title and Expected title is not same");
		}
		
		// closing the Chrome Driver
		driver.quit();
}

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

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