Page Object Model Implementation

Page Object Model

This tutorial will give you an introduction to "Page Object Model" and its implementation in Selenium using java. Before starting with Page Object Model let's first know what are design patterns.

Page Object Model in Selenium

A Page Object Model is a design pattern that can be implemented using selenium webdriver. It essentially models the pages/screen of the application as objects called Page Objects, all the functions that can be performed in the specific page are encapsulated in the page object of that screen. In this way any change made in the UI will only affect that screens page object class thus abstracting the changes from the test classes.

Advantages of using Page Object Model

  • Increases code re-usability 
  • Improves code maintainability
  • Makes code more readable and less brittle

Creating a Page Object Model in Java

Here, we'll create an automation framework implementing Page Object Model using Selenium with Java. 
Suppose we have to test a Anuko Time sheet Login application with only a login page. To start with we first need to create page objects for all available pages in our application - AnukoLoginFactory.java Then we will create a test class that will create instance of these page objects and invoke there methods to create tests.
Let's take the scenario where in the login page user enters valid credentials and on clicking submit button, user is redirected to home page.

Before that let's understand some basics of page @FindBy elements
Page Factory will initialize every WebElement variable with a reference to a corresponding element on the actual web page based on configured “locators”. This is done through the use of @FindBy annotations. With this annotation, we can define a strategy for looking up the element, along with the necessary information for identifying it:







Right click and run as Java Application should be able to login to AnukoTimeSheet. 


Note :


In summary, PageFactory class can be used to initialize elements of a Page class without having to useFindElement or FindElements. Annotations can be used to supply descriptive names of target objects in the AUT to improve code readability. 

Thanks


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.