Selenium WebDriver Framework: WebDriver Core Module

This is Part 2 of the Selenium WebDriver Framework Series and in this post, we will be creating the WebDriver Core module.

Selenium Framework WebDriver Core

WebDriver Core Module

The WebDriver core module has the following functionalities.

  • Initialize Browser based on user preference during execution
  • Managing Locators and Object Repositories
  • WebDriver core methods which provide easy access to WebDriver API for scripts

We will see each section in detail:

This is the project structure so far, by the way. This will make it easier for you to get a clear picture when you go through the below sections.

WebDriverCore Project Structure

As you can see, the com.axatrikx.webdriver package which deals with WebDriver Core module is almost complete. As you might have guessed, the common package holds the commonly used classes and exceptions class holds the custom exceptions.

A bit about the classes in commons package.

AxaConfig

We have two configuration files : conf.properties and execution.properties. The script can access the configuration values using the AxaConfig class.

conf.properties: Holds the application configurations like repository folder paths, customizable tokens, proxy settings etc.

execution.properties: Holds the execution properties like browser, default timeout, download directory, browser profiles, and binaries etc. These will be set each time before execution.

Common class

This class holds the string constants which are not expected to change.

Utils class

Utils class has the commonly used utility methods like getCallingClass(), joinPaths(), getResource() etc.

Now let’s go into the WebDriver module.

Initialize Browser

The WebDriver initialization is performed by the WebDriverFactory class. This class performs the initialization of WebDriver object along with ElementHelper class(of which we shall discuss later in this article).

It reads the configurations from conf.properties and execution.properties and provides the details to DriverLoader class which uses the information to load the corresponding driver.

For each browser, we have a class which implements the IDriverLoader interface. For eg, for firefox, we have FirefoxLoader, ChromeLoader for chrome etc. All the initialization steps like using custom binary, custom profile, proxy etc are handled by these classes.

SEE ALSO:   Selenium WebDriver Tutorial Series

The getDriver method in DriverLoader class initializes these separate browser classes based on the user setting as shown below.

Managing Locators and Object Repositories

In our framework, there are a few different ways of locating elements.

  • By object as in normal way (driver.findElement(byObject))
  • Using String locator
    • With an Object Repository (driver.findElement(“{{HomePage>LoginButton}}”))
    • A Script Repository (driver.findElement(“{{LoginButton}}”))
    • Direct locator (driver.findElement(“xpath=//xpath/to/locator”))

I know you are a bit confused about the String locator feature. Lets look at in detail.

Using Object Repository

In our test/resources folder, we have two subfolders – repository and script_repository.

Resources

The repository folder holds the Object Repository(OR) files – we use properties extension for this file. The repository files are ideally text files which contain the locators for each element in key-value pair format. In repository folder, we can have a single OR file for a page. If you are following Page Object model, you can have the name of the Page Object for the corresponding OR.

SEE ALSO:   How to Create Selenium WebDriver Automation Framework

For eg, the HomePage.properties hold the locators for HomePage elements. Here is a sample OR file.

OR Contents

Please note that all these are customizable – from the repository folder names to OR extensions etc.

Let’s see the usage again.

There are a few things to note here.

  • The opening and closing braces – {{ and }} : These are to denote that the string is a repository item. It is configurable in the conf.properties file – you can change it to { and } or any other token of your choosing.
  • The separator > : This separator separates the OR file and the locator key. Here ‘HomePage’ is the OR file (note that the extension .properties is not given) and ‘TextField_SearchBox‘ is the locator key present inside the OR file. It will basically look for ‘TextField_SearchBox’ value inside ‘HomePage.properties’ file.

The separator is also customizable and can be changed in conf.properties file.

  • OR file and locator values : As seen above, the OR file name should be provided before the separator (without extension) and the locator value(present in the OR file) should be given. The locator should be given in ‘<type>=<value>’ format.

eg: xpath=//xpath/value, css=css.Value, name=nameValue, id=idVal etc.

Using Script Repository

Script Repository is similar to Object Repository, but the difference is that Object repository (files in repository folder) are shared across multiple scripts whereas script repository is for a particular test script alone.

For all common or shared locators, you should be using the main object repository.

Usage of Script repository is similar to the main repository with a few minor differences.

No need to specify file name

Since the repository will only be used by the test class, there is no need to provide the file name. The only catch is that the name of the repository should be the fully qualified name of the script class.

SEE ALSO:   Compare Images and Screenshots in Selenium WebDriver

ie. If the test class is named ‘SearchTest’ and is in package ‘com.axatrikx.tests.functionalities‘, then the fully qualified name is ‘com.axatrikx.tests.functionalities.SearchTest‘ and the repository should be created in ‘script_repository’ folder with file name ‘com.axatrikx.tests.functionalities.SearchTest’.

Since file name need not be specified, there is no need of separator too. So the usage is

Using direct locator

If you don’t want to use the repositories, you can provide the locators directly in <type>=<value> format.

eg:

Using By object

You can use the regular by object as well

WebDriver Core Methods

AxaDriver class is the main class used by the scripts to access WebDriver API. Each script should instantiate the AxaDriver class to obtain the driver object which will be used for browser operations.

  • In addition to providing access to the WebDriver API, this class also simplifies the API providing additional methods like authenticateDialog(Credentials), waitForPageLoad(), selectByVisibleText(String, String) etc.
  • All the available methods accept String locators as we had mentioned above, in addition to the regular By object.

Here is a sample Test Script.

Please note that this supports any framework format like PageObject, Module based etc. More details on the usage will be covered in later posts.

You can check out the project so far in this Github branch.

Subscribe to our newsletter or follow on Facebook or Twitter to get updates on the upcoming posts.

You may also like...

Leave a Reply

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