How to use POM in Selenium (Simple Login Test Case)

Posted by

How to use Page Object Model in Selenium by using webdriver. First of all, make one class where we create our page object which we call later from main class.

Step 1 : Make Class with Objects

  1. import org.openqa.selenium.By;
  2. import org.openqa.selenium.By;
  3. import org.openqa.selenium.WebDriver;
  4. public class pom_wo_pf { By username=By.id(“email”);
  5. By password=By.id(“pass”);
  6. public WebDriver driver;
  7. public void POM_FB (WebDriver driver) throws InterruptedException
  8. {
  9. driver.findElement(username).click(); driver.findElement(username).sendKeys(“test_test@yahoo.com”); driver.findElement(password).click(); driver.findElement(password).sendKeys(“Password”); if(driver.findElements(By.id(“u_0_5”)).size() != 0)
  10. {
  11. System.out.println(“Element is Present”);
  12. driver.findElement(By.id(“u_0_5”)).click();
  13. }
  14. else if(driver.findElements(By.id(“u_0_2”)).size() != 0)
  15. {
  16. System.out.println(“Element is Absent”);
  17. driver.findElement(By.id(“u_0_2”)).click();
  18. }
  19. else {
  20. System.out.println(“Element is Absent 02”);
  21. }
  22. }
  23. }

Step 2: Now Create main class and call your child class

  1. import org.openqa.selenium.WebDriver;
  2. import org.openqa.selenium.WebDriver;
  3. import org.openqa.selenium.firefox.FirefoxDriver;
    public class pom_use
  4. {
  5. static WebDriver driver;
  6. public static void main(String[] args) throws InterruptedException
  7. {
  8.  driver=new FirefoxDriver();
  9. driver.manage().window().maximize();
  10. driver.get(“https://www.facebook.com/”);
  11. pom_wo_pf pom_fb_check = new pom_wo_pf(); pom_fb_check.POM_FB(driver);
  12. }
  13. }

Leave a Reply

Your email address will not be published. Required fields are marked *