Showing posts with label InitElements. Show all posts
Showing posts with label InitElements. Show all posts

Saturday, July 20, 2024

Understanding Page Object Model using workflows and examples

Page Object Model: Overview and Benefits

Page Object Model (POM) is a design pattern used in Selenium automation testing to enhance test maintenance and reduce code duplication.

Key concepts of Page Object Model:

Code Reusability:  Page Objects separate the test logic from the UI logic, promoting reuse of code across multiple tests.

Code Maintainability: Changes to UI elements identifiers are updated within the Page Object resulting minimal impact on test classes.

Improves Test Structure: Tests become more readable and easier to maintain as they interact with methods exposed by Page Objects rather than dealing with raw Selenium commands.

Reduces Duplication: Common interactions with elements (like clicking a button, entering text, etc.) are encapsulated within methods of Page Objects, eliminating redundant code in tests.

Scalability: Supports scaling of automation efforts by providing a structured approach to managing page interactions.


Page Object Model - Workflow



Components of Page Object Model (POM)

Page Object Model


Page Objects 

  • Classes that encapsulate the behavior and structure of a web page.
  • Each page in the application under test has its own corresponding Page Object class
  • Page Objects interact with the page elements (like button, input) and provide methods to perform actions on these elements
  • A Page class has:
    • Identifiers of elements in the Page.
    • methods to work with the identified elements in the page

Page Object Factory

  • Page Factory is a concept in POM that initializes elements of a Page Object.
  • It uses annotations (@FindBy) to locate elements on the web page.
  • These annotations are used in Page Object classes to find and initialize WebElement objects.

BasePage Class

  • This class serves as a foundational class that other Page Objects inherit from. 
  • Examples of common functionality that are shared across multiple Page Objects and should be defined in BasePage class includes:
    • WebDriver Initialization - Initialize the WebDriver instance in the base Class, so that it is used in the page Objects
    • Common Page Actions - Define reusable methods for common actions that occur across multiple pages.
    • Common Page Interactions: methods for handling common interactions  across different pages like handling alerts, executing JavaScript.

    • Wait Mechanisms: Implement methods for waiting and waiting strategies to be used across pages

Any additional method that might be beneficial across multiple Page Objects.

Relation between Test Class and Page Class



Base Class with logic for common implementations


Code example base class


Creation of Page class extending the Base class




Implementation of the Page Class in Test class