Java Selenium Chrome Driver Example

Google Chrome dominates the browser market with a massive 64% global market share. With a number of useful features, it is one of the most preferred browsers in the world. Given its importance and usage, it becomes crucial to test all websites on Chrome.

This article will explain how one can perform Selenium tests on a Chrome browser using ChromeDriver. But before that, let's understand what ChromeDriver is and how users can configure it on their systems.

What is a Selenium ChromeDriver?

A ChromeDriver is a standalone server or a separate executable that is used by Selenium WebDriver to control Chrome. It is impossible to run Selenium test scripts on the Google Chrome browser without ChromeDriver. One can easily initialize the object of ChromeDriver using the following command:

WebDriver driver = new ChromeDriver

How to configure ChromeDriver

Step 1: First download the ChromeDriver. Users need to download ChromeDriver for their respective Operating systems from this link. Refer to the image to get a better sense of ChromeDriver downloads.

Selenium ChromeDriver

Step 2: Once the zip file is downloaded for the operating system, unzip it to retrieve the chromedriver.exe executable file. Copy this file to a specific location of your choice.

Step 3: Now copy the path where the ChromeDriver file is saved to set the system properties in environment variables. Follow the steps below to set the path in the environment variables:

1. Right-click on My Computer and click on Properties

Selenium ChromeDriver

Image source

2.  Click on the Change settings option and then click on the Advanced tab

3.  Now select the Environmental variables from the Advanced tab

Selenium tests on Chrome

4. Now, from the available options under system variables, select the Path option and click on Edit

Selenium with ChromeDriver

5. At the end of the string, enter a semicolon ';' and paste the path of your ChromeDriver file that you copied earlier, and click OK.


Heard about SafariDriver? Learn how to how to run Selenium Tests on Safari using SafariDriver.


macOS users can follow the steps below:

  1. Go to the terminal and type the command: sudo nano /etc/paths
  2. Enter the password
  3. At the bottom of the file, add the path of your ChromeDriver
  4. Type Y to save
  5. To exit press Control + C

Steps to run Selenium Tests on Chrome Browser

Now, let's discuss how to run Selenium scripts on a Chrome browser using ChromeDriver.

  1. Install the Eclipse Integrated Development Environment (IDE) and import all the Selenium dependencies into the project directory. (Users can use other IDEs too as per their choice)
  2. Set the properties by specifying the type of driver to be used along with its path describing where it is stored
  3. Initialize the object of the ChromeDriver. This helps in launching the Chrome browser.
  4. To navigate to a particular URL, use the driver.get() method. One can also locate certain web elements using specific locators. To know more about how to locate elements in Selenium, refer to this detailed guide on Locators in Selenium.

Pro Tip: Want to dive deeper into Selenium implementation on BrowserStack with free interactive courses and lab exercises? Visit Test University

Now let's consider a test case example wherein we want to perform 3 simple steps:

  1. Open Chrome Browser
  2. Go to www.google.com
  3. Enter Youtube in the search text box

Did you know: There are more than 75 Chrome Browser versions to test on. Don't miss out on testing across!


Refer to the code snippet below to get a better sense of executing the steps mentioned above:

import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.chrome.ChromeDriver; public class ChromeExample { public static void main(String[] args) {  //Setting system properties of ChromeDriver System.setProperty("webdriver.chrome.driver", "C://Selenium-java browserstack//chromedriver_win32//chromedriver.exe");  //Creating an object of ChromeDriver WebDriver driver = new ChromeDriver(); driver.manage().window().maximize();  //Deleting all the cookies driver.manage().deleteAllCookies();  //Specifiying pageLoadTimeout and Implicit wait driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);  //launching the specified URL driver.get("https://www.google.com/");  //Locating the elements using name locator for the text box driver.findElement(By.name("q")).sendKeys("YouTube");  //name locator for google search button WebElement searchIcon = driver.findElement(By.name("btnK")); searchIcon.click(); } }

When the above program is executed, the ChromeDriver launches the Chrome browser, opens the Google website, and enters the value Youtube in the search box. This is how one can run Selenium tests in Chrome using ChromeDriver.

Run Selenium Tests using ChromeDriver For Free

Although it's vital to test web-apps for Chrome, it's also important for a web app to be optimized for other browsers in the market like Firefox, Safari, Edge, etc. Teams can leverage platforms like BrowserStack which enable QAs to perform cross browser testing for their web-apps across thousand of real browsers and browser versions like Safari, Opera, Edge, etc. All real browsers are installed on real desktop and mobile devices, thus providing the optimal environment for software testing in real user conditions.

martinpeoppon50.blogspot.com

Source: https://www.browserstack.com/guide/run-selenium-tests-using-selenium-chromedriver

0 Response to "Java Selenium Chrome Driver Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel