Monday, 29 October 2018

POM(page objective modeling)

POM (Page Objective Modeling)
  • Page Object Model is a design pattern to create Object Repository for web UI elements.
  • Under this model, for each web page in the application, there should be corresponding page class.
  • This Page class will find the Web Elements of that web page and also contains Page methods which perform operations on those Web Elements.
  • Name of these methods should be given as per the task they are performing, i.e., if a loader is waiting for the payment gateway to appear, POM method name can be waitForPaymentScreenDisplay().

Advantages of POM

  1. Page Object Patten says operations and flows in the UI should be separated from verification. This concept makes our code cleaner and easy to understand.
  2. The Second benefit is the object repository is independent of test cases, so we can use the same object repository for a different purpose with different tools. For example, we can integrate POM with TestNG/JUnit for functional Testing and at the same time with JBehave/Cucumber for acceptance testing.
  3. Code becomes less and optimized because of the reusable page methods in the POM classes.
  4. Methods get more realistic names which can be easily mapped with the operation happening in UI. i.e. if after clicking on the button we land on the home page, the method name will be like 'gotoHomePage()'.    

EXAMPLE :

step:1 Create two new packages and give name pages and tests
           right click on src->new->packages

step:2 Crate pom of pages
           right click on pages->new-.class
            here you give class name "Loginpage"

Login page pom file
--------------------------------------------------------------------------------------------------------------------
public class Loginpage {
WebDriver driver;
By mail = By.xpath("html/body/div[1]/div[3]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[1]/input");
By pass= By.xpath("html/body/div[1]/div[3]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[2]/input");
By login =By.xpath("html/body/div[1]/div[3]/div/div/div/div/div[2]/form/table/tbody/tr[2]/td[3]/label/input");
//you also use name , id ,css and other behalf of xpath

public Homepage(WebDriver driver)
{
this.driver = driver;
}
//here we create constructor of class

public void getmail(String mailid)
{
driver.findElement(mail).sendKeys(mailid);
//make method of get value of emaild id

public void getpass(String password)
{
driver.findElement(pass).sendKeys(password);
//make method of get value of password

public void log()
{
                driver.findElement(login).click();
}
//make method for click on loging button

//now we make one common method who provide all in one

public void logintofb(String mailid, String password)
{
this.getmail(mailid);
                this.getpass(password);
this.log();
}


--------------------------------------------------------------------------------------------------------------------
now we create all pages which need in testing

Step:4 make main java class in tests package

package tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import pages.Loginpage;
import pages.fbhomepage;

public class Setup {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","D:\\vishal\\selenium\\chromedriver_win32\\dasdf\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("https://www.facebook.com");
Loginpage objlog;
                 //create object of page
objlog = new Loginpage(driver);
objlog.logintomail("vishallaniya1997", "vlaniya@123");
Thread.sleep(3000);
driver.close();

}

}

No comments:

Post a Comment

Jmeter

Database Testing: (1) Add mysql JDBC to Jmeter lib folder and restart Jmeter (2) Right click on test name -> add  Thread group (3)...