What is POM?
The full form of POM is the Page Object Model. It provides the structural display for the project. In the real-time project, there will be many pages and many functions so framework structure will play a key role in framework updation. Web applications will be updated frequently so there will be many changes on the locators. So the framework should help the locator updation. Let's discuss with the sample example below
Sample Program
Below are the steps that are scripted
- Login to the ticket booking site
- Fill in travel details and search the busses
- Click sign-in option
- Enter mobile number and generate OTP
package travelSite;
import java.awt.AWTException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class LoginSite {
public static void main(String[] args) throws InterruptedException, AWTException {
System.setProperty("webdriver.chrome.driver","F:\\Selenium drivers\\chromedriver.exe");
String landingURL = "https://www.redbus.in/";
WebDriver driver = new ChromeDriver();
//Launch the site
driver.get(landingURL);
driver.manage().window().maximize();
//Enter from location
driver.findElement(By.cssSelector("input[id='src']")).sendKeys("Chennai (All Locations)");
//Enter to location
driver.findElement(By.cssSelector("input[id='dest']")).sendKeys("Erode");
//Enter date in the calender
driver.findElement(By.cssSelector("span[class='fl icon-calendar_icon-new icon-onward- calendar icon']")).click();
driver.findElement(By.xpath("//*[@id='rb- calendar_onward_cal']/table/tbody/tr[5]/td[7]")).click();
driver.findElement(By.cssSelector("li[data-id='123']")).click();
driver.findElement(By.cssSelector("li[data-id='236']")).click();
//Click search button
driver.findElement(By.cssSelector("button[id='search_btn']")).click();
//Click sign in link
driver.findElement(By.cssSelector("div[id='sign-in-icon-down']")).click();
driver.findElement(By.cssSelector("li[id='signInLink']")).click();
driver.quit();
}
}
The above code will work as expected. But the challenges will occur with the above code after some site modification. In many scenarios, developers will change the locator attributes. When you take it in a full application script it is very hard to change locators on all locations and sending values to the field also time-consuming. To overcome this issue the POM model gets introduced
Below is the structure of POM.
Here I had 3 packages(homePage, searchBussesPage, siteLandingPage) to manage the different page functions. TestCases package holds all the testcases. Below are the code that splited in every packages
Package : homePage
package homepage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class Homepage {
By fromLocation = By.cssSelector("input[id='src']");
By toLocation = By.cssSelector("input[id='dest']");
By calanderPick = By.cssSelector("span[class='fl icon-calendar_icon-new icon-onward- calendar icon']");
By calenderDatePick = By.xpath("//*[@id='rb-calendar_onward_cal']/table/tbody/tr[5]/td[7]");
public void HomePage(WebDriver driver, String fromLocationValue, String toLocationValue) throws InterruptedException {
//Enter from location
driver.findElement(fromLocation).sendKeys(fromLocationValue);
//Enter tolocation
driver.findElement(toLocation).sendKeys(toLocationValue);
driver.findElement(calanderPick).click();
driver.findElement(calenderDatePick).click();
Thread.sleep(3000);
driver.findElement(By.cssSelector("li[data-id='123']")).click();
driver.findElement(By.cssSelector("li[data-id='236']")).click();
//Click search button
driver.findElement(By.cssSelector("button[id='search_btn']")).click();
}
}
This package holds the functions that are performed in homepage.
Package : searchBussesPage
package searchBussesPage;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class SearchBussesPage {
By signInOverlay = By.cssSelector("div[id='sign-in-icon-down']");
By SigInLink = By.cssSelector("li[id='signInLink']");
public void signIn(WebDriver driver) {
driver.findElement(signInOverlay).click();
driver.findElement(SigInLink).click();
}
}
This is the simple test to click sign in button in the search busses page. In realtime there will be lot of functions in this search busses page.
Package : siteLandingPage
package siteLandingPage;
import org.openqa.selenium.WebDriver;
public class SiteLandingPage {
public void URLLandingPage(WebDriver driver, String URL) {
driver.manage().window().maximize();
driver.get(URL);
}
}
The purpose of this package is to make the browser to land on entered application
Package : TestCases
package TestCases;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import homepage.Homepage;
import searchBussesPage.SearchBussesPage;
import siteLandingPage.SiteLandingPage;
public class LoginTest {
static String landingURL = "https://www.redbus.in/";
static String fromLocationValue = "Chennai (All Locations)";
static String toLocationValue = "Erode";
public static void main(String args[]) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","F:\\Selenium drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
//Object reference were created for all the classes
SiteLandingPage siteLandingPage = new SiteLandingPage();
Homepage homepage = new Homepage();
SearchBussesPage searchBussesPage = new SearchBussesPage();
//Pass the desired parameters to the methods available on the classes
siteLandingPage.URLLandingPage(driver, landingURL);
homepage.HomePage(driver, fromLocationValue, toLocationValue);
searchBussesPage.signIn(driver);
driver.quit();
}
}
This is the test class to check the login navigation functionality. In this model, to change from and to location it is enough to change in LoginTest class alone. If any of the locator got changed in the future, locators can be easily replaced as all the locator are placed just below the class name.
This basic framework is further modified for easier maintanance. We will see all type of frameworks in upcomming articles
Comments
Post a Comment