In our engaging Ask Me Anything webinar with @utchbe, Susan Lee raised an interesting question regarding transitioning to Playwright:
“If a product already has a solid implementation of test-ids, as in our case where we’re considering moving from Cypress to Playwright, would you recommend continuing with test-id usage or switching to getByRole for most things?”
Similarly, L S asked:
"In Playwright, would you say it is better to use a lot of testIds or rather use locators like getByRole whenever possible? "
This question addresses a common concern about best practices in test automation, especially when shifting between frameworks. Your perspectives on leveraging existing test-id patterns versus adopting different querying methods in Playwright would be beneficial.
I’d say that’s each responsible automators’ choice.
getByRole
has the advantage of using accessible elements and not relying on customization;
forces the development of better HTML and accessibility;
the disadvantage is, your application might be crappy and you’ll have to rely on many ‘page.locators’ css/xpath;
using roles requires one to learn the w3 accessibility elements and then how to manipulate and chain them;
getById
if it’s already coded and there’s a deterministic no new knowledge required way of transitioning or using;
the disadvantage is, it avoids several weird coding patterns and behaviors maybe your app might have;
However I’m not convinced that fixing HTML and UI issues should be done while implementing automated checks. People can set aside time for that and focus their testing on it.
In theory it looks good, but at time get by Role and other user friendly locators creator trouble in automation.
There are some text which is sometimes repeated 3 times on a page at various places
some elements are hidden
so test id is the best in all such apps where these scenarios happen. For the current context i would not change the existing locators which are done with id as id will not make it flaky
but a small mistake in user friendly locator can make it troublesome to maintain if you have an app which is under development