-
Notifications
You must be signed in to change notification settings - Fork 5.9k
feat(webauthn): include credentials in storageState #41215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dgozman
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
dgozman:storagestate-credentials
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -271,9 +271,9 @@ existing authentication state instead. | |
| Playwright provides a way to reuse the signed-in state in the tests. That way you can log | ||
| in only once and then skip the log in step for all of the tests. | ||
|
|
||
| Web apps use cookie-based or token-based authentication, where authenticated state is stored as [cookies](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies), in [local storage](https://developer.mozilla.org/en-US/docs/Web/API/Storage) or in [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API). Playwright provides [`method: BrowserContext.storageState`] method that can be used to retrieve storage state from authenticated contexts and then create new contexts with prepopulated state. | ||
| Web apps use cookie-based or token-based authentication, where authenticated state is stored as [cookies](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies), in [local storage](https://developer.mozilla.org/en-US/docs/Web/API/Storage), in [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API), or as passkeys ([WebAuthn](https://developer.mozilla.org/en-US/docs/Web/API/Web_Authentication_API) credentials). Playwright provides [`method: BrowserContext.storageState`] method that can be used to retrieve storage state from authenticated contexts and then create new contexts with prepopulated state. | ||
|
|
||
| Cookies, local storage and IndexedDB state can be used across different browsers. They depend on your application's authentication model which may require some combination of cookies, local storage or IndexedDB. | ||
| Cookies, local storage, IndexedDB and virtual WebAuthn credentials (passkeys) can be used across different browsers. They depend on your application's authentication model which may require some combination of cookies, local storage, IndexedDB or passkeys. | ||
|
|
||
| The following code snippet retrieves state from an authenticated context and creates a new context with that state. | ||
|
|
||
|
|
@@ -397,75 +397,6 @@ export const test = baseTest.extend<{}, { workerStorageState: string }>({ | |
| }); | ||
| ``` | ||
|
|
||
| ### Passkeys (WebAuthn) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe also keep for now? |
||
| * langs: js | ||
|
|
||
| **When to use** | ||
| - Your app signs users in with passkeys (WebAuthn), and you want tests to start already enrolled. | ||
|
|
||
| **Details** | ||
|
|
||
| [`property: BrowserContext.credentials`] is a virtual WebAuthn authenticator. Unlike cookie or local storage state, a passkey is seeded **imperatively** with [`method: Credentials.create`] and [`method: Credentials.install`], so it lives in a [`context` fixture override](./test-fixtures.md#overriding-fixtures) rather than in the `storageState` config option. | ||
|
|
||
| If your backend already provisioned a passkey for the test user, seed it directly — no setup project required: | ||
|
|
||
| ```js title="playwright/fixtures.ts" | ||
| import { test as baseTest } from '@playwright/test'; | ||
| export * from '@playwright/test'; | ||
|
|
||
| export const test = baseTest.extend({ | ||
| context: async ({ context }, use) => { | ||
| // A passkey your backend provisioned for the test user. | ||
| await context.credentials.create({ | ||
| rpId: 'example.com', | ||
| id: process.env.PASSKEY_ID, | ||
| userHandle: process.env.PASSKEY_USER_HANDLE, | ||
| privateKey: process.env.PASSKEY_PRIVATE_KEY, | ||
| publicKey: process.env.PASSKEY_PUBLIC_KEY, | ||
| }); | ||
| await context.credentials.install(); | ||
| await use(context); | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| Otherwise, let the app register a passkey once in a [setup project](#basic-shared-account-in-all-tests), capture it with [`method: Credentials.get`], and save it to disk: | ||
|
|
||
| ```js title="tests/passkey.setup.ts" | ||
| import { test as setup } from '@playwright/test'; | ||
| import fs from 'fs'; | ||
|
|
||
| setup('enroll passkey', async ({ context, page }) => { | ||
| await context.credentials.install(); | ||
| await page.goto('https://example.com/register'); | ||
| // The app calls navigator.credentials.create() to register the passkey. | ||
| await page.getByRole('button', { name: 'Create a passkey' }).click(); | ||
|
|
||
| // Read back the registered passkey, including its private key, and save it. | ||
| const [credential] = await context.credentials.get({ rpId: 'example.com' }); | ||
| fs.writeFileSync('playwright/.auth/passkey.json', JSON.stringify(credential)); | ||
| }); | ||
| ``` | ||
|
|
||
| Then seed the captured passkey into every test's context: | ||
|
|
||
| ```js title="playwright/fixtures.ts" | ||
| import { test as baseTest } from '@playwright/test'; | ||
| import fs from 'fs'; | ||
| export * from '@playwright/test'; | ||
|
|
||
| export const test = baseTest.extend({ | ||
| context: async ({ context }, use) => { | ||
| const credential = JSON.parse(fs.readFileSync('playwright/.auth/passkey.json', 'utf8')); | ||
| await context.credentials.create(credential); | ||
| await context.credentials.install(); | ||
| await use(context); | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| Declare the `setup` project as a [dependency](./test-projects.md#dependencies) of your testing projects, just like in the [basic flow](#basic-shared-account-in-all-tests). The saved `passkey.json` contains a private key, so keep it under `playwright/.auth` and out of source control (see [Core concepts](#core-concepts)). | ||
|
|
||
| ### Multiple signed in roles | ||
| * langs: js | ||
|
|
||
|
|
@@ -657,7 +588,7 @@ test('admin and user', async ({ adminPage, userPage }) => { | |
|
|
||
| ### Session storage | ||
|
|
||
| Reusing authenticated state covers [cookies](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies), [local storage](https://developer.mozilla.org/en-US/docs/Web/API/Storage) and [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) based authentication. Rarely, [session storage](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage) is used for storing information associated with the signed-in state. Session storage is specific to a particular domain and is not persisted across page loads. Playwright does not provide API to persist session storage, but the following snippet can be used to save/load session storage. | ||
| Reusing authenticated state covers [cookies](https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies), [local storage](https://developer.mozilla.org/en-US/docs/Web/API/Storage), [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) and passkey ([WebAuthn](https://developer.mozilla.org/en-US/docs/Web/API/Web_Authentication_API)) based authentication. Rarely, [session storage](https://developer.mozilla.org/en-US/docs/Web/API/Window/sessionStorage) is used for storing information associated with the signed-in state. Session storage is specific to a particular domain and is not persisted across page loads. Playwright does not provide API to persist session storage, but the following snippet can be used to save/load session storage. | ||
|
|
||
| ```js | ||
| // Get session storage and store as env variable | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keep this section too?