This post is part of the Pride & Paradev series.
When writing automated acceptance tests for a web application, there are a couple of different ways to identify and interacting with elements, two of the most common are using strings or values.
Take this simple select list of tea:
<html> <select id="tea"> <option value="bop">Broken Orange Pekoe</option> <option value="eg">Earl Grey</option> <option value="eb">English Breakfast</option> <option value="darl">Darjeeling</option> </select> </html>
This renders as:
There are two ways to automate choosing something from this select list: by string, like “Broken Orange Pekoe” or by value, like ‘bop’.
Use strings to interact with elements
The benefit of using strings to identify and interact with elements is that it’s how a user uses your web application: a user will select “Darjeeling” from your drop down so why shouldn’t your automated tests do the same.
If your web application is internationalized and you wish to run your automated tests in a different locale, you will also need to translate your selection as this will change with your locale that you set.
Use values to interact with elements
The benefit of using values to interact with elements is that values are the most resilient to change. For example, if “Earl Grey” was changed to “Earl Gray” and you are automating based upon value, then your automated acceptance tests will continue to work as they will use the value ‘eg’.
This is also the best option if your web application is internationalized and you run your automated acceptance tests against a different locale: whilst “English Breakfast” may display “Petit Déjeuner Anglais” in French, it’ll still happily be selected by using the value ‘eb’.