Skip to content

Commit

Permalink
cherry-pick(#21008): docs: update auth docs to remove cross-run caching
Browse files Browse the repository at this point in the history
  • Loading branch information
dgozman committed Feb 21, 2023
1 parent 725dd8b commit 1cad99c
Showing 1 changed file with 8 additions and 45 deletions.
53 changes: 8 additions & 45 deletions docs/src/auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Playwright executes tests in isolated environments called [browser contexts](./b

Regardless of the authentication strategy you choose, you are likely to store authenticated browser state on the file system.

We recommend to create `playwright/.auth` directory and add it to your `.gitignore`. You authentication routine will produce authenticated browser state and save it to a file in this `playwright/.auth` directory. Later on, tests will reuse this state and start already authenticated.
We recommend to create `playwright/.auth` directory and add it to your `.gitignore`. Your authentication routine will produce authenticated browser state and save it to a file in this `playwright/.auth` directory. Later on, tests will reuse this state and start already authenticated.

```bash tab=bash-bash
mkdir -p playwright/.auth
Expand All @@ -27,21 +27,6 @@ New-Item -ItemType Directory -Force -Path playwright\.auth
Add-Content -path .gitignore "`r`nplaywright/.auth"
```

Usually you would want to reuse authenticated state between multiple test runs, especially when authoring tests. All the examples in this guide authenticate lazily, and reuse auth state when possible. However, your app may require to re-authenticate after some amount of time. In this case, just remove `playwright/.auth` directory, and re-run your tests.

```bash tab=bash-bash
# Remove auth state
rm -rf playwright/.auth
```

```batch tab=bash-batch
rd /s /q playwright/.auth
```

```powershell tab=bash-powershell
Remove-Item -Recurse -Force playwright/.auth
```

## Basic: shared account in all tests
* langs: js

Expand All @@ -61,16 +46,10 @@ Create `auth.setup.ts` that will prepare authenticated browser state for all oth
```js
// auth.setup.ts
import { test as setup } from '@playwright/test';
import fs from 'fs';
import path from 'path';

const authFile = 'playwright/.auth/user.json';

setup('authenticate', async ({ page }) => {
// Reuse authenticate from previous runs.
if (fs.existsSync(authFile))
return;

// Perform authentication steps. Replace these actions with your own.
await page.goto('https://github.com/login');
await page.getByLabel('Username or email address').fill('username');
Expand Down Expand Up @@ -160,7 +139,7 @@ export const test = baseTest.extend<{}, { workerStorageState: string }>({
workerStorageState: [async ({ browser }, use) => {
// Use parallelIndex as a unique identifier for each worker.
const id = test.info().parallelIndex;
const fileName = path.resolve(__dirname, `.auth/${id}.json`);
const fileName = path.resolve(test.info().project.outputDir, `.auth/${id}.json`);

if (fs.existsSync(fileName)) {
// Reuse existing authentication state if any.
Expand Down Expand Up @@ -327,17 +306,11 @@ In the [setup project](#basic-shared-account-in-all-tests):

```js
// auth.setup.ts
import { test } from '@playwright/test';
import path from 'path';
import fs from 'fs';
import { test as setup } from '@playwright/test';

const authFile = 'playwright/.auth/user.json';

test('authenticate', async ({ request }) => {
// Reuse authenticate from previous runs.
if (fs.existsSync(authFile))
return;

setup('authenticate', async ({ request }) => {
// Send authentication request. Replace with your own.
await request.post('https://github.com/login', {
form: {
Expand Down Expand Up @@ -366,7 +339,7 @@ export const test = baseTest.extend<{}, { workerStorageState: string }>({
workerStorageState: [async ({}, use) => {
// Use parallelIndex as a unique identifier for each worker.
const id = test.info().parallelIndex;
const fileName = path.resolve(__dirname, `.auth/${id}.json`);
const fileName = path.resolve(test.info().project.outputDir, `.auth/${id}.json`);

if (fs.existsSync(fileName)) {
// Reuse existing authentication state if any.
Expand Down Expand Up @@ -410,17 +383,11 @@ We will authenticate multiple times in the setup project.

```js
// auth.setup.ts
import { test } from '@playwright/test';
import fs from 'fs';
import path from 'path';
import { test as setup } from '@playwright/test';

const adminFile = 'playwright/.auth/admin.json';

test('authenticate as admin', async ({ page }) => {
// Reuse authenticate from previous runs.
if (fs.existsSync(adminFile))
return;

setup('authenticate as admin', async ({ page }) => {
// Perform authentication steps. Replace these actions with your own.
await page.goto('https://github.com/login');
await page.getByLabel('Username or email address').fill('admin');
Expand All @@ -433,11 +400,7 @@ test('authenticate as admin', async ({ page }) => {

const userFile = 'playwright/.auth/user.json';

test('authenticate as user', async ({ page }) => {
// Reuse authenticate from previous runs.
if (fs.existsSync(userFile))
return;

setup('authenticate as user', async ({ page }) => {
// Perform authentication steps. Replace these actions with your own.
await page.goto('https://github.com/login');
await page.getByLabel('Username or email address').fill('user');
Expand Down

0 comments on commit 1cad99c

Please sign in to comment.