Synchronization in Selenium Webdriver - Selenium Tutorials

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.

Synchronization in Selenium Webdriver

package newproject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//comment the above line and uncomment below line to use Chrome
//import org.openqa.selenium.chrome.ChromeDriver;
public class PG1 {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty(“webdriver.firefox.marionette”,”C:\\geckodriver.exe”);
WebDriver driver = new FirefoxDriver();
//comment the above 2 lines and uncomment below 2 lines to use Chrome
//System.setProperty(“webdriver.chrome.driver”,”G:\\chromedriver.exe”);
//WebDriver driver = new ChromeDriver();

String baseUrl = “http://demo.guru99.com/selenium/newtours/”;
String expectedTitle = “Welcome: Mercury Tours”;
String actualTitle = “”;

// launch Fire fox and direct it to the Base URL
       driver.get(baseUrl);

// 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
driver.close();
}
}

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

driver.close();

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.

System.exit(0);

Leave a Reply

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