9c. WebDriver – First test script by launching Firefox

Hiya everyone! Time to grab a cup of Java, I mean coffee 😉

Without further ado, let’s get started with our first test script in WebDriver. Under the same project that we created from our previous post (where WebDriver JAR files are added), I have created a new package named, ‘com.blog.tests’.

Next, right click on the package -> New -> Class.

Test Script - Class Creation

I have named my class as, ‘HelloWorldFirefox.java’.  The Package Explorer pane looks as below once all these are created,

Test Script - Package Explorer

The scenario that we will consider for our first test script is,

  1. Open Firefox browser.
  2. Navigate to ‘https://www.google.com/’
  3. Assert the page title as ‘Google’.
  4. Display a message on the console based on the assertion result.
  5. Close the browser.

Sticky Note: We have an entire post dedicated for Assert and Verify. Since our main motto for our first script is to see the working of WebDriver, let us use simple if-else statements to compare the actual and expected page title.

The code is as below,

package com.blog.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class HelloWorldFirefox {

	public static void main(String[] args) {
//Selenium version 3 beta releases require system property set up
System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\
			Selenium\\geckodriver-v0.10.0-win64\\geckodriver.exe");
		//Create a new instance for the class FirefoxDriver
		//that implements WebDriver interface
		WebDriver driver = new FirefoxDriver();
		//Assign the URL to be invoked to a String variable
		String baseUrl = "https://www.google.com";
		String pageTitle = "";
		String expectedTitle = "Google";
		//Open baseUrl in Firefox browser window
		driver.get(baseUrl);
		//Get the page title and assign to a String variable
		pageTitle = driver.getTitle();
		//Check if obtained page title matches with the expected title
		//and print the console output accordingly
		if(pageTitle.equals(expectedTitle)){
			System.out.println("Hello World! Result is as expected.");
		}else{
			System.out.println("Hello World! Assertion failed!");
		}
		//Close the Firefox browser
		driver.quit();
	}
}

I know that LOOKS intimidating! But don’t worry. We will shed some light on all of that code and find out what it means. Let us go though it line by line leaving no stone unturned.

Code walk-through:

1. System property set up.

System.setProperty("webdriver.gecko.driver", "E:\\Softwares\\Selenium\\
                           geckodriver-v0.10.0-win64\\geckodriver.exe");

Selenium 3 beta versions don’t support direct launch of Firefox as in Selenium 2.* versions. Hence it is required that the path to the driver executable, “geckodriver.exe”  must be set by the “webdriver.gecko.driver” system property. Specify the path where the executable is saved in your system accordingly.

2. Next, in order to instantiate Firefox browser, we will have to import two packages.

Once we type, “WebDriver driver = new FirefoxDriver();” a squiggly line appears below ‘WebDriver’ and ‘FirefoxDriver()’. Upon hovering, eclipse will suggest all possible quick fixes. Click on the first fix that suggests to import respective packages.

test script - import packages

  • Below package specifies the WebDriver interface which is used to instantiate a new browser window as required.
import org.openqa.selenium.WebDriver

Sticky Note:We DO NOT say, WebDriver driver = new WebDriver(); because WebDriver is an interface and it only contains empty methods that are defined but not implemented. Hence cannot be instantiated.

  • FirefoxDriver is a class specific to Firefox browser. It has methods defined and implemented as per the interface WebDriver. Hence this can be instantiated.
import org.openqa.selenium.firefox.FirefoxDriver

Thus, the below statement is kind of like our Gandalf. (Don’t tell me you haven’t read or watched the ‘Lord of the Rings’ series!!) Most powerful and respectful line of all times in SeleniumVille. Because it creates an object for the class FirefoxDriver. We can now use this object, “driver” in this case, to automate various actions on the Firefox browser by invoking the methods already implemented in the FirefoxDriver class as per the contract of WebDriver interface.

WebDriver driver = new FirefoxDriver();

The Firefox browser that will be launched will have a default profile. It will have no extensions and plugins loaded with the Firefox instance and it will run in safe mode. If you wish to know more information on Firefox Profiles, visit ‘https://support.mozilla.org/en-US/kb/profiles-where-firefox-stores-user-data’.

Similarly for other browsers such as Chrome, IE, Safari etc. we have specific classes written following the same contract specified in WebDriver interface (i.e. implementing the methods defined in the interface). We will see how to use other browsers with specific examples in the upcoming post.

3. Now, declaring necessary variables of type String.

String baseUrl = "https://www.google.com";
String pageTitle = "";
String expectedTitle = "Google";

Pretty easy and self explanatory I guess. We are just declaring variables of type String and assigning values to them. The baseUrl is the URL that we wish to invoke for our test scenario in the Firefox browser window. For now, pageTitle is kept empty as this will be actual value that we are going to get from the browser. And expectedTitle is the expected value that will be compared against the actual.

4. Navigating to Google page.

driver.get(baseUrl);

This is the first thing we would like to achieve. ‘get’ method is used to navigate to the specified URL. In this case it is, https://www.google.com.

Sticky Note: driver.navigate().to(baseUrl); also achieves the same result. Give it a try!

5. Obtaining the page title.

pageTitle = driver.getTitle();

The actual value of the page title can be obtained using the getTitle() method. The title is then saved to a variable, pageTitle for further assertions.

6. Comparing page title and displaying the result in console.

if(pageTitle.equals(expectedTitle)){
   System.out.println("Hello World! Result is as expected.");
}else{
   System.out.println("Hello World! Assertion failed!");
}

The actual value stored in pageTitle is checked if it equals the expected value assiged to expectedTitle variable. System.out.println() prints the specified argument and a newline. Just pure Java here!

7. Closing the browser window.

 driver.quit();

The quit() method closes all the browser windows and ends the WebDriver session completely. This avoids memory leaks that might happen if any of the related files are not cleared properly.

Sticky Note: driver.close(); can also be used. The difference is that, it will close the browser window that has the current focus on.

Let’s take a break here so that all this sinks in! We will execute this test in our next post.

See you soon. Have a great day!

Leave A Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.