QA Graphic

CSS Selector Reference Sheet in PlayWright

PlayWright with Typescript reference guide

When working with Playwright and TypeScript, having a concise reference guide for selectors and locators can significantly improve productivity. This cheat sheet highlights commonly used locator strategies and actions in Playwright - ideal for anyone writing end-to-end tests or automating modern web applications.

Below is a handy visual cheat sheet that you can download and keep nearby during your testing work.

Playwright Locator Cheat Sheet

Highlights from the Cheat Sheet

Here's a breakdown of some key locator patterns and actions:


// CSS Selectors
page.locator('#id');
page.locator('.class');
page.locator('tagname');
page.locator('[data-test="button"]');
// By Text
page.locator('text=Submit');
page.locator('text=/Submit/i');
page.getByText('Submit');
// By Role (Recommended for accessibility)
page.getByRole('button', { name: 'Submit' });
page.getByRole('textbox', { name: 'Username' });
page.getByRole('checkbox', { name: 'Remember me' });
page.getByRole('link', { name: 'Learn More' });
// By Test ID
page.getByTestId('submit-button');
// Filtering Locators
page.locator('div.card').getByRole('button', { name: 'Add to Cart' });
page.locator('.item').first();
page.locator('.item').last();
page.locator('.item').nth(2);
// Filter by text content
page.locator('.product-item').filter({ hasText: 'Limited Edition' });
// Filter by child element
page.locator('.product-card').filter({ has: page.locator('.discount-badge') });
// Actions on Locators
await page.locator('#element').click();
await page.locator('#element').fill('value');
await page.locator('#element').scrollIntoViewIfNeeded();
        

This cheat sheet is great for both beginners and experienced QA engineers who want quick access to essential Playwright syntax. It covers a variety of selector strategies from basic CSS to accessibility-driven roles and filtering techniques.

Feel free to print it out, save it, or bookmark this post for future reference.

 

Comments

Add Your Comments

Name:
Comment:

 

About

Welcome to Playwright Tips and Tricks, your go-to resource for mastering the art of web automation and testing with Playwright! Whether you're a seasoned developer looking to streamline your workflows or a curious beginner eager to dive into the world of browser automation, this blog is designed with you in mind. Here, I'll share a treasure trove of practical insights, clever hacks, and step-by-step guides to help you harness the full power of Playwright - a modern, open-source tool that's revolutionizing how we interact with web applications.

Schedule

Thursday 17 PlayWright
Friday 18 Macintosh
Saturday 19 Internet Tools
Sunday 20 Misc
Monday 21 Media
Tuesday 22 QA
Wednesday 23 Veed