9c. WebDriver – First test script by launching Firefox

This entry is part 29 of 39 in the series Selenium

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 considering 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,

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.

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.

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.

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.

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.

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.

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.

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.

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.

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!

Series Navigation<< 9b. WebDriver – Set Up in Eclipse9d. WebDriver – Executing the Test >>

Leave A Comment

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