Example code:
Driver is being passed as an Argument so that Selenium is able to locate the element on the browser (driver).
Element is returned, so that an Action can be performed on it.
Method is declared as Public Static, so that it can be called in any other method without instantiate the class.
Follow the same rule for creating LogIn Page class.
So each and every single fragment, using the POM, always gets a WebDriver
reference? Each and every fragment might then invoke
PageFactory.initElements(driver, this)
?
Just trying narrow down an overly broad, sprawling, question.
If each POM, each fragment, is as:
package pageObjects;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class LogIn_Page {
private static WebElement element = null;
public static WebElement txtbx_UserName(WebDriver driver){
element = driver.findElement(By.id("log"));
return element;
}
public static WebElement txtbx_Password(WebDriver driver){
element = driver.findElement(By.id("pwd"));
return element;
}
public static WebElement btn_LogIn(WebDriver driver){
element = driver.findElement(By.id("login"));
return element;
}
}
then it's all static
all the way down, is it not? How can this be refactored so that static
isn't relied on so extensively, the POM
is followed, and, as a bonus, PageFactory
is employed?
Here's a sample using PageFactory
:
public LoginPage(WebDriver driver) {
PageFactory.initElements(driver, this);
}
@FindBy(how = How.ID, using = "username")
private WebElement userName;
@FindBy(how = How.ID, using = "password")
private WebElement password;
@FindBy(how = How.ID, using = "login-btn")
private WebElement login;
public void logIn(String userName, String password) {
this.userName.sendKeys(userName);
this.password.sendKeys(password);
this.login.click();
}
Let's say that this LoginPage
wanted to create a page fragment, using the PageFactory
which the POM
utilized, how would it do so?
PageFactory
usage and thePOM
. I'm not literally asking someone to come along and fix my code. Nor figuratively.