Search This Blog

Thursday, 22 October 2015

Page Object Model - Example


An example to create a small validation test using Page Object Model

All we will need for this are
  1. One class file for every Page (Page Objects will be defined in this class)
  2. One class file for invoking/writing the tests
Lets start creating the Page Object file
(To make life simpler, I've opted to write a test the google Home page and the Search Result Pages)

This is the Home Page related Class File

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class HomePageTest {
    final String xpathToLogo = "";
    final String xpathToSearchBox = "";
    final String xpathToSearchButton = "";
    final String xpathToImFeelingLuckyButton = "";
    final String xpathToAboutLink = "";
    final String xpathToGmailLink = "";
    final String xpathToImagesLink = "";
    final String xpathToSearchResult = "";

    WebDriver driver = new FirefoxDriver();

    @Before
    public void setUpBrowser() {
        driver.get("http://www.google.com");
    }

    @Test
    public void validateHomePageElements() {
        Assert.assertTrue("Logo is not displayed", driver.findElement(By.xpath(xpathToLogo)).isDisplayed());
        Assert.assertTrue("Search text box is not displayed", driver.findElement(By.xpath(xpathToSearchBox)).isDisplayed());
        Assert.assertTrue("Search Button is not displayed", driver.findElement(By.xpath(xpathToSearchButton)).isDisplayed());
        Assert.assertTrue("Im Feeling lucky link is not displayed", driver.findElement(By.xpath(xpathToImFeelingLuckyButton)).isDisplayed());
        Assert.assertTrue("About Link is not displayed", driver.findElement(By.xpath(xpathToAboutLink)).isDisplayed());
        Assert.assertTrue("Gmail Link is not displayed", driver.findElement(By.xpath(xpathToGmailLink)).isDisplayed());
        Assert.assertTrue("Image Link is not displayed", driver.findElement(By.xpath(xpathToImagesLink)).isDisplayed());
    }

    @Test
    public void validateSearchResultPageElements() {
        Assert.assertFalse("Image Link is displayed", driver.findElement(By.xpath(xpathToImagesLink)).isDisplayed());
        Assert.assertFalse("Gmail Link is displayed", driver.findElement(By.xpath(xpathToGmailLink)).isDisplayed());
        Assert.assertTrue("Search Results are not displayed", driver.findElement(By.xpath(xpathToSearchResult)).isDisplayed());
    }
}

This is the Search Result Page Class file

public class SearchResultPage {
     public SearchResultPage() {
        isLogoDisplayed();
        isPagenationDisplayed();
        isTitleCorrect();
        isSearchButtonDisplayed();
    }

    private void isLogoDisplayed() {
        System.out.println("Logo displayed..??");
    }

    private void isSearchButtonDisplayed() {
        System.out.println("Search Button displayed..??");
    }

    private void isPagenationDisplayed() {
        System.out.println("Pagination displayed..??");
    }

    private void isTitleCorrect() {
        System.out.println("Title Correct..??");
    }

    public  void isSearchResultDisplayed() {
        System.out.println("This is where I can test some other test");
    }

}

This is from where we invoke the tests.

import org.junit.Test;

public class SearchResultPageTest {
    @Test
    public  void validateSearchResultPage() {
        // By creating the object of the SearchResultPage() class, the validations are done
        new SearchResultPage();
    }

    @Test
    public void validateSomethingSpecial() {
        SearchResultPage searchResultPage = new SearchResultPage();
        searchResultPage.isSearchResultDisplayed();
    }
}



No comments:

Post a Comment