I am working on an automation infrastructure based 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. For various reasons, some of which are related to efficiency, and others due to my lack of ownership and control over the test environment in which the web application that is supposed to be tested with this framework is installed, I wanted to avoid starting the browser and use SUT to check code of my structure. So, I thought that Mock objects would be a worthy alternative.
The problem is that I can’t really think about the idea of fictitious objects and really can’t find a decent concrete example on the Internet that would illustrate how this will actually work. I found one link that seemed promising, but the examples were too abstract to be useful to me.
http://www.methodsandtools.com/archive/testingcodetdd.php
So, I decided to publish 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 of 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, as they are interfaces, according to the link I posted above. But the document I referenced is not entirely clear to a beginner in Mock objects and Mocking platforms, how exactly do I use this to write a unit test for my page object. Let's take public login methods, for example. What exactly does a unit test look like for them? I just need to make sure that logging in using returns a page object of the expected type. Or, for example, methods that enter text into the username and password fields ... Perhaps I would like to have a test that checks that any existing text is erased before entering the username and password. Since I would not have a real browser with a loaded page of a real application, I’m not quite sure how PowerMock will create and initialize all my web elements in order to run a test on the publicly provided services of the page object.