Focus on verifying that the application code works for end users. Interact with the rendered output on the page, just like your users would.
Ensure each test is independent, with its own local storage, session storage, data, and cookies. This improves reproducibility, debugging, and prevents cascading failures.
Utilize before
and after
hooks to avoid repetition and keep tests isolated. For example, use a before
hook to navigate to a specific URL or log in before each test.
import { test } from '@playwright/test';
test.beforeEach(async ({ page }) => {
await page.goto('https://ray.run/');
});
test('example test', async ({ page }) => {
// Your test code here
});
It's acceptable to have some duplication in simple tests if it makes them clearer and easier to maintain.
With Playwright Test, use the built-in request
fixture for API testing. Create APIRequestContext
methods for tasks like testing server APIs or validating post-conditions after browser actions.
import { test } from '@playwright/test';
test('API test example', async ({ request }) => {
const response = await request.post('https://ray.run/api/some-endpoint', {
json: { key: 'value' },
});
expect(response.status()).toBe(200);
expect(await response.json()).toEqual({ result: 'success' });
});
By following these best practices, you'll create well-structured and organized API tests with Playwright.
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].