89 Selenium - Page Object Model
89 Selenium - Page Object Model
The test cases developed using Selenium Webdriver can be streamlined by incorporating
different design patterns within the tests. One of the widely used design patterns is Page Object
Model, which is popularly known as the POM.
The actual test cases make use of the methods of the object classes to interact with the web
application. After a test case has been developed, in future if any property of the web elements
get changed, then necessary changes would only need to be made at the page object level and
not at the test case level.
Likewise, if a new web element has been added to a web page in application, that should be first
added to the one and only dedicated place where all the objects are declared at the page level.
While designing test cases based on Page Object Model, each page in the application is
considered as a separate class file. Within that independent class file, all the web elements
specific to that page, will be available.
Verifications and Assertions for a test case are maintained in the particular test class file and not
on the page object class file. Thus there is no interdependence between any class files.
There is a proper distinction among code written for test cases, features specific to a
page, locators for the web elements, and so on.
Page Object Model provides something similar to an object repository, where all the web
elements and the actions to be performed on them are stored at a particular place, rather
than being declared in multiple places within the test cases.
In case, a change in property of a web element is seen, modifications specific to that
element need to be made only at a single place, rather than multiple places. Thus it is
easy for maintenance purposes.
As separate and independent class files are maintained, the test code developed for a
page, can be reused for another test case. Hence saving a lot of resources.
As separate and independent class files are developed as per the web pages on the
application, it makes the code very readable and clean by just going through the class
files.
Every distinctive web element is declared only once.
Example
Let us take an example of the below page, where we have the New User button on the Welcome
Page.
On clicking the New User button, we will be navigating to the Registration page, having the
Welcome, Register text as shown in the below image.
For the above example, we would create two page classes - WelcomePage.java and
RegisterPage.java where we would have the web elements on these respective pages and
actions to be performed on those elements. We would also create the test class -
WelcomePageTest.java which would contain the actual test case, and assertions related to that
test case. All the test cases would be under the same POM package.
Code Implementation on page class WelcomePage.java.
package POM;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
package POM;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
package POM;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;
import java.util.concurrent.TimeUnit;
import static org.testng.Assert.assertEquals;
Dependencies in pom.xml.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>SeleniumJava</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>16</maven.compiler.source>
<maven.compiler.target>16</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding
</properties>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/sele
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.11.0</version>
</dependency>
Output
In the above example, we had first identified the web elements on the Welcome and Register
pages and performed actions on them within the page classes - WelcomePage.java and
RegisterPage.java.
Within the test class WelcomePageTest.java, we had created objects of the above mentioned
page classes to create the whole flow of the test case and also added assertions.
We had taken help of the TestNG test framework, to implement the Page Object Model within the
Selenium Webdriver test and retrieved the page headers with the message in the console - Page
heading in Welcome Page: Welcome, Login In and Page heading in Register Page:
Welcome,Register.
The result in the console shows Total tests run: 3, as there are three methods with @Test
annotations - verifyWelcomePageHeading(), moveToRegisterPage(), and
verifyRegisterPageHeading().
Finally, the message Passes: 3, and Process finished with exit code 0 was received, signifying
successful execution of the code.
This concludes our comprehensive take on the tutorial on Selenium Webdriver - Page Object
Model. We’ve started with describing a Page Object Model and walked through the benefits of a
Page Object Model, and an example illustrating how to use it along with Selenium. This equips
you with in-depth knowledge of the Page Object Model. It is wise to keep practicing what you’ve
learned and exploring others relevant to Selenium to deepen your understanding and expand your
horizons.