Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

docs(auth): wait for the response with cookies (#21114)

Yury Semikhatskya81ce3f13d7c
diff --git a/docs/src/auth.md b/docs/src/auth.md
index 4682381f51..744f8bd636 100644
--- a/docs/src/auth.md
+++ b/docs/src/auth.md
@@ -55,6 +55,14 @@ setup('authenticate', async ({ page }) => {
await page.getByLabel('Username or email address').fill('username');
await page.getByLabel('Password').fill('password');
await page.getByRole('button', { name: 'Sign in' }).click();
+ // Wait until the page receives the cookies.
+ //
+ // Sometimes login flow sets cookies in the process of several redirects.
+ // Wait for the final URL to ensure that the cookies are actually set.
+ await page.waitForURL('https://github.com/');
+ // Alternatively, you can wait until the page reaches a state where all cookies are set.
+ await expect(page.getByRole('button', { name: 'View profile and more' })).toBeVisible();
+
// End of authentication steps.
await page.context().storageState({ path: authFile });
@@ -161,6 +169,14 @@ export const test = baseTest.extend<{}, { workerStorageState: string }>({
await page.getByLabel('Username or email address').fill(account.username);
await page.getByLabel('Password').fill(account.password);
await page.getByRole('button', { name: 'Sign in' }).click();
+ // Wait until the page receives the cookies.
+ //
+ // Sometimes login flow sets cookies in the process of several redirects.
+ // Wait for the final URL to ensure that the cookies are actually set.
+ await page.waitForURL('https://github.com/');
+ // Alternatively, you can wait until the page reaches a state where all cookies are set.
+ await expect(page.getByRole('button', { name: 'View profile and more' })).toBeVisible();
+
// End of authentication steps.
await page.context().storageState({ path: fileName });
@@ -393,6 +409,14 @@ setup('authenticate as admin', async ({ page }) => {
await page.getByLabel('Username or email address').fill('admin');
await page.getByLabel('Password').fill('password');
await page.getByRole('button', { name: 'Sign in' }).click();
+ // Wait until the page receives the cookies.
+ //
+ // Sometimes login flow sets cookies in the process of several redirects.
+ // Wait for the final URL to ensure that the cookies are actually set.
+ await page.waitForURL('https://github.com/');
+ // Alternatively, you can wait until the page reaches a state where all cookies are set.
+ await expect(page.getByRole('button', { name: 'View profile and more' })).toBeVisible();
+
// End of authentication steps.
await page.context().storageState({ path: adminFile });
@@ -406,6 +430,14 @@ setup('authenticate as user', async ({ page }) => {
await page.getByLabel('Username or email address').fill('user');
await page.getByLabel('Password').fill('password');
await page.getByRole('button', { name: 'Sign in' }).click();
+ // Wait until the page receives the cookies.
+ //
+ // Sometimes login flow sets cookies in the process of several redirects.
+ // Wait for the final URL to ensure that the cookies are actually set.
+ await page.waitForURL('https://github.com/');
+ // Alternatively, you can wait until the page reaches a state where all cookies are set.
+ await expect(page.getByRole('button', { name: 'View profile and more' })).toBeVisible();
+
// End of authentication steps.
await page.context().storageState({ path: userFile });