Skip to content

Commit

Permalink
add cache check in fixture
Browse files Browse the repository at this point in the history
  • Loading branch information
Seroxdesign committed Jun 25, 2024
1 parent a2356f3 commit ffdee82
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 42 deletions.
2 changes: 1 addition & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion wallets/metamask/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
},
"dependencies": {
"@synthetixio/synpress-cache": "0.0.1-alpha.7",
"@synthetixio/synpress-core": "0.0.1-alpha.7",
"@synthetixio/synpress-core": "workspace:*",
"@viem/anvil": "0.0.7",
"fs-extra": "11.2.0",
"zod": "3.22.4"
Expand Down
87 changes: 47 additions & 40 deletions wallets/metamask/src/fixtures/metaMaskFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { type Anvil, type CreateAnvilOptions, createPool } from '@viem/anvil'
import fs from 'fs-extra'
import { persistLocalStorage } from '../fixture-actions/persistLocalStorage'

const USECACHE = false;

type MetaMaskFixtures = {
_contextPath: string
metamask: MetaMask
Expand All @@ -30,53 +32,58 @@ let _metamaskPage: Page
export const metaMaskFixtures = (walletSetup: ReturnType<typeof defineWalletSetup>, slowMo = 0) => {
return base.extend<MetaMaskFixtures>({
_contextPath: async ({ browserName }, use, testInfo) => {

const contextPath = await createTempContextDir(browserName, testInfo.testId)
if (USECACHE) {
const contextPath = await createTempContextDir(browserName, testInfo.testId)

await use(contextPath)
await use(contextPath)

const error = await removeTempContextDir(contextPath)
if (error) {
console.error(error)
const error = await removeTempContextDir(contextPath)
if (error) {
console.error(error)
}
}
},
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!`)
}

// Copying the cache to the temporary context directory.
await fs.copy(cacheDirPath, _contextPath)

const metamaskPath = await prepareExtension()

// We don't need the `--load-extension` arg since the extension is already loaded in the cache.
const browserArgs = [`--disable-extensions-except=${metamaskPath}`]

if (process.env.HEADLESS) {
browserArgs.push('--headless=new')

if (slowMo > 0) {
console.warn('[WARNING] Slow motion makes no sense in headless mode. It will be ignored!')
let context
if (USECACHE) {
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!`)
}

// Copying the cache to the temporary context directory.
await fs.copy(cacheDirPath, _contextPath)

const metamaskPath = await prepareExtension()

// We don't need the `--load-extension` arg since the extension is already loaded in the cache.
const browserArgs = [`--disable-extensions-except=${metamaskPath}`]

if (process.env.HEADLESS) {
browserArgs.push('--headless=new')

if (slowMo > 0) {
console.warn('[WARNING] Slow motion makes no sense in headless mode. It will be ignored!')
}
}

context = await chromium.launchPersistentContext(_contextPath, {
headless: false,
args: browserArgs,
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)
}

}

const context = await chromium.launchPersistentContext(_contextPath, {
headless: false,
args: browserArgs,
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)
}

if (!context) return;
// 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 ffdee82

Please sign in to comment.