Automated Testing and the Semantic Web
Good automated web tests need semantic web pages to be readable and maintainable.
Join the DZone community and get the full member experience.
Join For FreeWhen writing tests against web applications, developers will quickly find that they need to define some standard way of identifying elements located on the page. The reason is obvious: for a test script to do anything, it needs to know what to do it against. What isn’t obvious is how you identify elements, because HTML provides so many different ways to reference elements.
Let’s run through some of the various options available to developers to provide an answer to a form field that is asking for the user’s age.
Selection by ID
<input type=”text” name=”age” id=”age”>
The ID attribute is perhaps the first and most obvious way to identify elements in a web page. They are (supposed to be) unique, so you know exactly which element you are targeting.
The problem is that the value assigned to the ID attribute is often generated at runtime. While our “age” ID above is reasonably unambiguous in a small web application, in reality, you’ll see a lot of IDs like this in modern web applications:
<input type=”text” name=”age” id=”cust_10267_details_section_age_field”>
To the developer that wrote the code that generated this ID, this makes perfect sense. To everyone else, this ID is gibberish. Targeting this ID leads to rules like this in a test case:
And I populate the field with the ID of “cust_10267_details_section_age_field” with “21”
It only takes a few lines in a test script that read like this before the test script is unreadable.
Selection by XPath
XPaths are even more unreadable than IDs when it comes to selecting elements on a page:
And I populate the field with the xpath of “/html/body/div/div[2]/div[1]/div[1]/input” with “21”
Not even the developers of the website could read that xpath and tell you what it was referring to. And worse yet, a single change in the structure of the page would break this test case.
XPaths are fragile and unintelligible, which makes them a very poor choice for identifying elements.
Selection by Class
While classes aren’t as likely as IDs to be generated as long and obtuse strings, they are also not typically very good at uniquely identifying elements. You will very likely see classes shared between multiple elements on the page, and the class names won’t be much help in determining what the purpose of the elements are since most of the time they will be used to apply CSS rules. In this example, all form elements would share the same class:
<input type=”text” name=”age” class=”forminput”>
Depending on your naming convention, these class names may end up being something resembling an XPath anyway. The BEM standard leads to class names like “block block--size-big block--shadow-yes”, which has benefits from a styling point of view, but would lead to test rules like this:
And I populate the field with the class of “block block--size-big block--shadow-yes” with “21”
This is as unintelligible as an XPath selection.
Selection by Data Attribute
What we really need is the ability to target elements by their meaning rather than how they look (CSS), where they appear (XPath), or by some developer specific index (ID). What we need some some semantic context.
The data-* attributes come to the rescue here. These attributes provide a way to assign meaning to elements while still generating valid HTML. Using these attributes we can identify elements based on what they mean rather than how they are displayed.
<input
type=”text”
name=”age”
id=”cust_10267_details_section_age_field”
class=”block block--size-big block--shadow-yes”
data-user-input-field=”age”>
Here we have used a custom data attribute of data-user-input-field and assigned it the value of age. We can then interact with this element using a rule like this:
And I populate the user input field asking for “age” with the value “21”
This is much better.
Meeting Testers Halfway
While test rules like And I populate the user input field asking for “age” with the value “21” still sounds robotic, the reality is that most people can read this and understand what it is doing.
Languages like Gherkin (used by the popular Cucumber library) provide a lot of flexibility when it comes to generating a syntax that reads like natural language, but it is quite difficult to build a test case that reads like fluent, natural language. The reality is that people will be better at reading even these robotic statements than a computer will be at generating natural language.
By making the effort to build semantically rich web pages, you are meeting non-technical users halfway by providing the ability to write test cases that can be understood without any particular understanding of the underlying code that the application being tested was written with.
Published at DZone with permission of Matthew Casperson, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments