Playwright's authentication feature is your go-to for logging in. It's as simple as filling in the username and password fields and clicking the "Sign in" button. But remember, you need to wait until the page receives cookies. This is because the login flow often sets cookies during several redirects. So, wait for the final URL to make sure all cookies are set.
// TypeScript code example
import { test } from '@playwright/test';
test('login test', async ({ page }) => {
await page.goto('https://ray.run/login');
await page.fill('input[name="username"]', 'your-username');
await page.fill('input[name="password"]', 'your-password');
await page.click('button[type="submit"]');
await page.waitForNavigation();
});
Sometimes, you might want to authenticate once per worker with a worker-scoped fixture. For this, you need a unique account. Fill in its username and password fields, click on "Sign in", and wait for all cookies to set.
// TypeScript code example
import { test } from '@playwright/test';
test.use({
storageState: 'path-to-authenticated-storage-state.json',
});
test('authenticated test', async ({ page }) => {
await page.goto('https://ray.run/dashboard');
// Perform your actions here
});
There you have it! Different ways to log in with Playwright, tailored to your needs.
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].