Find elements by their data attributes in Capybara

@ktec and I love Capybara for acceptance testing. We also love the `data-id` convention to identify database records in HTML attributes.

Semantically speaking the `id` attribute is for anchors in hyperlinks and presentation hooks for CSS. Capybara finders that use `id` or `class` of even the `tag` name are coupling the test suite to the presentation markup. Using the `data-*` attributes as finders in Capybara decouples content and presentation from the acceptance test suite.

Having a module to wrap this particular convention means that we don’t have to re-remember xpath syntax. Generalising this code to accept any data attribute makes this re-usable for other scenarios too.


It is easier to adopt conventions if they are easy to follow.

comments powered by Disqus

Contact

joe@joejames.io. You wont find me on social networks (because I'm not there).