Details of Sample Programs in Webdriver

Details of Sample Programs in Webdriver


In this chapter we are going to work of a sample program in Webdriver. Here, we will be creating a webdriver script that would fetch Mercury Tours website, verify its title Print out the result and close it before the program ends.


The webdriver code for the scenario mentioned above is given below.


package newproject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//comment the above line and uncomment below line to use Chrome
public class PG1 {
   public static void main(String[] args) {
       // declaration and instantiation of objects/variables
WebDriver driver = new FirefoxDriver();
//comment the above 2 lines and uncomment below 2 lines to use Chrome
//WebDriver driver = new ChromeDriver();
       String baseUrl = “”;
       String expectedTitle = “Welcome: Mercury Tours”;
       String actualTitle = “”;

       // launch Fire fox and direct it to the Base URL

       // get the actual value of the title
       actualTitle = driver.getTitle();

        * compare the actual title of the page with the expected one and print
        * the result as “Passed” or “Failed”
       if (actualTitle.contentEquals(expectedTitle)){
           System.out.println(“Test Passed!”);
       } else {
           System.out.println(“Test Failed”);
       //close Fire fox



Explaining the Code

Here is a detailed explanation about the code


Importing Packages

Following two packages should be imported before starting the project



  • Org.openqa.selenium: – Which contains the webdriver class that is must for initiating a new browser which is loaded with a specific driver.
  • org.openqa.selenium.firefox.FirefoxDriver: – Which contains FirefoxDriver class which is necessary to initiate a Firefox specific driver into the browser initiated by webdriver class.



Instantiating Objects and Variables


Here is how a driver object is usually instantiated


WebDriver driver =  new FirefoxDriver()


Here the default Firefox profile will be launched by the Java program. It is just like launching Firefox in the safe mode where no extensions will be loaded


Launching Browser Session

get() method in Selenium Webdriver can be used to launch a new browser and to direct it to the url specified as the parameter.


Driver.get (Base URL);


Get the actual Page Title


getTitle() method in Selenium can be used here to get the page title


Actual title = driver.getTitle();


Compare the Expected and Actual Values


Java if-else structure is used here to make comparison.


If (actualTitle.contentEquals(expectedTitle) ) {

System.out.printIn(“Test Passed!”);

}  else  {

System.out.printIn(“Test Failed!”);



Terminating a Browser Section


close() method will be used to close the browser window




Terminating the Entire Program


Following is the command for terminating the entire program and if the same has been used before closing the browser window, whole Java program will end but the browser window will remain open.




Related posts

Leave a Comment