Create your First WebDriver script

Before reading this post, first you have to setup the Selenium + JAVA project in the eclipse where you can copy the script explained below and see the actual execution of it. You can go through the previous post by clicking following link where steps to Setup the Project have been here.

Lets create a very basic script in webdriver that would perform following actions :-
a) Launch page in Firefox Browser
b) Get the title of Google Home Page.
c) Verify its title
d) print the comparison result
e) Close browser

First you need to create a new class in a JAVA project where you can write the Selenium Webdriver code to automate the above mentioned scenario.

Create a new class in a project
Once you create a project in Eclipse, you will see a folder called ‘src’ in it. Right click on ‘src’ folder, select ‘New’ and then click on ‘class’.


It will open a popup, where you have to enter required details like Class Name, Package Name etc. Once you done with details, click on ‘Finish’ button.


A newly created class will look like :-


Let’s understand the script step by step:

Step 1: Launch Google Home page on Firefox browser.
In order to launch Firefox Browser, you need to create an object of FirefoxDriver class and provide the Path of Geckodriver.exe.
If you want to know that why GeckoDriver.exe is required for Firefox browser. click here.

        //Initialize driver object
        System.setProperty("webdriver.gecko.driver", "C:/gridsetup/geckodriver.exe");
        WebDriver driver = new FirefoxDriver();

Step 2: Open
You need to call get() function of Selenium Webdriver to open any URL as shown below:

       //Launch Application on browser

Step 3: Get Google Home Page Title
In order to get the title of a page, you need to call getTitle() function as it will return the tile of current Page.

        String actualPageTitle = driver.getTitle();
        //Print title

Step 4: Close Browser
You need to call the close() function to close the browser.

         //close browser

Below is the complete Script
Just copy the below code in your class and execute it.

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Sample {
  public static void main(String s[]) {
        String baseUrl = "";
        String expectedPageTitle = "Google";
        String actualPageTitle = "";
        //Initialize driver object
        System.setProperty("webdriver.gecko.driver", "C:/gridsetup/geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        //Launch Application on browser
        //Get page title and store it in a variable
        actualPageTitle = driver.getTitle();
        //Print title
        if (actualPageTitle.equals(expectedPageTitle)) {
             System.out.println("Test case passed");
         } else {
	     System.out.println("Test case Failed");
         //close browser

In order execute above script, just right click on class > Run As > Java Application. Kindly refer below screen shot in case you have any confusion.


As soon as you click on ‘Java Application’ option, the execution will be started and script output would be printed in Console.


If you really like the information provided above, please don’t forget to like us on Facebook, you can also leave the comment.

Leave a Reply

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