PageFactory Class

 1. What is page factory class?

  • PageFactory class is part of the selenium support package
  • PageFactory class is used to make Page Objects simpler and easier.

2. What is the FindBy annotation interface?

  • FindBy annotation provides a mechanism to locate elements or a list of elements
  • Simply it replaces driver.findElement(locator) code block.
  • It provides an easy way to create page objects

3. Provide an example of how to use @FindBy annotation

  • @FindBy(id="row") WebElement element;
  • @FindBy(how = How.ID , using = "row") WebElement element;
  • @FindBy(tagName = "a") Link<WebElement> links;
  • @FindBy(className = ".input.username");

4. What are the parameters taken by @FindBy annotation?

  1. className
  2. css
  3. id
  4. tagName
  5. linkText
  6. partialLinkText
  7. name
  8. how
  9. using
  10. xpath

5. What does initELements() method does in PageFactory class?

  • It instantiates an instance of the given class 

6. How to use the PageFactory class for creating page objects?

  • We can  use page factory pattern to initialize web elements which are defined in page objects
  • We initialize page objects using initElements() method from PageFactory class
  • Once we call initElements() method all elements will get initialized.
  • initElements() is a static method
  • It will take driver instance and class type as arguments
  • It will return the page object with its fields fully initialized
Method 1
Home home = new Home(driver);
PageFactory.initElements(driver, home)

Method 2

public Home(WebDriver driver){

this.driver = driver;
PageFactory.initElements(driver, this);
}

7.  What is the use of annotations in the Page Factory class?

  • Annotations are used to give descriptive names for Web Elements to improve code readability
  • @FindBy annotation is used to identify web elements on the page
  • PageFactory will search for elements on the page with matching attribute  id attribute
  • If that fails it will search by name attribute
  • Then if we need more control over identifying elements in the HTML page and mapping them with our page object fields
  • So one way of doing this is @FindBy annotation

8. What are the annotations interfaces used in the PageFactory class?

  • @FindBy 
  • @CacheLookup

9. When do we need to use @CacheLookup annotation?

  • Everytime when a method is called on a web element the driver will find it on the current page and simulate the action on the web element
  • There are cases where we will be working on the basic page and we know that we will find the element on the page every time we look for it 
  • We use @CacheLookup annotation to indicate that it never changed
  • That means that the same instance in the DOM will always be used
  • It is used to instruct the initElements() method to cache the element once it's located and that it will not be searched over and over again
  • This is useful when the elements that are always going to be there
  • Otherwise, whenever we use a web element the web driver will go and search for it again

10. Where exactly do we add @CacheLookup annotation?

@FindBy(name = "user")
@CacheLookup
private WebElement element;

11. Write code to show the difference between POM and PageFactory class

Using POM

private By username = By.className("tow");

private By password = By.id("row");
private By submit =. By.name("submit");

public void verifyLogin(String username , String password){

driver.findElement(username).sendKeys("name");
driver.findElement(password).sendKeys("paa");
driver.findElement(submit).click();

}

Using PageFactory class

public class Home{

@FindBy(id="row") private WebElement user;
@FindBy(className ="tow") private WebLement password ;
@FindBy(name="rop") private WebElement submit;

public void userLogin(String username, String password){

user.sendKeys(username);
password.sendKeys(password);
submit.click();
}

}


No comments:

Post a Comment

Cucumber

What is Cucumber? Definition : Cucumber is an open-source tool that supports BDD . Purpose : Bridges the communication gap between business ...