How to Set Up Page Object Model in Automation Framework
Print Friendly, PDF & Email

Making Selenium test cases can result in an unmaintainable task. Don’t believe me?

Well, one of the major reasons behind this fact is that an excessive number of copied code is used for this purpose. The copied code could be caused by common functionality and this will further bring about copied utilization of locators. The drawback of the copied code is that the task is less viable.

On the other hand, when some locator will change, you need to go through the entire test code to alter locators where necessary. Don’t worry, this can be sorted out with the utilization of the page object model by creating a strong test code, which will further eliminate the duplicate test code. Alongside that, it enhances the readability and enables us to make intuitive documentation. To wrap things up, we can make tests with less keystroke.

But before starting with the implementation of page object model in automation framework, the first thing you need to know is what actually an automation framework is and why it is necessary to be used? So, let’s widen our knowledge…

Automation Framework

An “Automation Framework” is a platform that is used to provide an environment for execution of automation test scripts. The automation framework provides different benefits that help you in development, execution, and reporting of automation test scripts efficiently. It is more like a system that has created specifically to automate our tests.

In other words, we can say that an automation framework is a combination of various concepts, processes, practices, coding standards, guidelines, modularity, project hierarchies, reporting mechanism etc. to support automation testing. Thus, we can follow these guidelines while automating application to take benefits of different beneficial outcomes.

Advantages of Automation Framework

  • Reusability of code.
  • Maximum coverage.
  • Recovery scenario.
  • Low-cost maintenance.
  • Minimal manual intervention.
  • Easy Reporting.

Types of Automation Framework

By far, we have a basic understanding of what is an Automation Framework. In this segment, we are going to start with the different kinds of Test Automation Frameworks that are accessible in the marketplace. We would likewise attempt shed lights over their upsides and downsides and ease of use proposals.

There is a unique and different scope of Automation Frameworks accessible these days. These frameworks may contrast from each other in light of their help to various key components to do computerization like reusability, the simplicity of support and so on.

There are four commonly used automation frameworks and they are as follows:

  • Modular Driven Automation Framework
  • Data Driven Automation Framework
  • Keyword Driven Automation Framework
  • Hybrid Automation Framework

Modular Driven Automation Framework

In most of the web application, we have few set of codes which are constantly executed in the series of actions. Instead of writing those lines of code again and again in our test, we can write those lines of code into a method and then call that method in our test script where needed.

Modularity avoids duplication of code. In future, if there is any change in the series of action, all you have to do is to make the change in your particular method. No test case will be impacted by that particular change.

Advantages

  • The framework represents the advance level of modularization which in turn leads to much easier and cost-efficient maintenance.
  • This framework provides scalability.
  • If any changes have been done in any part of the application, only the test script having that part of the application needs to be changed and rest of the parts remains untouched.

Disadvantages

  • The test data is hard-coded into the test scripts. Thus, whenever we need to test with a different set of test data, then we need to make the necessary change in the test scripts also.

Data Driven Automation Framework

Sometimes when we are automating or testing any application, it requires a good amount of time to test the functionality multiple times with the various set of test data. Thus, in such cases, we can’t let the test data hard coded in the test script. So it is better to retain test data into some external files outside the test scripts.

Data Driven Automation Framework helps you to differentiate the test script logic and the test data from each other. You can store the test data into an external file. These external files can be property file, XML files, excel files, text files, CSV files etc. The test data is typically stored in “Key-Value” pair. This key is used to make the test data accessible within the test scripts.

Advantages

  • The most imperative element of this framework is that it extensively decreases the total number of test scripts required to cover all the possible scenarios. Consequently lesser amount of code is required to test an entire set of test scenarios.
  • We can execute a test scenario by modifying the values of test data.
  • It also increases maintainability and flexibility.
  • If there is any change in the test data then there is no need to make changes in the test scripts.

Disadvantages

  • It requires some additional effort to work with the test data sources and test script logic to read the test data.

Keyword Driven Automation Framework

The Keyword Driven Automation Framework is an add-on to Data Driven Automation Framework in a sense that it not only differentiates the test data from the test scripts, it also has the specific set of code belonging to the test script into an external data file.

These set of codes are nothing but the keywords and hence the framework is named as Keyword Driven Automation Framework. The test data is stored in a table along with the keywords. You should be aware of that the keywords and the test data are the entities which are independent of the automation tool.

Advantages

  • It is easy to understand.
  • Increases re-usability of code.
  • You can use single keyword across multiple test scripts.

Disadvantages

  • You need to be well aware of the keyword creation system so that you will be able to take the benefits provided by the framework.
  • If the number of keywords increases as per the need the framework will get complicated gradually.

Hybrid Automation Framework

The Hybrid Automation Framework is a fusion of more than one automation framework which is mentioned above. The most important feature of the hybrid framework is you can take the advantage of all kinds of automation framework by making a combination of them. It is the most commonly used framework in test automation.

