Page Object Model(POM)
Page Object Model (POM): A Paradigm for Efficient Test Automation
Page Object Model (POM) is a widely adopted design pattern in test automation, particularly for Selenium WebDriver. This pattern establishes an Object Repository for web UI elements, offering advantages such as reduced code duplication and improved test maintenance. The core idea behind POM is to create a Page Class for each web page in the application, encapsulating the identification of WebElements and defining methods that perform operations on these elements.
Key Components of Page Object Model
Page Class:
For every web page in the application, a corresponding Page Class is created. This class not only identifies the WebElements present on the page but also contains methods that execute operations on these elements. Naming conventions play a crucial role in POM, with method names reflecting the tasks they perform. For instance, in a scenario where a loader is in standby mode until the payment gateway interface becomes visible, the method name could be "waitForPaymentScreenDisplay()."
Why Adopt Page Object Model?
With Selenium WebDriver, initiating a UI automation is NOT a difficult task. All you have to do is locate the elements and perform on them.
Consider a simple login script, where elements are found and values are filled. Initially, script maintenance appears manageable, but as the test suite grows, problems arise.
Access details to demo site. |
|
User ID : |
mngr478189 |
Password : |
vabAqUn |
Challenge with Script Maintenance:
- Code Duplication: If multiple scripts utilize the same page element, any change to that element necessitates modification in all scripts. This is both time-consuming and error-prone.
Developing a separate class file to locate, fill, or validate web elements is a better way to handle script maintenance. You can use this class again in any script that uses that particular element. When a web element changes in the future, we should only need to make changes to one class file rather than ten separate scripts.
In Selenium, this method is referred to as Page Object Model. It enhances the readability, reliability, and reuse of the code.
Advantages of POM
Code Clarity:
POM separates the user interface (UI) operations from verification, making the code more organized and easy to understand. This clarity is especially beneficial when multiple team members collaborate on the same project.
Improved Maintenance:
By creating a separate class for each web page, POM reduces code duplication. This means that when a change occurs in a web element, you only need to update the corresponding class, streamlining maintenance and minimizing the chances of errors.
Reusability:
POM promotes the creation of reusable components, such as methods for interacting with specific elements. These components can be utilized across multiple scripts and even different tools, enhancing efficiency and saving time.
Independent Object Repository:
The object repository in POM is independent of specific test cases. This independence allows for flexibility in using the same repository for different purposes with different testing tools, providing versatility in testing approaches.
Optimized Code:
POM results in more optimized and concise code due to the use of reusable methods. This not only improves the overall quality of the code but also facilitates easier debugging and maintenance.
Intuitive Method Names:
Methods in POM classes have names that directly reflect the actions performed in the UI. This naming convention enhances the readability of the code, making it easier for developers and testers to understand the purpose of each method.
How to implement POM?
The Simple Page Object Model (POM) is a fundamental structure for organizing and maintaining Web elements and related methods within a class file. This framework helps in creating modular and maintainable code for automated testing.
First, create two packages, like testcases and page objects.
In the page object, create two classes
- login process
- validation process
TEST CASES FOR LOGIN
package testcases;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import pageobjects.loginpageobjects;
public class logintestcase {
@Test
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:\\Users\\admin\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe" );
WebDriver driver = new ChromeDriver();
driver.get("https://www.amazon.com");
//driver.manage().window().maximize();
driver.findElement(By.xpath("//*[@id=\"nav-link-accountList\"]/span")).click();
Thread.sleep(3000);
//loginpageobjectsLoginpageobjects = new loginpageobjects();
loginpageobjects.username(driver).sendKeys("9789563417");
loginpageobjects.password(driver).sendKeys("jothi123");
loginpageobjects.continues(driver).click();
loginpageobjects.loginbutton(driver).click();
//WebElement username=driver.findElement(By.id("ap_email"));
//username.sendKeys("9789563417");
//WebElement Continues=driver.findElement(By.id("continue"));
//Continues.click();
//WebElement password=driver.findElement(By.id("ap_password"));
//password.sendKeys("jothi123");
//WebElementloginbutton=driver.findElement(By.className("a-button-input"));
//loginbutton.click();
driver.quit();
}
}
PROFILE UPDATES
packagetestcases;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
importorg.openqa.selenium.chrome.ChromeDriver;
importpageobjects.Updatedprofileobjects;
importpageobjects.loginpageobjects;
publicclassupdatedprofile {
publicstaticvoid main(String[] args) throwsInterruptedException {
System.setProperty("webdriver.chrome.driver","C:\\Users\\admin\\Downloads\\chromedriver_win32 (1)\\chromedriver.exe" );
WebDriverdriver = newChromeDriver();
driver.get("https://www.amazon.com");
//driver.manage().window().maximize();
driver.findElement(By.xpath("//*[@id=\"nav-link-accountList\"]/span")).click();
Thread.sleep(3000);
//WebElementusername=driver.findElement(By.id("ap_email"));
//username.sendKeys("9789563417");
//WebElement Continues=driver.findElement(By.id("continue"));
//Continues.click();
//WebElement password=driver.findElement(By.id("ap_password"));
//password.sendKeys("jothi123");
//WebElementloginbutton=driver.findElement(By.className("a-button-input"));
//loginbutton.click();
//driver.quit();
loginpageobjects.username(driver).sendKeys("9789563417");
loginpageobjects.password(driver).sendKeys("jothi123");
loginpageobjects.continues(driver).click();
loginpageobjects.loginbutton(driver).click();
Updatedprofileobjects.myaccount(driver).click();
Updatedprofileobjects.viewgiftcard(driver).click();
driver.navigate().back();
Updatedprofileobjects.managemyprofile(driver).click();
driver.navigate().back();
Updatedprofileobjects.prime(driver).click();
//WebElementmyaccount=driver.findElement(By.xpath("//*[@id=\"nav-link-accountList\"]/span"));
//myaccount.click();
//WebElementviewgiftcard=driver.findElement(By.xpath("//*[@id=\"a-page\"]/div[2]/div/div[3]/div[1]/a/div"));
//viewgiftcard.click();
//driver.navigate().back();
//WebElementmanagemyprofile=driver.findElement(By.xpath("//*[@id=\"a-page\"]/div[2]/div/div[3]/div[3]/a/div/div/div/div[2]/h2"));
//managemyprofile.click();
//driver.navigate().back();
//WebElement prime=driver.findElement(By.xpath("//*[@id=\"a-page\"]/div[2]/div/div[2]/div[3]/a/div/div/div/div[2]/h2"));
//prime.click();
//driver.quit();
}}
LOGIN PAGE OBJECTS
package pageobjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class loginpageobjects {
public static WebElement username(WebDriver driver) {
return driver.findElement(By.id("ap_email"));
}
public static WebElement continues(WebDriver driver) {
return driver.findElement(By.id("continue"));
}
public static WebElement password(WebDriver driver) {
return driver.findElement(By.id("ap_password"));
}
public static WebElementloginbutton(WebDriver driver) {
return driver.findElement(By.className("a-button-input"));
}
}
UPDATE PROFILE PAGE OBJECTS
packagepageobjects;
importorg.openqa.selenium.By;
importorg.openqa.selenium.WebDriver;
importorg.openqa.selenium.WebElement;
publicclassUpdatedprofileobjects
{
publicstaticWebElementmyaccount(WebDriverdriver) {
returndriver.findElement(By.xpath("//*[@id=\"nav-link-accountList\"]/span"))
}
publicstaticWebElementviewgiftcard(WebDriverdriver) {
returndriver.findElement(By.xpath("//*[@id=\"a-page\"]/div[2]/div/div[3]/div[1]/a/div"));
}
publicstaticWebElementmanagemyprofile(WebDriverdriver) {
returndriver.findElement(By.xpath("//*[@id=\"a-page\"]/div[2]/div/div[3]/div[3]/a/div/div/div/div[2]/h2"));
}
publicstaticWebElement prime(WebDriverdriver) {
returndriver.findElement(By.xpath("//*[@id=\"a-page\"]/div[2]/div/div[2]/div[3]/a/div/div/div/div[2]/h2"));
}
}
Conclusion
Page Object Model (POM) stands as a powerful and efficient framework in the realm of test automation. It brings structure, maintainability, and reusability to test scripts, making them more resilient to changes in the application under test. As organizations continue to prioritize test automation as an integral part of their development processes, embracing frameworks like POM becomes pivotal in achieving speed, reliability, and collaboration in the software development lifecycle.In this blog journey, we delved into the fundamental principles and advantages of POM, providing a comprehensive understanding of its significance in the realm of automated testing.