Selenium follows up on webelements with the assistance of their properties such ID, name, XPath, and so on. Dissimilar to QTP which has an inbuilt item repository(OR), Selenium has no inbuilt ORs.
Henceforth we have to construct an OR which ought to likewise be viable and available on request. Page Object Model (POM) is a well known plan example to make an Object Repository in which every single one of those webelements properties are made utilizing a class record.
Advantages
- POM is an execution where test items and capacities are isolated from one another, in this manner keeping the code clean.
- The articles are kept autonomous of test contents. An item can be gotten to by at least one test contents, subsequently POM encourages us to make protests once and use them on numerous occasions.
- Since objects are made once, it is anything but difficult to access just as update a specific property of an article.
POM Flow Diagram
Items are made for every last one of the pages and techniques are grown solely to access to those articles. Let us use http://calculator.net for understanding the equivalent.
There are different adding machines related with it and every single one of those articles in a specific page is made in a different class record as static techniques and they all are gotten to through the 'tests' class document in which a static strategy would get to the items.
Example
Let us comprehend it by actualizing POM for percent adding machine test.
Stage 1 − Create a straightforward class (page_objects_perc_calc.java) document inside a bundle and make strategies for every single one of those article identifiers as demonstrated as follows.
package PageObject;
import org.openqa.selenium.*;
public class PageObjectsPercCalc {
private static WebElement element = null;
// Math Calc Link
public static webElement lnk_math_calc(WebDriver driver) {
element = driver.findElement(By.xpath(".//*[@id = 'menu']/div[3]/a"));
return element;
}
//Percentage Calc Link
public static webElement lnk_percent_calc(WebDriver driver) {
element = driver.findElement(By.xpath(".//*[@id = 'menu']/div[4]/div[3]/a"));
return element;
}
//Number 1 Text Box
public static webElement txt_num_1(WebDriver driver) {
element = driver.findElement(By.id("cpar1"));
return element;
}
//Number 2 Text Box
public static webElement txt_num_2(WebDriver driver) {
element = driver.findElement(By.id("cpar2"));
return element;
}
//Calculate Button
public static webElement btn_calc(WebDriver driver) {
element =
driver.findElement(By.xpath(".//*[@id = 'content']/table/tbody/tr/td[2]/input"));
return element;
}
// Result
public static webElement web_result(WebDriver driver) {
element =
driver.findElement(By.xpath(".//*[@id = 'content']/p[2]/span/font/b"));
return element;
}
}
Stage 2 − Create a class with fundamental and import the bundle and make techniques for every last one of those item identifiers as demonstrated as follows.
package PageObject;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class PercentCalculator {
private static WebDriver driver = null;
public static void main(String[] args) {
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.calculator.net");
//Use page Object library now
page_objects_perc_calc.lnk_math_calc(driver).click();
page_objects_perc_calc.lnk_percent_calc(driver).click();
page_objects_perc_calc.txt_num_1(driver).clear();
page_objects_perc_calc.txt_num_1(driver).sendKeys("10");
page_objects_perc_calc.txt_num_2(driver).clear();
page_objects_perc_calc.txt_num_2(driver).sendKeys("50");
page_objects_perc_calc.btn_calc(driver).click();
String result = page_objects_perc_calc.web_result(driver).getText();
if(result.equals("5")) {
System.out.println(" The Result is Pass");
} else {
System.out.println(" The Result is Fail");
}
driver.close();
}
}
Output
The test is executed and the outcome is imprinted in the comfort. Given beneath is the depiction of the equivalent.