How to test page objects within Selenium using PowerMock

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.

+5
source share
2 answers

Selenium tests are not a suitable place for Mock objects. Layout objects are used when UnitTesting classes have dependencies. You mock dependencies to focus on testing a single class.

Selenium is used to check a webpage / browser based application. By the very nature of this web page, it is indicated that there is some kind of server and a running browser (automated in the case of Selenium). This is, by definition, an integration test, as you are testing the final “integrated” product. If there is a problem, it could be within ClassA or possibly a web server configuration. Integration testing is a place where you can find where this material breaks down (this is often used to reduce the load on the QA team for regression tests, because the testing functionality of test classes and integration tests tests the functionality of the system as a whole (or, possibly, in small integrated parts).

With all that said, it looks like you are mixing them. From your published code, I would say that you are doing integration testing, and you should just forget about Mock objects.

0
source

You may be interested in http://popperfw.org . This is a much more flexible implementation of the page object template than the default implementation from selenium, and it comes with direct support for the “unit” of testing your created PageObjects => object if your PageObjects match your test page pages

http://popperfw.org/unitTest.html

0
source

Source: https://habr.com/ru/post/1482170/


All Articles