Skip to content
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

✨ feat: Persist storageState #1129

Merged
merged 3 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions wallets/metamask/src/fixture-actions/persistLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { BrowserContext } from '@playwright/test'

export async function persistLocalStorage(
origins: {
origin: string
localStorage: { name: string; value: string }[]
}[],
context: BrowserContext
) {
const newPage = await context.newPage()

for (const { origin, localStorage } of origins) {
const frame = newPage.mainFrame()
await frame.goto(origin)

await frame.evaluate((localStorageData) => {
localStorageData.forEach(({ name, value }) => {
window.localStorage.setItem(name, value)
})
}, localStorage)
}

await newPage.close()
}
12 changes: 11 additions & 1 deletion wallets/metamask/src/fixtures/metaMaskFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@synthetixio/synpress-cache'
import { type Anvil, type CreateAnvilOptions, createPool } from '@viem/anvil'
import fs from 'fs-extra'
import { persistLocalStorage } from '../fixture-actions/persistLocalStorage'

type MetaMaskFixtures = {
_contextPath: string
Expand Down Expand Up @@ -38,7 +39,7 @@ export const metaMaskFixtures = (walletSetup: ReturnType<typeof defineWalletSetu
console.error(error)
}
},
context: async ({ context: _, _contextPath }, use) => {
context: async ({ context: currentContext, _contextPath }, use) => {
const cacheDirPath = path.join(process.cwd(), CACHE_DIR_NAME, walletSetup.hash)
if (!(await fs.exists(cacheDirPath))) {
throw new Error(`Cache for ${walletSetup.hash} does not exist. Create it first!`)
Expand Down Expand Up @@ -66,6 +67,15 @@ export const metaMaskFixtures = (walletSetup: ReturnType<typeof defineWalletSetu
slowMo: process.env.HEADLESS ? 0 : slowMo
})

const { cookies, origins } = await currentContext.storageState()

if (cookies) {
await context.addCookies(cookies)
}
if (origins && origins.length > 0) {
await persistLocalStorage(origins, context)
}

// TODO: This should be stored in a store to speed up the tests.
const extensionId = await getExtensionId(context, 'MetaMask')

Expand Down
Loading