Page
Object Model | POM
Creating Selenium test cases can result in an
unmaintainable project. One of the reasons is that too many duplicated code is
used. Duplicated code could be caused by duplicated functionality and this will
result in duplicated usage of locators. The disadvantage of duplicated code is
that the project is less maintainable. If some locator will change, you have to
walk through the whole test code to adjust locators where necessary. By using
the page object model we can make non-brittle test code and reduce or eliminate
duplicate test code. Beside of that it improves the readability and allows us
to create interactive documentation. Last but not least, we can create tests
with less keystroke. An implementation of the page object model can be achieved
by separating the abstraction of the test object and the test scripts.
·
Count and print no.
of links present on webpage using Page Object Model
1.
In order to initialize the elements
which are declare using @FindBy. We use initElements method of Page Factory
Class
import java.util.List;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.support.FindBy;
import
org.openqa.selenium.support.PageFactory;
public class Flipkartpage
{
@FindBy(xpath="//a")
private List<WebElement> allLinks;
public Flipkartpage (WebDriver driver)
{
PageFactory.initElements(driver,this);
}
public void PrintNoofLinks()
{
//To count no. of links present on webpage
System.out.println(allLinks.size());
for (WebElement pagelink : allLinks)
{
String linktext = pagelink.getText();
String link = pagelink.getAttribute("href");
System.out.println(linktext+" ->");
System.out.println(link);
}
}
}
2. Now create the class of Main method
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.firefox.FirefoxDriver;
public class POM
{
public static void main(String[] args)
{
WebDriver driver=new FirefoxDriver();
driver.get("Page Url");
Flipkartpage f=new Flipkartpage(driver);
f.PrintNoofLinks();
driver.close();
}
}
selenium Online Training Hyderabad
ReplyDeleteGood one.it was informative.Thank you..keep update best selenium training in chennai
ReplyDeletebest selenium training institute in chennai
Training with placement in chennai