How to run Selenium WebDriver test cases in Chrome?

SeleniumAutomation TestingTesting Tools

We can run Selenium webdriver test cases in Chrome browser. But before working with Chrome browser with Selenium we have to ensure that the Java JDK, any Java IDE like Eclipse and Selenium webdriver are configured in our system. Next we have to download the Chrome browser driver and configure it to our project by following the below step by step process −

  • As per the available version of the Chrome browser in the system, we have to select the download link. The next page shall be navigated where chrome drivers compatible with various operating systems shall be present.

  • After downloading the chromedriver as per the system configuration, a zip file gets created. We need to extract that and save the chromedriver.exe file at any location.

Next we can configure the Chrome driver by any of the two ways −

  • With System Properties in the Environment Variables.

  • With System Properties in the code.

Let us discuss how to configure chromedriver with System properties within the environment variables −

  • Navigate to Start and find the System and navigate to it. Then select Advanced System Settings. Next under Advanced Tab click on Environment Variables.

  • Select Path from the System Variables, then click on Edit. Inside the Edit environment variable pop up click on New.

  • Now the path of the chromedriver.exe file is added, then click on OK.

Example

Code Implementation

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LaunchChrome{
   public static void main(String[] args) {
      WebDriver driver = new ChromeDriver();
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
   }
}

Let us discuss how to configure chrome driver with System properties within the Selenium code −

  • Add the System.setProperty method in the code which takes the browser type and the path of the chrome driver executable path as parameters.

System.setProperty("webdriver.chrome.driver","<chrome driver path>");

Example

Code Implementation

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LaunchChromeBrowser{
   public static void main(String[] args) {
      WebDriver driver = new ChromeDriver();
      // to configure the path of the chromedriver.exe
      System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe");
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
   }
}
raja
Published on 26-Oct-2020 05:53:46
Advertisements