With this blog, we are going to learn how to create a Page Object Model based Selenium Automation Framework. Integrate the build tool with the framework and parameterize your commands to execute the desired test in your environment.
The first question anyone would have is why we have chosen Page Object Model?
Before going into Automation Framework model selection, let’s recall the core properties any Automation Framework should have, which are:
1. Low and Easy Maintenance
2. Extensible and Robust
3. Adaptable by various applications
4. User friendly and easy to understand
Let’s try to understand how Page Object Model incorporates these properties.
We will take a very simple example where we need to automate following scenarios.
- Login into an application –>Verify account –>Logout.
- Login into the application –>Update user profile –>Logout.
Now, with these scenarios, if we start creating test scripts straight away , we will create WebElements for all the fields and functional methods corresponding to each functionality in both the scripts.
If you have noticed, Login and Logout functionalities are common in both the test scenarios. But we are repeating creation process of WebElements and functional methods corresponding to these functionalities in both the test scripts.
As the number of test scripts will increase, we will find repeated WebElements and Functional methods in multiple test scripts.
At any point of time, if the element locator of any field gets changed, we will have to make changes in all the test scripts, using that locator. This process will consume a lot of time and effort.
Now think if we could have created an Object Repository containing all these WebElements, how easy it could be to deal with this change.
At this time Page Object Model comes into picture.
Page Object Model is basically a design pattern to create an Object Repository for all the WebElements in UI.
It runs on the concept of Page Factory where it uses @FindBy annotation to get the WebElements.
Now let’s start with the POM based Selenium Automation Framework creation.
First download the project from our git repository: https://github.com/singh-supriya/POM-Based-Selenium-Framework and import it into your Eclipse Workspace. Now we will explain you to develop the likewise/same Selenium Automation Framework here.
Let’s go through the Architecture and Class Diagram of the Page Object Model Automation Framework to have a high level understanding of what we are going to achieve.
We will be creating our Selenium Automation Framework in following steps:
1. Creating Base Of The Selenium Framework:
First of all we will be creating all the utilities that will be used throughout the automation framework. We will be creating these utilities under src.utils package.
The first thing that happens with the Automation Script Execution is instantiation of Webdriver so let’s see how we will do it.
DriverTestCase.java: This class is used to initialize the Selenium Webdriver. It uses the TestNG annotations @BeforeClass where we are initializing the Webdriver and @AfterClass which will get executed after all the executions are done and will quit the Webdriver. We have also created loginSetUp method under this class which will return object of the very first page of the application under execution. We will discuss this in detail later.
DriverHelper.java: This class is been created to handle all the methods related to Webdriver. Any method that is related to Webdriver and will be used globally in the page classes, can be written here.
Log.java: This class is written for enabling logging corresponding to each step of Automation script execution. We are using log4j here which is a reliable, fast and flexible logging Framework written in Java.
PropertyReader.java: We will be putting the data being used globally throughout the Selenium Automation Framework in .properties file so this java class will be used to read the .properties file and update it at runtime.
2. Following POM | Creating pages:
Now its time to create the page classes. We will be putting our page classes under src.pages package.
As we all know, the concept of Page Object Model says that the framework should have page classes corresponding to each web page in application which should contain all the WebElements (element locators) and methods applicable on this page.
In this Page Object Model Framework, we have taken example of a website which is having a Home Page, a Signup Page, Ad Page and Ad Details Page etc. So we have created Page Classes corresponding to all these pages in our Selenium Framework.
Let’s take example of one page class as all the page classes are following the same pattern except their corresponding WebElements (element locators) and methods.
HomePage.java: All the page classes will be extending DriverHelper as DriverHelper is having all the methods related to Selenium Webdriver.
Now we will be adding WebElements (element locators) corresponding to this page using Selenium’s @FindBy annotation.
The class will contain all the methods that can be applied on the same page and each method will return object of a page which could be the same page or another depending on behavior of method.
3. Creating Scripts:
Once we are ready with the utilities and page classes, we are ready to write our Selenium Test scripts. All the test scripts will be written under src.test package.
ScriptFile.java: This class is the main script file. All the script files will be extending DriverTestCase so that the first method of the application(loginSetUp) can be implemented directly and we can have object of the first page.
All the page classes being used throughout the test script will get declared under the script file.
We will be reading all the test data being used in the script from properties file and store it in local variables in the script.
We will be using Selenium’s @Test annotation under the class file and all the execution statements will be written under this annotation.
With Selenium Page Object Model Framework , we create object of only one page class and pass the reference of rest of the page classes in the same object.
In this test script file each called method will return reference object of next or same page and next method will be called from that reference object.
As we can see in this example, verifyHomePage() method is been called from the HomePage which is returning object of same page. But the clickSignUp() method is been called from the Home page which is returning object of SignUpPage.
Corresponding to each test script class file, we will be having a TestNG file that will show up reference of the Java Class file. When we will be executing our Test Script using ant, we will be passing name of this file and the class being referred by this file will get executed.
This folder will contain resources being used in the Selenium Automation Framework.
(a). application.properties: This file will contain data related to Web Application like application url.
(b). testData.properties: This file contains test data being used in the test scripts.
We are using Ant an abbreviation for Another Neat Tool as the build tool in this Page Object Model Framework. Ant uses xml file format for its configuration and build.xml used to be the default file name for it. As all the build files require project and one target element, here we are customizing our target element and name it as ‘run-tests-by-pattern’ . We have also assigned an attribute called ‘pattern’ to this target. This attribute will take the name of script file under execution as its value.
So our command for execution will be:
ant run-tests-by-pattern -Dpattern=Name Of The Test File Under Execution
We are using xml approach of log4j for logging in this Automation Framework. In this file you can give name of the file where you want your logs to get printed.
Once we are done with all the Page Class generation, Script creation and Automation Framework setup, its time to execute our test script.
For this, we need to open the command prompt, move to the directory where you have placed your framework and enter this command:
ant run-tests-by-pattern -Dpattern=File Name to be executed
Once we are done with the test script execution, we can get the results under Reports—>html folder
Open index.html file in your browser and you will be having overview of your result. You can also see the detailed report of each test case by clicking on the name of test case.
This file contains all the logs printed in it throughout the execution of test script.
Fore More Details On Script Creation In Above POM based Selenium Framework, Visit https://www.slideshare.net/SupriyaSingh92/selenium-framework-design-77274912
To Study & Download a Tool Independent Test Automation Framework, Visit Our Blog http://www.wings2automation.com/selenium-tutorial-framework-design/