Skip to content

Commit

Permalink
✨ feat: Persist storageState
Browse files Browse the repository at this point in the history
  • Loading branch information
matstyler committed May 10, 2024
1 parent 2146ab3 commit f110f5a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 2 deletions.
2 changes: 1 addition & 1 deletion wallets/metamask/playwright.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default defineConfig({
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] }
use: { ...devices['Desktop Chrome'], storageState: 'playwright/.auth/user.json' }
}
]
})
21 changes: 21 additions & 0 deletions wallets/metamask/playwright/.auth/user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"cookies": [
{
"name": "dsadsadsa",
"value": "2121221",
"domain": "localhost:9999",
"path": "/"
}
],
"origins": [
{
"origin": "/",
"localStorage": [
{
"name": "string",
"value": "string"
}
]
}
]
}
21 changes: 21 additions & 0 deletions wallets/metamask/src/fixture-actions/persistLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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()
}
11 changes: 10 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,14 @@ 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

0 comments on commit f110f5a

Please sign in to comment.