Skip to content

Commit

Permalink
feat(roll): roll to 1.29 Playwright (microsoft#887)
Browse files Browse the repository at this point in the history
Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
playwrightmachine and github-actions[bot] authored Dec 20, 2022
1 parent b813847 commit 74e2c50
Showing 1 changed file with 5 additions and 58 deletions.
63 changes: 5 additions & 58 deletions nodejs/versioned_docs/version-stable/auth.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -302,17 +302,7 @@ module.exports = async () => {

By default, Playwright Test runs tests in parallel. If you reuse a single signed-in state for all your tests, this usually leads to the same account being signed in from multiple tests at the same time. If this behavior is undesirable for your application, you can sign in with a different account in each [worker process](./test-parallel.mdx#worker-processes) created by Playwright Test.

In this example we [override `storageState` fixture](./test-fixtures.mdx#overriding-fixtures) and ensure we only sign in once per worker, using [testInfo.workerIndex](/api/class-testinfo.mdx#test-info-worker-index) to differentiate between workers.

<Tabs
groupId="js-flavor"
defaultValue="ts"
values={[
{label: 'TypeScript', value: 'ts'},
{label: 'JavaScript', value: 'js'}
]
}>
<TabItem value="ts">
In this example we [override `storageState` fixture](./test-fixtures.mdx#overriding-fixtures) and ensure we only sign in once per worker, using [testInfo.parallelIndex](/api/class-testinfo.mdx#test-info-parallel-index) to differentiate between workers.

```js
// fixtures.ts
Expand All @@ -327,15 +317,15 @@ const users = [

export const test = baseTest.extend({
storageState: async ({ browser }, use, testInfo) => {
// Override storage state, use worker index to look up logged-in info and generate it lazily.
const fileName = path.join(testInfo.project.outputDir, 'storage-' + testInfo.workerIndex);
// Override storage state, use parallel index to look up logged-in info and generate it lazily.
const fileName = path.join(testInfo.project.outputDir, 'storage-' + testInfo.parallelIndex);
if (!fs.existsSync(fileName)) {
// Make sure we are not using any other storage state.
const page = await browser.newPage({ storageState: undefined });
await page.goto('https://github.com/login');
// Create a unique username for each worker.
await page.getByLabel('User Name').fill(users[testInfo.workerIndex].username);
await page.getByLabel('Password').fill(users[testInfo.workerIndex].password);
await page.getByLabel('User Name').fill(users[testInfo.parallelIndex].username);
await page.getByLabel('Password').fill(users[testInfo.parallelIndex].password);
await page.getByText('Sign in').click();
await page.context().storageState({ path: fileName });
await page.close();
Expand All @@ -352,49 +342,6 @@ test('test', async ({ page }) => {
});
```

</TabItem>
<TabItem value="js">

```js
// fixtures.js
const { test: base } = require('@playwright/test');

const users = [
{ username: 'user-1', password: 'password-1' },
{ username: 'user-2', password: 'password-2' },
// ... put your test users here ...
];

exports.test = base.extend({
storageState: async ({ browser }, use, testInfo) => {
// Override storage state, use worker index to look up logged-in info and generate it lazily.
const fileName = path.join(testInfo.project.outputDir, 'storage-' + testInfo.workerIndex);
if (!fs.existsSync(fileName)) {
// Make sure we are not using any other storage state.
const page = await browser.newPage({ storageState: undefined });
await page.goto('https://github.com/login');
await page.getByLabel('User Name').fill(users[testInfo.workerIndex].username);
await page.getByLabel('Password').fill(users[testInfo.workerIndex].password);
await page.getByText('Sign in').click();
await page.context().storageState({ path: fileName });
await page.close();
}
await use(fileName);
},
});
exports.expect = base.expect;

// example.spec.js
const { test, expect } = require('./fixtures');

test('test', async ({ page }) => {
// page is signed in.
});
```

</TabItem>
</Tabs>

## Multiple signed in roles

Sometimes you have more than one signed-in user in your end to end tests. You can achieve that via logging in for these users multiple times in globalSetup and saving that state into different files.
Expand Down

0 comments on commit 74e2c50

Please sign in to comment.