Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PageObjects, Page Factory and WebDriverWait in Selenium WebDriver using Java

I've been using Selenium WebDriver to implement functional tests for some projects that I've worked with. I'm trying to use the Page Object design pattern with Page Factory to factor out my locators. I've also created a static WaitTool object (singleton) that implements several waiting techniques with optional timeout parameters.

My current problem is that I would like to use my wait methods before the PageFactory attempts to initialise the WebElements. The reason I would like to wait is because the PageFactory may try to initialise the page elements before they are available on the page.

Here is a sample PageObject:

public class SignInPage extends PageBase {
    @FindBy(id = "username")
    @CacheLookup
    private WebElement usernameField;

    @FindBy(id = "password")
    @CacheLookup
    private WebElement passwordField;

    @FindBy(name = "submit")
    @CacheLookup
    private WebElement signInButton;

    public SignInPage(WebDriver driver) {
        super(driver);

        WaitTool.waitForPageToLoad(driver, this);

        // I'd like initialisation to occur here
    }

    public MainPage signInWithValidCredentials(String username, String password){
        return signIn(username, password, MainPage.class);
    }

    private <T>T signIn(String username, String password, Class<T> expectedPage) {
        usernameField.type(username);
        passwordField.type(password);
        signInButton.click();

        return PageFactory.initElements(driver, expectedPage);
    }
}

Here is a sample TestObject:

public class SignInTest extends TestBase {
    @Test
    public void SignInWithValidCredentialsTest() {
        SignInPage signInPage = PageFactory.initElements(driver, SignInPage.class);

        MainPage mainPage = signInPage.signInWithValidCredentials("sbrown", "sbrown");

        assertThat(mainPage.getTitle(), is(equalTo(driver.getTitle())));
    }
}

I tend to put my logic in the Page Object as much as possible (including waits), as it makes the test cases much more readable.

like image 808
Jamie Piltz Avatar asked Apr 24 '13 02:04

Jamie Piltz


People also ask

What is Page Factory in Selenium Java?

What is Page Factory in Selenium? Page Factory is a class provided by Selenium WebDriver to support Page Object Design patterns. In Page Factory, testers use @FindBy annotation. The initElements method is used to initialize web elements.

What is difference between POM and page Factory?

What is the difference between Page Object Model (POM) and Page Factory: Page Object is a class that represents a web page and hold the functionality and members. Page Factory is a way to initialize the web elements you want to interact with within the page object when you create an instance of it.

How do you test web pages and initialize using POM?

Steps To Create Test Cases:Initialize the driver and open the application. Create an object of the PageLayer Class(for each webpage) and pass the driver instance as a parameter. Using the object created, make a call to the methods in the PageLayer Class(for each webpage) in order to perform actions/verification.

What is page object model and page Factory in Selenium?

Page Object Model in Selenium WebDriver is an Object Repository design pattern. Selenium page object model creates our testing code maintainable, reusable. Page Factory is an optimized way to create object repository in Page Object Model framework concept.


1 Answers

The WebElements in a PageFactroy are really proxies to WebElements. This means that every time you access a WebElement it will perform a search to find the element on the page.

This has some advantages:

  • When the PageFactory is initialised the proxies are configured, but the WebElements are not found at that point (so you won't get a NoSuchElementException)
  • Every time you use a WebElement it will go and find it again so you shouldn't se StaleElementException's

You are using the @CacheLookup annotation which is losing you the second benefit as it will find the element once and then keep a reference to it, you are now far more likely to see StaleElementExceptions.

That being said you still keep the main benefit which is that Selenium will not go to the page and actually find the element until you first use it.

So in summary all you need to do is move

PageFactory.initElements(driver, this);

Into your constructor and it will all work fine.

like image 176
Ardesco Avatar answered Sep 19 '22 16:09

Ardesco