Create and Execute your first Webdriver script using Eclipse

I have already posted in my last blog how to install and Configure Webdriver using Eclipse and Java. Now, its time to write the Java code for your testcase of the web application. I will give one simple example of creating a webdriver script with explanation in the form of comments.

For this, we need to write the following code into Eclipse.

package MyPackage;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class MyClass {

    public static void main(String[] args) {
        WebDriver driver = new FirefoxDriver();  //Create a new instance of Firefox Browser
        driver.get("https://www.google.co.in/"); //Open the URL in firefox browser
        driver.manage().window().maximize(); //Maximize the Browser window
        String str = driver.getCurrentUrl(); //Get the current page URL and store the value in variable 'str'
        System.out.println("The current URL is " + str); //Print the value of variable in the console
    }
}

Now, click on the Run Menu of the Eclipse window to execute the script.

As an output, It will open the firefox browser , maximize the browser window, get the current page URL and print the value in the Console.

Setup and configure Selenium Webdriver With Eclipse and Java

In this post, we will discuss how to setup and configure Selenium Webdriver using Eclipse IDE and Java.

Step – 1: In the first step, download and install JDK (Java Development Kit) in your system

For downloading Java, you need to visit the following link http://www.oracle.com/technetwork/java/javase/downloads/index.html

Install_JDK

 

Step – 2: In the second step, download and install Eclipse from the link below

http://www.eclipse.org/downloads/

Eclipse_IDE

 

Step – 3: Download the Selenium Java Client Driver from

http://docs.seleniumhq.org/download/

Java_Client_Driver

Click on the Download link for Java language, you will get a ZIP file named “selenium-2.43.0.zip”. Then extract the contents of this ZIP file on any location say “D:\selenium-2.43.0\”. The unzipped folder contains the ‘libs’ folder , 2 jar files and Change Log.

 

Step – 4: Configure Eclipse to work with Webdriver

Double click on ‘Eclipse.exe’ to launch eclipse

When asked to select for a workspace, just accept the default location. Or, you can change the location

Launch_Eclipse

Create a new project through File → New → Java Project. Name the project as “MyProject”.

Right-click on the newly created project and select New > Package, and name that package as “MyPackage”.

Create a new Java class under MyPackage by right-clicking on it and then selecting New > Class, and then name it as “MyClass”. Your Eclipse IDE should look like the image below.

New_Project

Right-click on MyProject and select “Build Path” and then “Configure Build Path”.

Build_Path

This will open the Libraries tab in Properties dialog. Then on click “Add External JARs..”

Add_Jar_Files

Navigate to D:\selenium-2.43.0\ (or any other location where you saved the extracted contents of “selenium-2.43.0.zip”).

Add all the JAR files inside and outside the “libs” folder.

These are the Jar files present outside the Libs folder

Lib_Outside

 

These are the Jar files present inside the Libs folder

Lib_Inside

 

Then your Properties dialog should look similar to the image below.

Properties_Jar_Files

Finally, click on the OK button and we are done with importing Selenium libraries into our project.  Now, we are ready to write our test in Eclipse and run it in WebDriver. I will post about the first Webdriver script using Eclipse in my next blog.