Thursday, December 26, 2019

Selenium Webdriver data driven page objects using java hash maps

There are many articles on the web about Webdriver frameworks that encourage a data driven approach to test case development. By separating the data from the executable code two benefits are realized, the first is that the test case can be reused in various scenarios: for example the ‘log on’ could be for an admin user or the chief purchasing officer by varying the name and password in the data parameters.

The second, not so obvious benefit, is that the data itself can be separately maintained and updated as required, this approach towards data maintenance is similar to making everything configurable in the application code itself.

For example if there was an implementation of a drop down option list that contained various shipping rates then by separating the list of rates and feeding them into the test cases, the list could be easily changed when the requirements changed.

One caveat to the above mentioned benefits of separating out data from the test case code is: Do not be tempted to validate from the same data source as what is used in the application.

This approach is tempting but the validation should be done from an independent source of data otherwise the application becomes self validating. That said it is valid to seed test data from values in the test database, just do not seed the verification data.

Hash maps are an ideal java structure for data driving your tests, whether you are using Webdriver or some other java based automation testing services framework.

To use hash maps you need the following package import:-

import java.util.HashMap;

The hash map can be thought of as a list of key and value pairs. Although this is a simplistic description, and I would encourage you to Google hash maps to find out more, the value of hash maps for data driving automated tests can be illustrated using this simple analogy.

The data type of both that Key and the Value can be defined when the hash map is created:-

HashMap wordcount = new HashMap();

Within an automated testing framework the hash map can be seeded (from a CSV file, DB or other data source) within the test runner or scenario you are driving the tests from.

Seeding the hash map from a data source is a matter of reading through the data source and loading the hash map. In this way the data file (or other source) is easily maintained should the requirements change.

There are many good examples of doing this, on the web. Just Google reading a CSV file into a hash map and you will get some useful articles.

Having seeded the hash map a page object can be called with the hash map as a parameter. The page object is a reusable function that is targeted at a given screen (page), for example the Log On page.

Within the page object, which contains all the Webdriver locators and code to set the values of the various elements, the hash map value pairs can be used to retrieve the key (data field) and the value that is to be set – e.g. (username, “Mike Smith) and (password, “mikes password”).

There are many examples on the web of how to retrieve specific Key values from the hash map, so I won’t repeat them but I will emphasize the additional benefits of using this construct.

If you want to make your page object flexible you could have a series of If statements, for each Key (data field) and If that Key exists (in the hash map) it is populated otherwise that field is left empty. In this way a specific number of fields (with their unique values) can be set in the page object, which could then be used in multiple scenarios.

In the simple Log on example you could just send the username to the Page object and the password could be omitted.

 If you did this then, to test the validation, that the system returns password required should be done outside of the Log On page object as the page object does not validate (as it only repeats the required steps). See the page object framework for more details of this approach.

Although this Blog post has only just touched on the use of hash maps for passing data variables into selenium Webdriver test cases (which could be organized as page objects), I hope I have conveyed the overall flexibility and advantages of using this versatile construct.

No comments:

Post a Comment