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 explained.click here.

Lets create a very basic script in webdriver that would perform following actions :-
a) Launch google.com 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’.

FirstScript1

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.

FirstScript2

A newly created class will look like :-

FirstScript3

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 Google.com
You need to call get() function of Selenium Webdriver to open any URL as shown below:

       //Launch Application on browser
        driver.get("http://google.com");

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
        System.out.println(actualPageTitle);

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

         //close browser
         driver.close();   

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 = "http://www.google.com";
        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
        driver.get(baseUrl);
	  
        //Get page title and store it in a variable
        actualPageTitle = driver.getTitle();
        //Print title
        System.out.println(actualPageTitle);
	  
        if (actualPageTitle.equals(expectedPageTitle)) {
             System.out.println("Test case passed");
         } else {
	     System.out.println("Test case Failed");
         }
	  
         //close browser
         driver.close();   
  }
 
}

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.

FirstScript

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

FirstScript

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 *


Notice: Undefined offset: 0 in G:\PleskVhosts\automate-apps.com\httpdocs\wp-content\plugins\cardoza-facebook-like-box\cardoza_facebook_like_box.php on line 924