Race condition testing with Selenium

Selenium is a good tool for automating at the UI, but it also has a number of other usages that often go overlooked. Looking for Race Conditions is one of these usages.

Using a tool like Selenium IDE allows you to enter data, click buttons and navigate the site at speeds a human utilising the browser wouldn’t be able to do. Sometimes going too fast through a process or application can change the order events/messages should happen, or trigger results at stages they shouldn’t be triggering. These are race conditions.

A race condition or race hazard is a flaw in an electronic system or process whereby the output or result of the process is unexpectedly and critically dependent on the sequence or timing of other events. The term originates with the idea of two signals racing each other to influence the output first. – Wikipedia - http://en.wikipedia.org/wiki/Race_condition

It’s often easy to find race conditions using a Selenium UI test. I’ve used Selenium to recreate issues at a pace I’ve not been able to manage through the UI myself.

How to test for race conditions
Use a Selenium script to play back a sequence of activities at a faster pace than normal.

For example, if you are completing a form and when you choose “yes" to question 3 a further two mandatory questions become available at the end of a form - then you have a good potential for race conditions. The questions may be shown after a quick page re-load (there are other ways of implementing this though).

If you work through the form at your own pace and provide a “yes" to question 3 then you see the further questions revealed, and are not able to continue because the two further questions are mandatory. The form validation fires and you receive a warning message to complete the two further questions.

However, if you record the form entry and play it back much faster using Selenium, you may be able to provide the answer “yes" and click continue before the page load happens.

This may mean you are able to submit a form that technically is invalid (you didn’t answer the two further compulsory questions).

I found a bug like this very one in a real system a few years back. It was not reproducible by me clicking all of the time, but Selenium would catch it every time.

There is always an interesting debate regarding whether an end-user would see these types of issues as they aren’t working at the same speeds, but sometimes these Race Conditions point to underlying code issues or process flow problems. They are worth starting a discussion about at least.

Each team will treat these sorts of bugs differently. Some automation frameworks use the concept of “think time" to emulate real user response times. This can be effective for on-going regular automation, but if you’re specifically looking for race conditions then fast automation through the UI can help you.

Some users work faster than others so there always needs to be some judgment applied, but race conditions can occur at any speed, it just so happens Selenium could help you find more of them.

Useful Hint
Mastering the IDE test controls for Selenium will mean you can move quickly to edit and run tests.

Useful Links
Race Conditions (Wikipedia) - http://en.wikipedia.org/wiki/Race_condition

Selenium - http://seleniumhq.org/



If you’re interested in a career in Software Testing then check out my book Remaining Relevant And Employable (Tester’s Edition) - it’s packed full of ideas about writing good CVs, communicating your value to employers and doing well in an interview.