When selecting UI elements like buttons and text fields in @playwright/test, follow these best practices:
import { test, expect } from '@playwright/test';
test('Find a button with a specific label', async ({ page }) => {
await page.goto('https://ray.run/');
const button = await page.locator('button').withText('Submit');
await button.click();
// Continue with your test
});
import { test, expect } from '@playwright/test';
test('Find a text field using role attribute', async ({ page }) => {
await page.goto('https://ray.run/');
const textField = await page.locator('[role="textbox"]');
await textField.fill('Sample text');
// Continue with your test
});
For complex UI components like dropdown menus, creating helper functions can simplify testing and reduce code duplication. However, for basic UI elements, helper elements may not always be necessary or beneficial.
Remember to follow these best practices to ensure your tests remain accurate even if your DOM structure changes.
If you still have questions, please ask a question and I will try to answer it.
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 [email protected].