Using the Page Object Model pattern in Selenium + TestNG tests
This post was published on December 30, 2014After having introduced the Selenium + TestNG combination in my previous post, I would like to show you how to apply the Page Object Model, an often used method for improving maintainability of Selenium tests, to this setup. To do so, we need to accomplish the following steps:
- Create Page Objects representing pages of a web application that we want to test
- Create methods for these Page Objects that represent actions we want to perform within the pages that they represent
- Create tests that perform these actions in the required order and performs checks that make up the test scenario
- Run the tests as TestNG tests and inspect the results
Creating Page Objects for our test application
For this purpose, again I use the ParaBank demo application that can be found here. I’ve narrowed the scope of my tests down to just three of the pages in this application: the login page, the home page (where you end up after a successful login) and an error page (where you land after a failed login attempt). As an example, this is the code for the login page:
package com.ontestautomation.seleniumtestngpom.pages; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; public class LoginPage { private WebDriver driver; public LoginPage(WebDriver driver) { this.driver = driver; if(!driver.getTitle().equals("ParaBank | Welcome | Online Banking")) { driver.get("http://parabank.parasoft.com"); } } public ErrorPage incorrectLogin(String username, String password) { driver.findElement(By.name("username")).sendKeys(username); driver.findElement(By.name("password")).sendKeys(password); driver.findElement(By.xpath("//input[@value='Log In']")).click(); return new ErrorPage(driver); } public HomePage correctLogin(String username, String password) { driver.findElement(By.name("username")).sendKeys(username); driver.findElement(By.name("password")).sendKeys(password); driver.findElement(By.xpath("//input[@value='Log In']")).click(); return new HomePage(driver); } }
It contains a constructor that returns a new instance of the LoginPage object as well as two methods that we can use in our tests: incorrectLogin, which sends us to the error page and correctLogin, which sends us to the home page. Likewise, I’ve constructed Page Objects for these two pages as well. A link to those implementations can be found at the end of this post.
Note that this code snippet isn’t optimized for maintainability – I used direct references to element properties rather than some sort of element-level abstraction, such as an Object Repository.
Creating methods that perform actions on the Page Objects
You’ve seen these for the login page in the code sample above. I’ve included similar methods for the other two pages. A good example can be seen in the implementation of the error page Page Object:
package com.ontestautomation.seleniumtestngpom.pages; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; public class ErrorPage { private WebDriver driver; public ErrorPage(WebDriver driver) { this.driver = driver; } public String getErrorText() { return driver.findElement(By.className("error")).getText(); } }
By implementing a getErrorText method to retrieve the error message that is displayed on the error page, we can call this method in our actual test script. It is considered good practice to separate the implementation of your Page Objects from the actual assertions that are performed in your test script (separation of responsibilities). If you need to perform additional checks, just add a method that returns the actual value displayed on the screen to the associated page object and add assertions to the scripts where this check needs to be performed.
Create tests that perform the required actions and execute the required checks
Now that we have created both the page objects and the methods that we want to use for the checks in our test scripts, it’s time to create these test scripts. This is again pretty straightforward, as this example shows (imports removed for brevity):
package com.ontestautomation.seleniumtestngpom.tests; public class TestNGPOM { WebDriver driver; @BeforeSuite public void setUp() { driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); } @Parameters({"username","incorrectpassword"}) @Test(description="Performs an unsuccessful login and checks the resulting error message") public void testLoginNOK(String username, String incorrectpassword) { LoginPage lp = new LoginPage(driver); ErrorPage ep = lp.incorrectLogin(username, incorrectpassword); Assert.assertEquals(ep.getErrorText(), "The username and password could not be verified."); } @AfterSuite public void tearDown() { driver.quit(); } }
Note the use of the page objects and the check being performed using methods in these page object implementations – in this case the getErrorText method in the error page page object.
As we have designed our tests as Selenium + TestNG tests, we also need to define a testng.xml file that defines which tests we need to run and what parameter values the parameterized testLoginOK test takes. Again, see my previous post for more details.
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="My first TestNG test suite" verbose="1" > <parameter name="username" value="john"/> <parameter name="password" value="demo"/> <test name="Login tests"> <packages> <package name="com.ontestautomation.seleniumtestngpom.tests" /> </packages> </test> </suite>
Run the tests as TestNG tests and inspect the results
Finally, we can run our tests again by right-clicking on the testng.xml file in the Package Explorer and selecting ‘Run As > TestNG Suite’. After test execution has finished, the test results will appear in the ‘Results of running suite’ tab in Eclipse. Again, please note that using meaningful names for tests and test suites in the testng.xml file make these results much easier to read and interpret.
An extended HTML report can be found in the test-output subdirectory of your project:
The Eclipse project I have used for the example described in this post, including a sample HTML report as generated by TestNG, can be downloaded here.
"