CS/IT Turotials

How to Get Started With Selenium WebDriver for Java

Google+ Pinterest LinkedIn Tumblr
We have already introduced the selenium webdriver in the previous article while writing about selenium tool suite. Selenium webdriver supports creating projects for different programming languages such as Java, C#, Python, Ruby, Perl, and PHP. In this article, you will get a step by step guide for installing and setting up the required components for running the first selenium webdriver script in java.

In this article we are writing in detail about installing and configuring the Java Development Kit(JDK), installing Eclipse IDE, downloading the Java client driver and configuring the Eclipse with selenium webdriver. Let’s use the following steps to set up the components for running your first selenium webdriver script on Java.

1. Download and Install Java Development Kit

Java Development Kit(JDK) is a prerequisite for running Eclipse IDE on your computer. So you have to install and configure it before running the Eclipse IDE.

You can download Java Development Kit(JDK) from oracle website as given below.

Java Development Kit (JDK)

Here you have to select “Accept License Agreement” radio option and download the setup file for you OS. For installing on windows, you have to download ‘.exe’ file that is available for both 32-bit and 64-bit windows. 

Now, install JDK on your computer going through the setup wizard. After successfully installing the JDK, you have to specify the path of the ‘bin’ folder on the environment variable. For this, you have to go to ‘Advanced system settings’ under ‘system’ from the control panel and have to click on ‘Environment variables’ button located under the advanced tab of system properties dialog box. You can edit the path variable and add the new path of ‘bin’ folder i.e. “C:\Program Files\Java\jdk-11.0.1\bin” from there.

Environment variables

You can verify the installation and configuration of Java Development Kit(JDK) through command prompt. For this, go to command prompt, type ‘path’ and press enter then verify the presence of recently added path. You can also type the command ‘java -version’ for verifying the version of installed java.

Verifying Java Development Kit (JDK) installation

2. Download and Install Eclipse IDE

After successfully installing and configuring the JDK on your computer, you have to download Eclipse IDE for Java developers from its website. For this, go to download page of Eclipse IDE for Java developers as shown on the image below.

Select the correct version of the Eclipse based on your OS.

When you have downloaded the zip file, extract it on the location from where you want to run the IDE. You can run the program simply double clicking on the “eclipse.exe” file going through the Eclipse folder.

3. Download Selenium Client Driver for Java

When you have completed installing Eclipse IDE on your computer, you have to download selenium client driver file for Java and import into Eclipse. You can download selenium Java client driver file from selenium Java client driver file from selenium website. Selenium client driver files for different languages like C#, Ruby, Python etc. are also available there along with Java.

Selenium client driver for Java

Once you have downloaded the “.zip” file, you have to extract it any of your desired location of your computer that will be easy to remember.

4. Create New Project and Class on Eclipse

After completing all the steps written above, you have to run the Eclipse IDE as given on the step 2. When you have launched the Eclipse, you will get the new window for selecting a workspace. Provide the desired location i.e.”D:\selenium_workspace” where you want to store Eclipse projects.

Now, you have to create a new class under a new Java project. Use the following steps for creating a Java project at first.

  1. Go to the file menu from the menu bar.
  2. Click on New and then click on Java project.
  3. Provide the name of the project i.e.”selenium_project”.
  4. Now click on the finish button to complete creating a new project. 
Create a Java project

Use the following steps to create a new class after creating a Java project.

  1. Go to new project name i.e. “selenium_project” on package explorer window and click on it.
  2. Right click on “src” folder and click on class under new option.
  3. Provide the name of the package i.e. “selenium_package” on the package field.
  4. Give the name of the class i.e. “selenium_class” on the class name field.
  5. Now, give tick mark on “public static void main(string [] args)” as shown on the screenshot and click on finish button.
Create a new Java Class

5. Import Selenium Client Driver on Eclipse

Now, import selenium client driver files on the newly created class from previous step. Use the following steps in order to add JAR files included under Java client driver folder.

  1. At first, right click on the name of the project i.e.”selenium_project” and select properties option.
  2. Then, click on the “Java Build Path” option located on the left side of the properties window.
  3. Go to “Libraries” tab and click on the class name.
  4. Click on “Add External JARs…” button shown on the screenshot below and select all the JAR files included under Java Client Driver folder.
Java Build Path

6. Create the First Selenium Webdriver Script

When you have completed all the steps written above, you will be ready to create your first selenium webdriver script. Write the following code on the newly created class under new Java project. It will open the chrome browser and opens the provided URL. When the execution completed, it prints “Test Passed” on the console.

package selenium_package;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Selenium_Class {

public static void main(String[] args) {
WebDriver driver = new ChromeDriver();	
	    	
// launch Chrome and direct it to the provided URL	  
driver.navigate().to("https://www.siteforinfotech.com/");

System.out.println("Test Passed!");

//close Chrome
driver.close();
		}
	}

If you want to learn more on writing advanced selenium webdriver scripts, read next post having sample selenium webdriver script written in Java for different browsers.

Shuseel Baral is a web programmer and the founder of InfoTechSite has over 7 years of experience in software development, internet, SEO, blogging and marketing digital products and services is passionate about exceeding your expectations.

Write A Comment