White Box Testing
Test your code
   Home      selenium2WebDriver-PageObject
 
Page Object Pattern
 
Page object pattern is a popular  design pattern used in selenium webdriver automation. Under this pattern create object oriented class for a page to interact, thus in this way the class written serve as interface to the web page. Within the class write methods for each element to interact
 
The Page Object pattern represents the screens of your web app as a series of objects and encapsulates the features represented by a page.
 
with the page object we have following advantages
  • Test framework will be more readable and code for a page elements will be logically grouped together
  • Test framework will be easy to maintain and less redundant code
  • If the UI changes for the page, the tests themselves don’t need to change, only the code within the page object needs to change.
  • There is clean separation between test code and page specific code such as locators (or their use if you’re using a UI map) and layout.
 
For following page http://www.whiteboxtest.com/selenium-test1.php we have written class in following manner for some of web elements
 
public class SeleniumTest1 {
    WebDriver driver;
    WebElement verifybutton;

    public SeleniumTest1(WebDriver driver) {
        this.driver = driver;
        
        // you may Check title of the page and assert if page title is incorrect
        System.out.println("Page title is: " + driver.getTitle());
    }
    
    public void clickButtonPresentByID_id012() {
        verifybutton = driver.findElement(By.id("id-012"));
        verifybutton.click();
    }

    // Find and click selenium-test2
    public SeleniumTest2 clickSeleniumTest2() {
        // Find the text link by its name
        WebElement someElement = driver.findElement(By.linkText("selenium-test2"));
        
        // Click the link text
        someElement.click();
        
        return new SeleniumTest2(driver);
    }
    
    // Find and click selenium-test3
}
 
 
For following page http://www.whiteboxtest.com/selenium-test2.php we have written class in following manner for some of web elements
 
public class SeleniumTest2 {
    WebDriver driver;
    WebElement textBox;

    public SeleniumTest2(WebDriver driver) {
        this.driver = driver;
        
        // you may Check title of the page and assert if page title is incorrect
        System.out.println("Page title is: " + driver.getTitle());
    }
   
    // Find the text box by ID
    public void inputTextBox(String txt) {
        // Find the text-box by ID and input data
        textBox = driver.findElement(By.id("q2"));
        textBox.clear();
        textBox.sendKeys(txt);
    }
   
    // Find and click selenium-test1
    public SeleniumTest1 clickSeleniumTest1() {
        // Find the text link by its name
        WebElement someElement = driver.findElement(By.linkText("selenium-test1"));
       
        // Click the link text
        someElement.click();
       
        return new SeleniumTest1(driver);
    }
   
    // Find and click selenium-test3
}
 
 
Following test code to run test cases
 
public class testMain {
    
    public static void main(String[] args) {

        // Create a new instance of the Firefox driver
        WebDriver driver = new FirefoxDriver();

        // Now use the firefox instance driver to open a page URL
        driver.get("http://www.whiteboxtest.com/selenium-test1.php");
        // driver.navigate().to("http://www.google.com"); alternative to open a
        // URL
       
        SeleniumTest1 seleniumTest1 = new SeleniumTest1(driver);
        seleniumTest1.clickButtonPresentByID_id012();
        SeleniumTest2 seleniumTest2 = seleniumTest1.clickSeleniumTest2();
        seleniumTest2.inputTextBox("hello");
        seleniumTest1 = seleniumTest2.clickSeleniumTest1();

        // Close and clean firefox driver instance
        driver.quit();
    }

}
 
 
Download example source code Selenium Webdriver Page Object Pattern
 
 
External References