the reluctant tester

Perpetual learner of the craft of Software Testing,Servant Leadership and creating better Teams

Robust Test design with Playwright – using .getbyRole()

One of Playwright api’s tenet for writing robust tests is..

“Prefer user facing/accessibility attributes for selecting elements vs writing CSS/XPath selectors”

This ,again, is a new mindset towards writing UI Automation code as you are less prone to DOM changes invalidating your selectors, in turn leading to less flaky tests .

The Playwright API facilitates this tenet by providing ( my new fav) method page.getByRole() .

getByRole() combines what the element looks like to an end user ( e.g. a button, a text box ) , with what it’s accessibility features are ( e.g. name) , and what you get is robust & intuitive to read/write code to select an element, like this –>

// get me a text box with this name

// click a button with this name for me please


// locate me an element with this CSS

I would encourage new Playwright practitioners to explore .getByRole() and see if they find this newer way to select element more intuitive & robust than purely using CSS/XPath ?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

About Me

I’m Sunjeet Khokhar

An experienced People Leader,Practice Lead  and Test Manager .

I am driven by the success of people around me, am a keen student of organisational behaviour and firmly believe that we can be better craftspeople by being better humans first.

CoNNECT with Me

%d bloggers like this: