Micro Focus is now part of OpenText. Learn more >

You are here

You are here

5 ways to create faster, more reliable UI tests

public://pictures/biopic.jpeg
Satyajit Malugu Senior SDET, SoFi
 

Over the years and at many different companies, I've often witnessed the bad user-interface (UI) automation practice of making an entire automation flow, click, and assert as an end user would do it.

At first that doesn't sound so bad: You're just creating code that drives the UI that would otherwise have to be done manually. But while this simple black-box strategy might work when you have three or four test cases, it doesn't scale when your test automation needs to provide reliable feedback over a period of time, and with hundreds of test runs. 

There's a better way. A good automation suite provides consistent, reliable feedback to stakeholders, and you should use all of the test hooks at your disposal to attain that goal.

With that caveat, here are the five things you can do to look beneath the black box in order to create faster, more reliable UI tests.

Use the API layer

APIs are the backbone of modern web and mobile applications. Many good architectures process complex business logic in the API, with UI clients as the rendering engine, and your test automation can do the same.

From test data setup to feature flag manipulation, cleanup and verification logic, you can do everything through the API. Better yet, if you co-locate your automation code with your app code, you can use the same data objects and helpers directly in your test code. 

Take advantage of navigation shortcuts

While a real user might have to type url->hit sign in-> login -> click on profile ->edit profile, your UI test doesn't. It's perfectly fine to directly navigate to the http://example.com/profile/edit screen and start your test. On iOS and Android, you have deep links you can use to do the navigation, and on Android you can open activities and intents directly. 

In some cases you might need to do some setup to have a page render, such as setting a cookie or other data. Fortunately, you can do all of this through code. 

Manipulate the cookies

Modern websites rely heavily on cookies to provide an integrated experience and to stay on top of what is essentially a stateless HTTP protocol. There are many decisions in UI rendering, such as whether or not the user is logged in or has already seen a page, or whether all shopping-cart items are maintained in the cookies. A clever automation engineer can use those same hooks to manipulate the state of the UI through cookies. 

Manipulate the database directly

Databases are used with a desktop browser or Android or iOS clients to provide persistence in much the same way that you can use cookie databases to store more complex structures and data, such as a list of all the product pages a visitor viewed on an e-commerce site or all the conversations users had in a messaging app. 

Now you can use the same database to provide the state you desire in your tests. You can create test scripts that set up the database in a desired state and clean it off after every run so you can create cleaner tests. 

Feature flag manipulation

Modern websites such as Facebook, Amazon, and Bing don't provide the same experience to everyone: They are constantly experimenting and testing what works better through A/B testing. Sometimes, they only release features to a limited set of customers before rolling them out completely, and they use a mechanism called feature flagging to control those tests.

It would be hard to create a login screen automation, for example, if you didn't know what variation of the experiment was going to render when you requested the login page. Either through network calls or manipulating the cookie database, you can create hooks that force the page to be in a certain experiment. You can create tests that go through various experiments with these hooks and validate all of them. 

Five principles for faster, more reliable tests

So there you have it: five key principles you can follow to ensure that your tests run faster and more reliably. Just keep these two underlying software engineering principles in mind when using these techniques: KISS (Keep It Simple, Stupid) and DRY (Do not Repeat Yourself).

You can create a UI test to do one thing right. But if you have hundreds of tests all trying to log in, navigate to a page, and perform a simple action, your tests will be repeating themselves. 

Finally, make sure you have some pure UI tests so that you touch the top of the pyramid and build confidence in your stakeholders. 

For more on building better UI tests with hands-on code, come to my presentation at the Automation Guild online conference. Registration is just $197 and you can view recordings of all presentations after the live events. The conference takes place February 3-4.

Keep learning

Read more articles about: App Dev & TestingTesting