Automation is being rapidly adopted and code optimization is the key to automate complex applications and processes. Automating complex web applications entails a lot of coding efforts to manage numerous web pages. To simplify automation efforts for development or testing, Page Object Model is used. This model handles web pages independently to track changes without having to change the code in multiple pages. Selenium is a portable and widely-used open-source web application automation testing framework. To achieve improved test automation results, learn how to use Selenium automation using the Page Object Model.
Page Object Model considers each web page as a different class. We can use this as a framework where all locators and methods of a page are stored in one class. Tests are designed with the same class name and tested for better understanding. We can call the methods about the page class within test class using the same name.
There are two different ways to implement the Page Object Model:
We can import the PageFactory class directly in Selenium using the import statement “org. openqa. selenium. support. PageFactory.” PageFactory is a class in Selenium used to initialize the elements declared in the Page Object(s). In PageFactory, we have annotations @FindBy to locate the Webelement in the script. The method “initElements” is used to initialize web elements. Below is a simple test case on how to implement PageFactory.
Consider a simple login test scenario. PageFactory is used to verify if the user has successfully logged in.
Create two classes, one for the Login Page and the other for the Home Page, as we interact with two modules to execute the test script.
Figure: Initialization of the Page Objects in LoginPage.class
Figure: Initiating Page Objects in HomePage.class
In Homepage.Class, a feature called @CacheLookup is used. This should be used if elements are visible in the application more frequently. When the class is referenced for the first time, the element is cached and stored by the PageFactory. This reduces execution time for the subsequent code run because the element is not searched again.
Here we initiate the Webdriver and create objects for the Login Page and Home Page classes. Then a call is made to corresponding class methods through the objects created for each of the classes.
Below are screenshots of the results of the Test Execution within the console and TestNG.
Figure: Results of Login Test execution in Console
Figure: Results of Login Test execution in TestNG
As discussed, the Page Object model is built as a page by page implementation of an application test. Let's understand the implementation in detail with the above scenario of a simple login test where the user logs into the application and the test verifies if the user has successfully logged in but without using PageFactory. Follow the steps below.
Step 1: Identify modules and create a Java page for each one. Create Java pages for the Login page and Home Page for our scenario
Step 2: Identify the elements we interact with within the pages and create Webelements for them using the By class
For example, for Logout the locator is: By logoutLink=By.xpath("//*[@id='option-menu']/li[3]/a")
Step 3: Create methods for interactions to be performed on Webelements
For example, to enter Username and Password in the login page:
public HomePage login (String un, String pwd) { demoUsername.sendKeys(un);
demoPassword.sendKeys(pwd);
demoLoginBtn.click(); }
Let's analyze the above implementation in code.
Figure: Initiate the Webelements using By class and pass Webdriver as an object
Figure: Initiate Webelements with By class and pass Webdriver as an object.
Below are screenshots of the results of the Test Execution within the console and TestNG.
Figure: Results of Login Test case execution in Console
Figure: Results of Login Test case execution in TestNG
Page Object Model is a great way to automate application development and testing. Using the Page Object Model, we can create a middle ground between the test script and application page, which makes maintaining the code easy. Creating an object repository enables easy integration with various tools and makes it easy to maintain code in classes and test scripts. Thereby promoting code reusability and saving coding time and effort.