Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit-test Page Objects in a Selenium framework with PowerMock

I'm working an automation framework built on Selenium 2 and based on the Page Object design pattern. I am at the point where I want to start thinking about writing test suites for my code. Due to various reasons, some of them having to do with efficiency and others having to do with my lack of ownership and control over the test environment where the web application this framework is supposed to test is installed, I wanted to avoid having to start a browser and use the SUT to verify my framework code. So, I thought that Mock objects would be decent alternative.

The problem is that I cannot really wrap my head around the idea of mock objects and I really couldn't find a decent concrete example on the internet that illustrated how this would actually work. I did find one link that appeared to be promising, but the examples were really just way too abstract to actually be useful to me.

http://www.methodsandtools.com/archive/testingcodetdd.php

So, I thought I would post my simple LoginPage page object and ask for a simple example for a unit test or two for this page object using PowerMock. Here is the source code for my LoginPage object:

public final class LoginPage extends Page<LoginPage> {

    @FindBy(how = How.ID, using = "username")
    private WebElement usernameBox;

    @FindBy(how = How.ID, using = "password")
    private WebElement passwordBox;

    public LoginPage(final WebDriver driver) {
        this(driver, driver.getCurrentUrl(), DEFAULT_TIMEOUT_IN_SECONDS);
    }

    public LoginPage(final WebDriver driver, final String url) {
        super(driver, url, DEFAULT_TIMEOUT_IN_SECONDS);
    }

    public LoginPage(final WebDriver driver, final String url, final int   
        timeoutInSeconds) {
        super(driver, url, timeoutInSeconds);
    }

    public final void enterUsername(final String username) {
        usernameBox.clear();
        usernameBox.sendKeys(username);
    }

    public final void enterPassword(final String password) {
        passwordBox.clear();
        passwordBox.sendKeys(password);
    }

    public final void clickLoginButton() {
        loginButton.click();
    }

    public final HomePage loginWithGoodCredentials(final User user) {
        return login(user, HomePage.class);
}

    public final LoginPage loginWithBadCredentials(final User user) {
        return login(user, LoginPage.class);
    }

    private <T extends Page<T>> T login(final User user, final Class<T> 
        expectedPage) {

        user.getUsername(), user.getPassword(), user.getType(), expectedPage);
        enterUsername(user.getUsername());
        enterPassword(user.getPassword());
        loginButton.click();

        return Page.constructPage(getDriver(), getTimeoutInSeconds(), 
            expectedPage);
    }

}

I understand that mocking WebDriver and WebElement is easy because they are interfaces, according to the link I posted above. But the document I referenced doesn't make it very clear to a total newbie to Mock objects and Mocking frameworks how exactly, I use that to write a unit test for my page object. Let's take the public login methods, for example. What does a unit test for those look like exactly? I would merely need to verify that logging in with returns a page object of the expected type. Or for example, the methods which enter text into the username and password boxes... I would perhaps want to have a test that verifies that any existing text is erased before a username and password are entered. Since I wouldn't have a real browser with the real application login page loaded, I'm not exactly sure how PowerMock would instantiate and initialize all my web elements in order to do a test on the page object's publicly exposed services.

like image 526
Selena Avatar asked May 22 '13 13:05

Selena


People also ask

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.

How do I use PageFactory initElements?

Using the initElements method, one can initialize all the web elements located by @FindBy annotation. lazy initialization: AjaxElementLocatorFactory is a lazy load concept in Page Factory. This is used to identify web elements only when they are used in any operation or activity.

Can unit testing be done with Selenium?

Unit Testing can be performed in both manual and by automation. Unit Testing Frameworks can be used in Selenium to automate/support/perform any of the below: Controls the flow of test case execution. Grouping the test cases in to separate groups is possible.

Can Selenium interact with hidden elements?

We can interact with hidden elements in Selenium Webdriver. The hidden elements are the ones that are present in the DOM but not visible on the page. Mostly the hidden elements are defined by the CSS property style="display:none;".


1 Answers

Maybe http://popperfw.org is interesting for you. It is a much more flexible implementation of the page object pattern than the default implementation from selenium and it comes with direct support for "unit" testing your created PageObjects => it tests if your PageObjects match on your testet pages

http://popperfw.org/unitTest.html

like image 104
mibutec Avatar answered Oct 11 '22 14:10

mibutec