How to handle Page Object Model (POM) in Selenium Webdriver using Java.

Create a simple class (PageObjectsFacebook.java) within a package and create methods for each one of those object identifiers as shown under.
 
 import org.openqa.selenium.*;  
   
 public class PageObjectsFacebook {  
   
 private static WebElement element = null;  
   
 //Username Text Box  
 public static WebElement txt_username(WebDriver driver) {  
 element = driver.findElement(By.id("email"));  
 return element;  
 }  
   
 //Password Text Box   
 public static WebElement txt_password(WebDriver driver) {  
 element = driver.findElement(By.id("pass"));  
 return element;  
 }  
   
 //Login Button   
 public static WebElement btn_lgin(WebDriver driver) {  
 element = driver.findElement(By.id("loginbutton"));  
 return element;  
 }  
   
 }  


Create a class call Loginfailtext with main and inherited upper super class as shown below.
 import java.util.concurrent.TimeUnit;  
 import org.openqa.selenium.WebDriver;  
 import org.openqa.selenium.firefox.FirefoxDriver;  
   
 public class Loginfailtext {  
   
 static WebDriver driver = null;  
   
 public static void main(String[] args) {  
   
 // WebDriver reference but Firefox object  
 driver = new FirefoxDriver();  
 //Maximize browser window   
 driver.manage().window().maximize();  
 //Go to URL  
 driver.get("http://www.facebook.com");  
 //Set timeout  
 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);  
   
 //Use page Object library now   
 PageObjectsFacebook.txt_username(driver).clear();  
 PageObjectsFacebook.txt_username(driver).sendKeys("abcde@gmail.com");  
   
 PageObjectsFacebook.txt_password(driver).clear();  
 PageObjectsFacebook.txt_password(driver).sendKeys("aaaaaaaa");  
   
 PageObjectsFacebook.btn_lgin(driver).click();  
   
 if (driver.getPageSource().contains("Please re-enter your password")) {  
 System.out.println("Pass");  
 } else {  
 System.out.println("Fail");  
 }  
 driver.close();  
 }  
 }  


2 comments:

  1. "Great blog created by you. I read your blog, its best and useful information. You have done a great work. Super blogging and keep it up.php jobs in hyderabad.
    "

    ReplyDelete
  2. Hi,

    I have been reading your blog. its very useful and nice as well. Thankyou.

    Can you pls. let me know:
    how t find number of items in the dynamic drop down.
    expedia.com is travel website.
    1. Get into expedia.com
    2. Click on Flight tab
    3. In the flight origin write down "Wash" -- this will list out flights going from Washington
    4. Now, need to find number of items displaying... ? -- this is where i need your help.

    Thank you,

    ReplyDelete