Here are some best practices to make your @playwright/test tests more robust:
Avoid relying on implementation details. Your tests should verify that the application works for the end users. Use Playwright's built-in locators to find elements on the webpage. These locators come with auto waiting and retry-ability, ensuring that Playwright performs a range of actionability checks on the elements before performing any actions.
import { test } from '@playwright/test';
test('test user-visible behavior', async ({ page }) => {
await page.goto('https://ray.run/');
const locator = page.locator('.my-element');
await locator.click();
});
Each test should run independently with its own local storage, session storage, data, cookies etc. This improves reproducibility and prevents cascading test failures. Use before and after hooks to avoid repetition.
import { test } from '@playwright/test';
test.beforeEach(async ({ page }) => {
await page.goto('https://ray.run/');
});
test('test isolation', async ({ page }) => {
// Your test code here
});
Instead of testing links to external sites or third party servers, use the Playwright Network API to guarantee the response needed.
import { test } from '@playwright/test';
test('avoid testing third-party dependencies', async ({ page, route }) => {
await route.fulfill({
response: {
body: JSON.stringify({ key: 'value' }),
},
});
await page.goto('https://ray.run/');
});
If working with a database, test against a staging environment and ensure the data doesn't change. For visual regression tests, ensure the operating system and browser versions are consistent.
For local debugging in VSCode, install an extension which allows you to run tests in debug mode. For CI failures, use Playwright trace viewer instead of videos/screenshots. This gives a full trace of your tests as a local Progressive Web App (PWA) that can easily be shared.
By following these best practices, you can ensure your automation suite is more resilient than ever before! For more tips, check out Mastering Playwright Test Automation: Your Comprehensive Cheat Sheet.
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].