Implementation of Page Object Model - Java Design Pattern:

11:10 PM 1 Comments

Page Object Model is the most popular Design pattern used by the developers as a best practice to develop the project.  Before get into the Page Object Model lets discuss the steps to follow once joined as a Automation Tester in the Company.
Steps to Follow:
1. Acquire Domain knowledge for the project.
2. Understand the (SRS) Software Requirement Specification. 
3. Understanding Manual Test cases Ex: Sanity or Regression Test cases     
4. Executing manual Test cases     
5. Identify the Actions perform in each and every page.  
Example: Consider the website Wikipedia. In the main page user can do the search by providing the input and Select the Language and click on Go button as shown below:








void enterText(String inputData)- To enter the text the Search Text-box Ex: India
void selectLanguage(String lang)- To Select a language Ex: English
void clickOnNext()-> To click on the Go Button
boolean verifyText(String expected)->To verify the Text
SearchResultsPage search(String inputData,String lang)-> Clicking on the Go button, navigates to the Search Result Page. So, this function returns an next page object.
Usually the Methods return the Other Page Objects.
6. Write down the methods in every Page.java. 
7. Identify common functions related to Framework.  
getDriverInstance(String browserType)
launchApp(String url)
closeBrowser()
readExcel(String FileName)     
8. Write testcases using Testng Java Framework and invoke the Page related methods in testcases as shown the below program:
Testng Program: WikiTests.java
package com.wiki.tests;
import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.wiki.lib.AppLibrary;
import com.wiki.pages.HomePage;
import com.wiki.pages.SearchResultsPage;

public class WikiTests {

AppLibrary appLib;
static WebDriver driver;
HomePage hPage;
SearchResultsPage sPage;

@Parameters({"browserType","url"})
@BeforeClass
public void invokeBrowser(String browserType,String url)
{
appLib = new AppLibrary();
driver =appLib.getDriverInstance(browserType);
appLib.launchApp(url);

}
@Parameters({"searchInput","lang"})
@Test
public void verifySearchResults(String searchInput,String lang)
{
hPage = new HomePage(driver);
sPage = hPage.search(searchInput,lang);
boolean result = sPage.verifyText(searchInput);
Assert.assertTrue(result);
}
}
HomePage.java
package com.wiki.pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.Select;
public class HomePage {
WebDriver driver;
public HomePage(WebDriver driver) 
{
this.driver= driver;
}
public void enterText(String inputData)
{
                    driver.findElement(By.id("searchInput")).sendKeys(inputData);
}
public void selectLanguage(String lang)
{
Select select = new Select(driver.findElement(By.id("searchLanguage")));
select.selectByVisibleText(lang);
}
public void clickOnNext()
{
driver.findElement(By.name("go")).click();
}
 
public boolean verifyText(String expected)
{
String actual = driver.findElement(By.xpath("//a[@href='//www.wikidata.org/']/text()")).getText();
boolean b=false;
if(actual.equals(expected))
{
b= true;
}
return b;

}
public SearchResultsPage search(String inputData,String lang)
{
enterText(inputData);  
selectLanguage(lang) ;  
clickOnNext();  

SearchResultsPage s1 =new SearchResultsPage(driver);
return s1;
}



}

References:
 https://code.google.com/p/selenium/wiki/PageObjects

 Advantages of Page Object Model: 
1. Maintenance of Code is easily  done.   
2. Duplication of Code is removed   
3. Save Time & Effort   




Unknown

This site creates a platform to become an expert by gaining knowledge of unknown real-time issues as well.

1 comments:

rmouniak said...


It is a very good blog and useful for students and developer, Thanks for sharing



Java Online Training