-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(browser): support playwright persistent context #9229
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
Merged
sheremet-va
merged 18 commits into
vitest-dev:main
from
hi-ogawa:12-11-feat_browser_support_playwright_persistent_context
Jan 19, 2026
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ebb1288
feat(browser): support playwright persistent context
hi-ogawa 5a71b60
docs(browser): document playwright persistentContext option
hi-ogawa f935feb
disable persistentContext when headless
hi-ogawa ed26cda
refactor: move getContextOptions
hi-ogawa 2b9c375
cleanup
hi-ogawa 4f5a594
Merge branch 'main' into 12-11-feat_browser_support_playwright_persis…
hi-ogawa 7d89e16
Merge branch 'main' into 12-11-feat_browser_support_playwright_persis…
hi-ogawa d4de468
fix(browser-playwright): use parallel flag instead of headless for pe…
hi-ogawa 88b03e2
test: e2e
hi-ogawa a03baa7
fix: fix parallel detection
hi-ogawa d036a17
refactor: tweak
hi-ogawa b81927b
test: test persistence
hi-ogawa e09644b
test: more robust
hi-ogawa 244f151
docs: Update docs/config/browser/playwright.md
hi-ogawa a68e687
refactor: not optional options: { parallel: boolean }
hi-ogawa bc61965
Merge remote-tracking branch 'origin/12-11-feat_browser_support_playw…
hi-ogawa 2ab69b7
Merge branch 'main' into 12-11-feat_browser_support_playwright_persis…
sheremet-va b2d883d
Apply suggestions from code review
sheremet-va 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
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
17 changes: 17 additions & 0 deletions
17
test/config/fixtures/browser-persistent-context/basic.test.ts
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 |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import { expect, test } from "vitest"; | ||
|
|
||
| const expectedValue = import.meta.env.TEST_EXPECTED_VALUE || "0"; | ||
|
|
||
| test(`expectedValue = ${expectedValue}`, () => { | ||
| // increment localStorage to test persistent context between test runs | ||
| const value = localStorage.getItem("test-persistent-context") || "0"; | ||
| const nextValue = String(Number(value) + 1); | ||
| console.log(`localStorage: value = ${value}, nextValue = ${nextValue}`); | ||
| localStorage.setItem("test-persistent-context", nextValue); | ||
|
|
||
| const div = document.createElement("div"); | ||
| div.textContent = `localStorage: value = ${value}, nextValue = ${nextValue}`; | ||
| document.body.appendChild(div); | ||
|
|
||
| expect(value).toBe(expectedValue) | ||
| }); |
20 changes: 20 additions & 0 deletions
20
test/config/fixtures/browser-persistent-context/vitest.config.ts
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 |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { playwright } from "@vitest/browser-playwright"; | ||
| import path from "node:path"; | ||
| import { defineConfig } from "vitest/config"; | ||
|
|
||
| export default defineConfig({ | ||
| define: { | ||
| 'import.meta.env.TEST_EXPECTED_VALUE': JSON.stringify(String(process.env.TEST_EXPECTED_VALUE)), | ||
| }, | ||
| test: { | ||
| browser: { | ||
| enabled: true, | ||
| headless: true, | ||
| provider: playwright({ | ||
| persistentContext: path.join(import.meta.dirname, "./node_modules/.cache/test-user-data"), | ||
| }), | ||
| instances: [{ browser: "chromium" }], | ||
| }, | ||
| fileParallelism: false, | ||
| }, | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { existsSync, rmSync } from 'node:fs' | ||
| import { resolve } from 'pathe' | ||
| import { expect, test } from 'vitest' | ||
| import { runVitest } from '../../test-utils' | ||
|
|
||
| test('persistent context works', async () => { | ||
| // clean user data dir | ||
| const root = resolve(import.meta.dirname, '../fixtures/browser-persistent-context') | ||
| const userDataDir = resolve(root, 'node_modules/.cache/test-user-data') | ||
| rmSync(userDataDir, { recursive: true, force: true }) | ||
|
|
||
| // first run | ||
| process.env.TEST_EXPECTED_VALUE = '0' | ||
| const result1 = await runVitest({ root }) | ||
| expect(result1.errorTree()).toMatchInlineSnapshot(` | ||
| { | ||
| "basic.test.ts": { | ||
| "expectedValue = 0": "passed", | ||
| }, | ||
| } | ||
| `) | ||
| // check user data | ||
| expect(existsSync(userDataDir)).toBe(true) | ||
|
|
||
| // 2nd run | ||
| // localStorage is incremented during 1st run and | ||
| // 2nd run should pick that up from persistent context | ||
| process.env.TEST_EXPECTED_VALUE = '1' | ||
| const result2 = await runVitest({ root }) | ||
| expect(result2.errorTree()).toMatchInlineSnapshot(` | ||
| { | ||
| "basic.test.ts": { | ||
| "expectedValue = 1": "passed", | ||
| }, | ||
| } | ||
| `) | ||
| // check user data | ||
| expect(existsSync(userDataDir)).toBe(true) | ||
| }) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.