Rayrun
← Back to Discord Forum

Best targeting practice : role, text or test id ?

Hello everyone,

Is there a common knowledge about which is the best targeting method between those 3 ? If you have some examples from your experience, that would be cool 🙂

Thanks a lot.

This thread is trying to answer question "No question was asked as the thread is empty."

6 replies
.aleksandaraleksandrov
.aleksandaraleksandrov

If your project has test-ids, you should surely use those.

So far, no test-id at all, but I was wondering if it would be a good idea to add them in order to target them.

.aleksandaraleksandrov
.aleksandaraleksandrov

Yes, this is the best practice to follow, but if it can't be done, move to CSS Selectors(whatever is availabe, id, class, role, etc)

refactoreric

Playwright some time ago introduced test-library inspired locators which are using accessibility roles. I think that is nowadays the best practice from Playwright's perspective. https://playwright.dev/docs/best-practices#prefer-user-facing-attributes-to-xpath-or-css-selectors

But what works best in your situation is a case of YMMV (your mileage may vary). Every application is different.

getByRole is by far the best way to grab elements if your ui is accessible

Where I currently work we habe set some principles

  • use testing library methods for all interactive elements
  • use test ids to narrow the scope of the dom on non interactive elements such as parent container or repeatable sections like cards

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.