Now, you have a basic understanding of the automation framework, if you want to leverage the benefits of these frameworks then Page Object Model is the best possible option for you. Let’s start the discussion about Page Object Model that what is it, how we can implement it and why should we use it in our automation framework.

Page Object Model

Page Object Model is basically a design pattern which helps to create object repository for all the web elements. In this, we create a separate class for each screen containing all the web elements which are required to perform some specific operation and also the methods which are created to perform those operations.

One thing which you should keep in mind that the name of the method should be same as the task it is going to perform, e.g. let’s say you have created a method for login to the application then the method name should be login().

We can use Page Object Model in any kind of automation framework such as data-driven, modular, keyword driven, hybrid etc.

PageFactory Class

The PageFactory Class is an add-on to the Page Object Model. We use the PageFactory class to initialize the web elements of a web page. We can initialize web elements without using “FindElement” or “FindElements” with the help of PageFactory class.

The PageFactory class has a static method initElements() which is used to initialize the page objects. We call this method in order to initialize all the web elements. This method takes driver instance of a class and returns the page object along with the fully initialized fields.

For example, if we want to initialize elements of a login page then the syntax will be as follows,

LoginPage loginPage = new LoginPage(driver); 

PageFactory.initElements(driver, loginPage);

or we can do it as a constructor for a particular class

public LoginPage(WebDriver driver) 

{           

this.driver = driver; 

PageFactory.initElements(driver, this); 

The recommended way to initialize the elements of a page is by using a constructor as it takes a WebDriver instance as its argument.

Annotations in PageFactory class

Annotations are used to give some meaningful names for the web elements in PageFactory class in order to improve the readability of code. The commonly used annotation in PageFactory class is @FindBy which is used to identify the web elements of a page. If you need more control on locating the elements in a web page then you need to do that by using @FindBy  annotation.

The @FindBy annotation supports all the locators used to locate the elements such as id, name, className, tagName, linkText, partialLinkText, cssSelector, and xpath. You can use this annotation by using any one of the locators e.g. id.

@FindBy(id = "username")

private WebElement userName;

We can also initialize list of web elements using PageFactory class,

@FindBy(tagName = "a")

private List<WebElement> titleLinks;

Advantages of Page Object Model

  • We can create object repository of the elements by splitting them page-wise, this gives a page repository too and every page will be created as a Java class.
  • If there is any change in the UI of a page then it is very easy to implement those changes in your test scripts which results in low maintenance.
  • We can create every operation which needs to be performed on the elements of a page within the same class which is created for that particular page.
  • Page Object Model helps to increase the readability of the code.
  • Page Object Model helps to reduce the redundancy of the code, i.e. there is no need to write the same piece of code again and again.

Disadvantages of Page Object Model

  • Page Object Model requires high setup time and efforts.
  • You need to have a very good understanding of the programming language which you are using to implement Page Object Model.
  • The framework developed using Page Object Model is specific to a particular application.

Regardless of the above-listed disadvantages, Page Object Model can be termed as the highly effective and most recommended approach to follow for the automation of the application.

Let’s take a simple example to create a Page Object class using PageFactory and how you can use it in your test scripts.

The implementation of Page Object Model is actually very simple you just need to follow some simple steps.

First, you need to do basic setup for that I am using TestNG,

public class LaunchBrowser 



public static WebDriver driver; 

@BeforeClass 

public static void setUp()

{ 

driver = new FirefoxDriver();

driver.get("http://www.abc.com");

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 





@AfterClass 

public static void tearDown()

{ 

driver.close();

 } 

}

Now, analyze the scenario which you need to follow and the elements required to complete your scenario, e.g. you need to signin on www.abc.com. So, first we need to locate, store and initialize the required elements like username field, password field and login button.

public class LoginPage

{

@FindBy(id="username")

private WebElement userName;

@FindBy(id="password")

private WebElement password;

@FindBy(id="login")

private WebElement loginBtn;

public LoginPage(WebDriver driver)

{

this.driver = driver;

PageFactory.initElements(driver, this);

}

public void login(String userid, String pass)

{

userName.sendKeys(userid);

password.sendKeys(pass);

loginBtn.click();

}

}

Now, write your actual test script…

public class LoginTest

{

@Test

public void testLogin()

{

LoginPage lp = new LoginPage(driver);

lp.login("xyz123" , "123456");

}

}

That’s how you can implement Page Object Model!!!

As you can observe, it helps in making your test scripts sound more precise and readable, and also you can maintain your code in an easy manner, as it will allow you with the storage of all your elements at one place i.e. page class.

In case, you need to make some changes due to UI change, then all you need to update is the page class and this will not affect your test scripts.

Simple…Isn’t it? So, let’s get started!

  •  
  •  
  •  
  •  
  •  

LEAVE A REPLY

Please enter your comment!
Please enter your name here