Reputation: 1
I have more than 500 web-elements in a webpage. If I use Page Object Model with PageFactory.initElements() in selenium it will initialize all the elements once the object is created.
Lets says I am using only specific web elements in a test case, lets say 30, I want only these web-elements to be initialized rather than initializing all the web elements in the page. Is there any solution for this?
I am guessing AjaxElementLocatorFactory(Lazy initialization) does that. Is that right?
Upvotes: -2
Views: 249
Reputation: 25531
No, that's not how PageFactory works. Elements are lazy loaded... they are fetched only and at the point when they are needed.
Having said that, the Selenium lead and contributors have stated not to use PageFactory for many reasons. You should instead use an actual page object. I've put a sample login page page object below.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class LoginPage {
WebDriver driver;
private final By usernameLocator = By.id("username");
private final By passwordLocator = By.id("password");
private final By submitButtonLocator = By.cssSelector("button[type='submit']");
public LoginPage(WebDriver driver) {
this.driver = driver;
}
public void login(String username, String password) {
driver.findElement(usernameLocator).sendKeys(username);
driver.findElement(passwordLocator).sendKeys(password);
driver.findElement(submitButtonLocator).click();
}
}
and then in your test, you call it like
LoginPage loginPage = new LoginPage(driver);
loginPage.login(username, password);
or more simply,
new LoginPage(driver).login(username, password);
Upvotes: 0