-2

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?

2
  • 1
    Please check guidelines for How to create a Minimal, Reproducible Example. Commented May 1, 2024 at 7:37
  • Please provide enough code so others can better understand or reproduce the problem. Commented May 1, 2024 at 10:13

1 Answer 1

0

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.