Selenium WebDriver Page Object Model Framework Introduction

Page Object Model Framework in Selenium WebDriver has come to be very popular Selenium WebDriver test automation framework in the software industry and many IT companies are following this framework because of its easy to write test scripts, Test script maintenance and reduces the duplication of code.

The main merit of Page Object Model in Selenium WebDriver is that if the Web UI changes for any module pages, it don’t require to change any Selenium WebDriver Test Scripts, we just need to change only the Test Page code within the page objects. 


Page Object Model in Selenium WebDriver


Example:


public class LoginPage{

public static WebDriver driver;
private By pageTile = By.xpath("//input[@title='Selenium WebDriver']");
private By homePage=By.id("home");
private By singInPage=By.id("login");
private By uname=By.id("userName");
private By password = By.id("password");

}




In the above code, we've identified the Element locators and defined in the after the class. In this way we can achieve readability of test scripts and we can easily identify Web Element locators and change the Element Locators as per required only in page object classes.
Page Object model in Selenium WebDriver is useful in preparing the AUT functionalities or Reusable components of a Webpage which we want to automate in a separate Test class. Let’s consider as Login Page.

As per Selenium WebDriver ORG Page Object Model is


"Within your web app’s UI there are areas that your tests interact 
with. A Page Object simply models these as
objects within the test code.This reduces the amount of duplicated code
 and means that if the UI changes,the fix need only be applied in one place."

For the above page I’m creating simile class as LoginPage.class. In this class we’ll write reusable methods to call in another class to implement the Test cases.

Let’s consider entry page as Bing Search Page which will have many options like Search, Sign In, Images, Videos, Maps, News Languages, MSN, Outlook.com links. Here as per user action it will navigates to respective web pages to achieve this all functionalities that we want to automate should have reusable methods/components for each web-page.

Now as our main Entry Page is Bing Home page user can navigate to respective pages by clicking on any link from the Bing Search page. Whenever users are navigating to respective pages, we have to return particular page objects. Otherwise Return the current page object as this action doesn't navigate to another page signified by another Page Object.

The Page Object model advantages.

1. Clean Code between test code and page specific code such as locators and layout.
2. Single Object Repository for all functionalities, No Duplication of codes.

In above situation this allows you to perform modifications only in UI because of CR in current requirements or changes in UI Designs.

As per Selenium WebDriver WIKI Page

There is a PageFactory in the support package that provides support for this pattern, and helps to remove some boiler-plate code from your Page Objects at the same time.

No comments:

Post a Comment