Page Object Model
What is Page Object Model?
Page Object Model in Selenium popularly used in test automation. This tutorial will demonstrate how to use Page Object Model in Selenium automation projects to maintain test cases easily.
Introduction:
Page Object is a Design Pattern that has become popular in test automation for enhancing test maintenance and reducing code duplication. A page object is an object-oriented class that serves as an interface to a page of your AUT (Application Under Test). The tests then use the methods of this page object class whenever they need to interact with the UI (User Interface) of that page.
Advantage:
Code reusability – We could achieve code reusability by writing the code once and use it in different tests.
Code maintainability – There is a clean separation between test code and page specific code such as locators and layout which becomes very easy to maintain code. Code changes only on Page Object Classes when a UI change occurs. It enhances test maintenance and reduces code duplication.
Object Repository – Each page will be defined as a java class. All the fields in the page will be defined in an interface as members. The class will then implement the interface.
Readability – There is a clean separation between test code and page specific code which improves better understanding.
How to Use Page Object Model:
I have mentioned simple steps to implement Page Object Model in an already existing program.
Down is a given example for Test Automation without Page Object:
public class SampleLogin {
public WebDriver driver;
@BeforeTest
public void setup() {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://practicetestautomation.com/practice-test-login/");
}
@Test
public void login() {
driver.findElement(By.id("username")).sendKeys("student");
driver.findElement(By.id("password")).sendKeys("Password123");
driver.findElement(By.xpath("//button[@class='btn']")).click();
String ActualTitle = driver.getTitle();
String ExpectedTitle = "Test Login | Practice Test Automation";
assertEquals(ExpectedTitle, ActualTitle);
System.out.println(ActualTitle);
driver.findElement(By.xpath("//article//a")).click();
String ActualTitle1 = driver.getTitle();
String ExpectedTitle1 = "Logged In Successfully | Practice Test Automation";
assertEquals(ExpectedTitle1, ActualTitle1);
System.out.println(ActualTitle1);
}
@AfterTest
public void quit() {
driver.quit();
}
}
Applying the page object techniques in the above example could be rewritten like this using the following steps.
1. Creating Testclass. Here we create an object of WebDriver, maximize browser, implementing waits, launching URL.
2. Creating classes for each page to hold element locators and their methods. Usually, we create page objects for all available pages in the AUT. For each page, we create a separate class with a constructor. Identify all the locators and keep them in one class.
In the below Example I have created Page Objects with two classes.
1. Login Page Class
2. Logout Page class
LoginPage:
import static org.testng.Assert.assertEquals;
import org.apache.hc.core5.util.Asserts;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPage {
private WebDriver driver;
By username = By.id("username");
By password = By.id("password");
By button = By.xpath("//button[@class='btn']");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void enter() {
driver.findElement(username).sendKeys("student");
}
public void enterPassword() {
driver.findElement(password).sendKeys("Password123");
}
public void submit() {
driver.findElement(button).click();
}
}
Logout Page:
package PageObjectModel;
import static org.testng.Assert.assertEquals;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LogoutPage {
WebDriver driver;
By logout= By.xpath("//article//a");
public LogoutPage(WebDriver driver) {
this.driver = driver;
}
public void logoutbutton() {
driver.findElement(logout).click();
}
}
3. Creating TestCases in the TestClass based on above pages. As per my test scenario which was mentioned above, scripts run as follows.
a) Launch browser and open the Sample Login Page.
b) Enter user credentials and do signin.
c) Once the page is signed in, verify the title in homepage.
d) Then Logout from the page.
TestClass:
package PageObjectModel;
import static org.testng.Assert.assertEquals;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.github.bonigarcia.wdm.WebDriverManager;
public class TestClass {
static WebDriver driver;
LoginPage pg;
LogoutPage lg;
@BeforeTest
public void setup() {
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://practicetestautomation.com/practice-test-login/");
}
@Test(priority = 1)
public void loginpage() {
pg = new LoginPage(driver);
String ActualTitle = driver.getTitle();
String ExpectedTitle = "Test Login | Practice Test Automation";
assertEquals(ExpectedTitle, ActualTitle);
System.out.println(ActualTitle);
pg.enter();
pg.enterPassword();
pg.submit();
}
@Test(priority = 2)
public void logoutpage() {
lg = new LogoutPage(driver);
String ActualTitle = driver.getTitle();
String ExpectedTitle = "Logged In Successfully | Practice Test Automation";
assertEquals(ExpectedTitle, ActualTitle);
System.out.println(ActualTitle);
lg.logoutbutton();
}
@AfterTest
public void TeardownTest() {
TestClass.driver.quit();
}
}
Summary:
Page Object Model in Selenium WebDriver is an Object Repository design pattern. The Page Object Model design pattern helps you develop faster, easier and cleaner tests. Selenium page object model creates our testing code maintainable, reusable.