DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Hidden Functions In Selenium 4
  • Spice Up Your 'CI/CD Process' With Automation Using Cucumber, Selenium, and Kotlin
  • Playwright: Test Automation You Must Know
  • 6 Easy Ways to Start Coding Accessibly

Trending

  • Edge Data Platforms, Real-Time Services, and Modern Data Trends
  • Beyond the Prompt: Unmasking Prompt Injections in Large Language Models
  • LLMs for Bad Content Detection: Pros and Cons
  • The Emergence of Cloud-Native Integration Patterns in Modern Enterprises
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Automated Testing and the Semantic Web

Automated Testing and the Semantic Web

Good automated web tests need semantic web pages to be readable and maintainable.

Matthew Casperson user avatar by
Matthew Casperson
·
Jun. 15, 16 · Tutorial
Like (5)
Save
Tweet
Share
12.57K Views

Join the DZone community and get the full member experience.

Join For Free

When 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.

Element Semantic Web Testing Semantics (computer science) Test script

Published at DZone with permission of Matthew Casperson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Hidden Functions In Selenium 4
  • Spice Up Your 'CI/CD Process' With Automation Using Cucumber, Selenium, and Kotlin
  • Playwright: Test Automation You Must Know
  • 6 Easy Ways to Start Coding Accessibly

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: