top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

How to improve Test Automation efficiency using Page Object Model with Selenium and TestNG

Automating complex web applications involves a lot of coding efforts to manage several web pages. To simplify automation testing, Page Object Model is used. This model handles web pages independently to track changes without having to change the code in multiple pages.


What is Page Object Model?


Page Object Model, also known as POM, is a design pattern which is commonly used in Selenium for Automating the Test Cases.

For implementing POM, we need a Page Library and a Testing Framework to begin with. POM approach can be used in both web applications and mobile applications (as both have multiple pages). But this model cannot be used for API testing.

This model considers each web page as a different class. We can use this as a framework where all locators and methods of a page are stored in one class. Tests are created with the same class name and tested for better understanding. We can call the methods of the page class within test class using the same name.


Why Page Object Model?


1. Reusability: Reusing the same web element across different web pages in the same application just by defining in Object repository.

2. Easy code maintenance: When a web element changes, locator need to be modified only in Object repository and it’s reflected throughout the project.

3. Better Readability: Gives better visual understanding of the elements and usage.


What is Page Factory?

Page Factory is an inbuilt class provided by Selenium WebDriver to support Page Object Design patterns. It is used to initialize the elements of the Page Object.


@FindBy

The @FindBy annotation is the essence of the Page Factory approach. It is used to locate web elements using different locators strategies. It helps in quickly locating the web elements using one search criteria.


@FindBys

To locate a web element with more than one search criteria, you can use @FindBys annotation. In simple words, @FindBys uses multiple @FindBy for each search criteria.


initElements

This is a static method of Page Factory class which is used to initialize all the web elements located by @FindBy annotation.

Implementing Page Object Model using Selenium

· Page Object Model with Page Factory


Step 1: Create a Test Scenario

Consider a simple Login test scenario. In this scenario, Page Factory is used to verify if the user has successfully logged in.

· Launch the WebDriver

· Navigate to the website

· Enter valid username and password credentials

· Login to the Dashboard page and verify the page title

· Logout

Step 2: Use Page Factory to initiate Page Objects in the Login Page class, Home Page class and Dashboard Page class.

Create three different page object classes: Login Page, Home Page and Dashboard Page, as we interact with three modules to execute the test script. All page object and test classes will extend the Base Class, thus inheriting all the base methods. So, implicitly the WebDriver object is initialized.

HomePage.java

Figure: Initialization of Page Objects in Home Page.

LoginPage.java

Figure: Initialization of Page Objects in Login Page.


DashboardPage.java


Figure: Initialization of Page Objects in Dashboard Page.


Step 3: LoginTest test case using TestNG

Here we initiate the WebDriver by extending the Base Class and create objects for the Login Page, Home Page and Dashboard Page classes. Then a call is made to corresponding class methods through the objects created for each of the classes.

· @BeforeTest: This annotation will be executed first before any test belonging to that class.

· @Test: This annotation denotes the method which is a part of the test.

· @AfterTest: This annotation is executed after all test methods.


LoginTest.java




Step 4: Results of LoginTest test case execution


Figure: Results of LoginTest test case execution in Console


Figure: Results of LoginTest case execution in TestNG


If the AUT(Application under Test) undergoes any change at the Login Page or at any page we just need to change the page object class. Thus, we don’t need to change our test script again and again (even for the new release or built). The project structure will look like :



Conclusion

Selenium is a widely used Test Automation tool and implementing Selenium with the Page Object Model is a great way to automate application development and testing. Using Page Object Model, creating an object repository enables easy integration with various tools and makes it easy to maintain code in classes and test scripts. Thereby promoting code reusability and saving coding time and effort.

337 views2 comments

+1 (302) 200-8320

NumPy_Ninja_Logo (1).png

Numpy Ninja Inc. 8 The Grn Ste A Dover, DE 19901

© Copyright 2022 by NumPy Ninja

  • Twitter
  • LinkedIn
bottom of page