Page Object Model

Design patterns

A Design pattern is a generic solution to a common software design/architecture problem. Implementation of these design patterns leads to inclusion of best practices and best solution, evolved over the time by others while working with similar problems.

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

1.Increases code reusability - code to work with events of a page is written only once and used in different test cases
2.Improves code maintainability - any UI change leads to updating the code in page object classes only leaving the test classes unaffected
3.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 dummy application with only a login page and a home page. To start with we first need to create page objects for all available pages in our application - LoginPage.java and HomePage.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.

Content of LoginPage.java




Content of HomePagePage.java















Content of Test class - POMTest.java 



This was just a demo for understanding of Page Object Model, an actual project would require several updations like creating abstract classes for page objects, creating base classes for test classes, creating helper and utility classes for database connectivity, for passing test data through config files etc

Comments

Popular posts from this blog

What’s the Difference Between Docker and Kubernetes?

TestNG Listeners

Database Sharding