Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test IDs for Test Automation in HTML Markup?

We use automated tests to verify functionality of our web application. In order to make assertions in test cases less complicated and more flexible, we are considering the introduction of 'TestIDs', i.e. IDs in the HTML markup that help testcases find and verify elements on the page. Additionally, these TestIDs would allow more specific integration tests which are currently impossible due to the limited data on the pages.

However, here is what makes us hesitate:

  • introducing test IDs means changing the tested for testing
  • security - we would disclose internal domain object IDs and other information that would otherwise not be visible on the page
  • standards - depending on how we put the TestIDs into the markup we would most likely violate the intended semantic use of an element or attribute (e.g. 'id' or 'class' attributes, other html elements, etc.)
  • interference - TestIDs could interfere with application code
  • performance - TestIDs are unneccessary markup (to the user) and increase page size (only significant on large pages)

Limiting TestIDs to test/staging HTML doesn't seem to be a good idea because we obviously want to test code that will be used in production and don't want our test/staging environment to behave differently. In fact, we currently run parts of our test suites against the live system after a release.

Do you think TestIDs are a good idea and if so how would you put them into the markup?


Some sample markup to demonstrate what I'm talking about:

<!-- this test id allows an integration test to verify that
  the carrot 188271 is in fact green but exposes the id to the user -->
<tr id="testid-carrot-id-188271">
    <td class="color">green</td>
    <td class="size">doesn't matter</td>
</tr>
like image 325
Josef Pfleger Avatar asked Dec 18 '22 07:12

Josef Pfleger


1 Answers

I would never leave code intended for testing in a live site. That's just bad principle and inviting hacking.

As long as your test IDs are formatted such that they're not colliding with other IDs on the page (IDs need to be unique) and aren't being referenced by any of the live code (if you can't determine that there's something larger wrong here) then there shouldn't be any difference in behavior between the test site with the IDs and the live site without them.

In my opinion, the best practice is to design such that your test code executes correctly on your development site and you know that removing it doesn't harm your live site. I'd be concerned if my site needed to have the live version tested regularly to make sure it's working right.

like image 196
Gabriel Hurley Avatar answered Dec 19 '22 21:12

Gabriel Hurley