How to Create Selenium WebDriver Automation Framework
This will be a series posts of 5 articles where we create a complete Selenium WebDriver framework from scratch. The framework will be a general one so that you can modify or directly use it for your needs.
We can use Maven for project management, TestNG for execution and ExtendReports for reporting. As we have seen in a previous post, we will save the results in a database for future reference n comparison. We will cover the creation of a dashboard over the results in a future post.
You can see the completed project here.
So let’s begin.
These are the various modules of our framework each of which will be covered in separate posts.
The WebDriver core module consists of the following components:
This initializes the browser based on the preferred value during execution. We will be having a UI to trigger the execution where we would provide the browser details along with other data. This will be written onto a config/property file which will be read by the script during execution.
The reason we use a text based config/property file is that this acts as a buffer between the trigger mechanism and the script. This enables us to change the triggering mechanism or to have multiple ways of triggering the execution: locally, from a central dashboard etc.
WebDriver core methods
The helper methods for WebDriver object which makes scripting easy and more intuitive, and increases the ease of coding.
Locators and Object Repository
This section handles locators and object repository. We can have an OR for each page which contains the locator handle and locator value as key value pair. Basically, our OR will be a property file. This helps in the easy modification of the file irrespective of the system. For eg, if you have an excel file, you would need Excel installed in the system where the application is deployed.
Report to output file with Screenshots
This section writes the test steps, test case, and script status to output HTML file along with the screenshots in case of failure.
Result to Database
This optional module helps you write the results in a database. You can refer this post to see why it’s good to have your automation results in a database. I feel like it will a bit too complicated if we discuss the means of using the data from the database in this series. That will be covered as a separate post.
We can have a UI which loads existing scripts for the user to select for execution. Once the scripts are selected for execution, he/she can save the selection as a job for future use. This job can be triggered for execution providing the environment details.
Test Scripts example
We can create some sample test scripts to demonstrate the usage of the framework.
Create Selenium WebDriver Framework
In this section, we will cover the creation of Maven project structure along with the dependencies including TestNG and ExtendReports.
Let’s create the project structure as shown below.
The three packages in main/java handle the execution, reporting and webdriver-core modules of the framework. The test scripts will be maintained in test/java folder.
The resources related to the framework(like chromedriver.exe etc) will be in main/resources, whereas the script resources will be in test/resources folder.
At this point, the pom.xml is as shown below.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<description>A framework for Selenium WebDriver</description>
<!-- https://mvnrepository.com/artifact/com.relevantcodes/extentreports -->
Some points regarding the POM.
- maven-jar-plugin sets the main class and add dependencies (in lib folder) to class path. It also adds the ‘resources/’ folder to the class path in Manifest file. We would be moving all the test resources to this folder while packaging
- maven-dependency-plugin copies the dependent jars to the lib folder
- maven-resources-plugin copies the resources from src/test folder (src/test/resources) to target folder while packaging.
So, when we perform mvn package on the project root:
- The project will be packaged to jar folder inside target folder
- The dependent jar files will be copied to lib folder with the Manifest file modified accordingly
- The test resources (object repository, data files etc) will be copied from src/test/resources folder to target/resources folder.
This enables us to move these files and folders (jar file, lib folder and resources folder) to any server and execute the tests using command
java -jar <jar-file-name>.jar
You can see the final project structure in my github project branch.
In the next post, we can see how to create the WebDriver core part of the framework.