This is widely used concept in the world on selenium
webdriver also known as POM.
With the use of POM, we can increase readability of code also
it is easy to maintain and user can reuse it.
Basically POM is design pattern to
create object repository (OR).
In POM, we are maintaining different
pages for different web pages. This page class
contains
Elements along with page methods which perform
operation on web element.
The main advantage of Page Object Model is that if the UI
changes for any page, it don’t require us to change any tests, we just need to
change only the code within the page objects.
Below I have given example of POM
public PageName(WebDriver driver)
{
this.driver = driver;
//This initElements method will create all WebElements
//PageFactory.initElements(driver, this);
PageFactory.initElements(new AjaxElementLocatorFactory(driver,
Constants.WebDriverWaitDuration), this);
}
/*
* All WebElements are identified by @FindBy annotation
* @FindBy can accept tagName, partialLinkText, name, linkText, id, css, className, xpath as
* attributes.
*/
Say we have text box
@FindBy(Locator="LocatorValue")
public WebElement NameForElement;
//Method to enter input
public void EnterInput(String text)
{
NameForElement.clear();
NameForElement.sendKeys(text);
}
//If Element is button
public void ClickOnButton()
{
NameForElement.click();
}
//If Element is drop-down
public void SelectValueFromDropDown(String visibletext)
{
new Select(NameForElement).selectByVisibleText(visibletext);
}
Lets consider two different cases of writing code as
below, in case 1 there no use of POM
concept and in case 2 I have used POM
Without POM
driver.findElement(By.id("username")).sendKeys("testuser");
driver.findElement(By.id("password")).sendKeys("testuser");
driver.findElement(By.id("signIn")).click();
Case 2
With POM
Say we are working with login and elements resides in loginpage
Code will look like below –
loginpage. EnterUserName(“testuser”);
loginpage. EnterPassword(“testuser”);
loginpage. ClickOnLoginButton();
So suppose I have used login code in 10 classes as per
case 1, and if there is change in locator value. In that case I have to make
changes in all 10 classes but if code is written as per case 2, I have to make
changes at one place only where all elements are resides.
- Code is more readable and robust.
- Avoid duplication of code.
- Improves the maintainability of tests (Useful in Agile methodology based projects).
- Simple and clear tests.
- Good support of tests, because everything is stored in one place.
- The UI changes, the fix need only be applied in one place.
- Code re-use: Able to use the same page object in a variety of tests cases.
Far and away the best prize that life offers is the chance to work hard at work worth doing.
ReplyDelete_________________________
This is football game,you can try to play for free:Cheap FIFA 16 Coins and FIFA 16 Coins