TestingDocs.com logoTestingDocs.com
Automation learning lab
TestingDocs • Self-paced course

Playwright Automation
Self Study Guide

Start from zero. Learn JavaScript and TypeScript, automate browsers, and build reliable end-to-end tests.

60 chaptersBeginner friendlyHands-on practice
Playwright / Chapter 38 of 60~18 min • Beginner
Chapter 38Playwright

Role, text, label and test-id locators

getByRole uses accessibility roles and accessible names, getByLabel finds form controls by labels, and getByText finds text. getByTestId uses an explicit test-id contract. Choose locators that express what a user sees or a stable agreed identifier.

How it works

Step-by-step walkthrough

  1. Create labeled controls
  2. Locate by label
  3. Assert a button is visible

Example

TypeScript
import { test, expect } from '@playwright/test';

test('chapter 38 practice', async ({ page }) => {
  await page.setContent('<label>Email <input></label><button>Continue</button>');
  await page.getByLabel('Email').fill('qa@example.com');
  await expect(page.getByRole('button', { name: 'Continue' })).toBeVisible();
});
Expected result: Email field filled; button visible
💡 Point to remember
Make locators unique instead of using first() to hide ambiguity.

Practice application screenshot

Actual screenshot of the TestingDocs task manager practice application
TestingDocs practice page, captured in Chromium.

✎ Your practice task

Add data-testid="continue" and locate it by test id.

Knowledge check

Which locator targets a labeled input?

🌐 Continue learning