Creating Script Using Selenium WebDriver

A Selenium WebDriver is an open source tool for automating the testing of web applications across many browsers. Selenium supports multiple web browsers such as IE, Firefox, Chrome etc. Selenium Webdriver is used to control the browser.

Here in this article we are creating a script which will perform the following steps.
  1. Open c-sharpcorner's homepage.
  2. Verify the title of homepage
  3. Comparing and print out the result of comparison
  4. Closing the Browser Session

Steps to perform the above mentioned scenario:

Step 1: Open Eclipse ID and create a new java project and name it 'FirstSeleniumProject'.

Step 2: Right click on the project name and add a new package and name it 'mypackage'.

Step 3: Right click on the project name and add a new class and name it 'myclass'.

Step 4: Now add all selenium library jar files by right click on the project name and select Configure build path from Build Path option.

Step 5: Copy and paste the following code in the myclass class.

  1. package mypackage;  
  2. import org.openqa.selenium.WebDriver;  
  3. import org.openqa.selenium.firefox.FirefoxDriver;  
  4. public class myclass  
  5. {  
  6.     public static void main(String[] args)  
  7.     {  
  8.         // declaration and instantiation of objects  
  9.         WebDriver driver = new FirefoxDriver();  
  10.         // launch Firefox and redirect it to c-sharpcorner homepage  
  11.         driver.get("http://www.c-sharpcorner.com");  
  12.         String expectedTitle = "C# Corner - Developers and IT Professionals Community";  
  13.         // get the actual value of the title  
  14.         String actualTitle = driver.getTitle();  
  15.         // compare the actual title of the page with the expected one  
  16.         if (actualTitle.equals(expectedTitle))  
  17.         {  
  18.             System.out.println("Test Passed!");  
  19.         }  
  20.         else  
  21.         {  
  22.             System.out.println("Test Failed");  
  23.         }  
  24.         // close Firefox  
  25.         driver.close();  
  26.     }  
  27. }  

Explaining the code

Importing Packages

We need following packages to proceed further:

  1. org.openqa.selenium.*: This package contains the WebDriver class needed to instantiate a new browser.

  2. org.openqa.selenium.firefox.FirefoxDriver: This package contains the FirefoxDriver class needed to instantiate a Firefox-specific driver.

Instantiating objects and variables

  1. // declaration and instantiation of objects  
  2. WebDriver driver = new FirefoxDriver();    

With the above statement the default Firefox profile will be launched because no parameter is passed with the FirefoxDriver class. No extensions are loaded in the default Firefox profile which is similar to launch Firefox in safe mode.

Launching a Browser Session

get() method is used to redirect a newly launched browser session to the URL passed as an parameter.=

  1. // launch Firefox and redirect it to c-sharpcorner homepage  
  2. driver.get("http://www.c-sharpcorner.com");    

Compare the Expected and Actual Values

With the help of a basic if-else structure we are comparing the actual title with the excepted title.

  1. // compare the actual title of the page with the expected one  
  2. if(actualTitle.equals(expectedTitle)){  
  3.    System.out.println("Test Passed!");  
  4.    }else{  
  5.       System.out.println("Test Failed");  
  6. }    
Terminating a Browser Session

With the "close()" method we can close the browser session.
  1. // close Firefox  
  2. driver.close();   

Running the Test

We can execute the code in Eclipse IDE. by clicking on Eclipse's menu bar, click Run > Run.

If you did everything correctly, Eclipse will output "Test Passed!"