You are here

You are here

3 steps to create a maintainable, browser-based test framework

Viktoriia Kuznetcova Performance Engineer and Test Automation Engineer, Breakpoint

Browser-based automated tests are normally a pain to maintain. There are lots of moving parts in complex web applications, forever breaking xpath locators, and long test execution times for business workflows with multiple steps. Those are just a few of the challenges.

But they don't have to be show-stoppers. The less code you have, and the more readable it is, the easier it will be to maintain in the long run.

Here are three ways to deliver a more maintainable browser-based test framework.

1. Split test logic and page manipulations into separate classes

You can find plenty of information about this online if you search for the PageObject model, but it essentially means that in one class you have all the methods you need to get the state of the page, input information, push a button, open or close popup messages, and so on.

Then, in your test classes, instead of interacting with a page directly, you use those classes as page manipulation proxies.

In this way, when the structure of a page changes and your CSS or xpath locators are broken, when extra formatting rules are applied to an input field, or when the type of a dropdown breaks an old way of selecting a value in it, you update just one class, and all your tests dealing with that page will pick up the change automatically.

2. Handle errors differently based on severity and purpose of the test

Depending on the system under test, you might want to minimize the time it takes to run the tests by having end-to-end tests that check several conditions along the way. In that case, using Spock assertions is not great, because a test will stop on the very first broken assertion.

To avoid that, split the possible issues into two categories: issues that make further test execution meaningless, and issues you need to note but that don't stop you from going on with the test and gathering as much information about the system as possible.

Handle the first category using assertions. Handle the second category in the "when" blocks and output the results manually in the "cleanup" block.

3. Make your tests smart enough to adjust to test data

In a complex, data-driven application, especially if you can't control the data—​it might be coming from independent back-end systems, for example—​write test code that's smart and flexible enough to adjust to the data.

For example, if you're depending on some value in the beginning of the workflow that the structure of the page changes, make sure that your test code takes that into account.

This allows you to write one piece of code for a lot of test datasets, and reduces the likelihood that your test will break because the data on one of the back ends changed. Data-driven test frameworks allow you to minimize code duplication and improve maintainability.

Less code, faster tests, better results

Follow the three steps above and you will significantly reduce the number of lines of code you have, provide a structure to support your tests, and shorten the time it takes to run a test suite.

Watch my talk, "Writing Maintainable Test code with Selenium, Spock and a few sparkles" from Automation Guild 2020, which took place in February, to learn more about these and other ways to improve the maintainability of your test framework. Registration entitles you to watch my presentation as well as all of the sessions at this year's event.

Keep learning