-
-
Notifications
You must be signed in to change notification settings - Fork 195
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: Optional caching for tests and enable tests for windows (#1155)
* optional tagging setuo * add cache check in fixture * do prep for cacheless download * do prep for cacheless download * cacheless fixture testing * gitignore * load extension zip correctly * done * cacheless import working * make compatible with windows * clean up, cache/cli message * linting * usecache setup * cleaning up expanding fixture flow for cacheless setup * fix an issue with async unzip * clean up and solve most other text * skip broken tests * resolve sec issue, clean up * merge * merge * useCache * imports * fix issues for review * review changes * lint * lint * irg * console r
- Loading branch information
1 parent
02d451e
commit af4405a
Showing
23 changed files
with
301 additions
and
65 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -49,3 +49,7 @@ playwright/.cache | |
|
||
**/ethereum-wallet-mock/cypress | ||
**/metamask/cypress | ||
|
||
### Cacheless | ||
|
||
**/metamask/downloads |
This file contains 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 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 |
---|---|---|
|
@@ -3,6 +3,7 @@ declare global { | |
interface ProcessEnv { | ||
CI: boolean | ||
HEADLESS: boolean | ||
USE_CACHE: string | ||
} | ||
} | ||
} | ||
|
This file contains 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 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,14 @@ | ||
import dotenv from 'dotenv' | ||
import findConfig from 'find-config' | ||
|
||
export const loadEnv = () => { | ||
const envFiles = ['.env', '.env.e2e', '.env.local', '.env.dev'] | ||
envFiles.find((envFile) => { | ||
const config = findConfig(envFile) | ||
if (config) { | ||
dotenv.config({ path: config }) | ||
return true | ||
} | ||
return false | ||
}) | ||
} |
39 changes: 39 additions & 0 deletions
39
wallets/metamask/src/fixture-actions/importAndConnectForFixtures.ts
This file contains 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 type { Page } from '@playwright/test' | ||
import { MetaMask } from '..' | ||
import { retryIfMetaMaskCrashAfterUnlock } from '..' | ||
import { closePopover } from '../pages/HomePage/actions' | ||
|
||
export async function importAndConnectForFixtures( | ||
page: Page, | ||
seedPhrase: string, | ||
password: string, | ||
extensionId: string | ||
) { | ||
const metamask = new MetaMask(page.context(), page, password, extensionId) | ||
|
||
await metamask.importWallet(seedPhrase) | ||
|
||
await metamask.openSettings() | ||
|
||
const SidebarMenus = metamask.homePage.selectors.settings.SettingsSidebarMenus | ||
|
||
await metamask.openSidebarMenu(SidebarMenus.Advanced) | ||
|
||
await metamask.toggleDismissSecretRecoveryPhraseReminder() | ||
|
||
await page.goto(`chrome-extension://${extensionId}/home.html`) | ||
|
||
await retryIfMetaMaskCrashAfterUnlock(page) | ||
|
||
await closePopover(page) | ||
|
||
const newPage = await page.context().newPage() | ||
|
||
await newPage.goto('http://localhost:9999') | ||
|
||
await newPage.locator('#connectButton').click() | ||
|
||
await metamask.connectToDapp() | ||
|
||
await newPage.close() | ||
} |
89 changes: 89 additions & 0 deletions
89
wallets/metamask/src/fixture-actions/noCacheMetaMaskSetup.ts
This file contains 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,89 @@ | ||
import path from 'node:path' | ||
import { type BrowserContext, chromium } from '@playwright/test' | ||
import appRoot from 'app-root-path' | ||
import axios from 'axios' | ||
import fs from 'fs-extra' | ||
import unzipper from 'unzipper' | ||
import { DEFAULT_METAMASK_VERSION, EXTENSION_DOWNLOAD_URL } from '../utils/constants' | ||
|
||
async function prepareDownloadDirectory(version: string = DEFAULT_METAMASK_VERSION): Promise<string> { | ||
const downloadsDirectory = | ||
process.platform === 'win32' ? appRoot.resolve('/node_modules') : path.join(process.cwd(), 'downloads') | ||
await fs.ensureDir(downloadsDirectory) | ||
|
||
const metamaskDirectory = path.join(downloadsDirectory, `metamask-chrome-${version}.zip`) | ||
const archiveFileExtension = path.extname(metamaskDirectory) | ||
const outputPath = metamaskDirectory.replace(archiveFileExtension, '') | ||
const metamaskManifestPath = path.join(outputPath, 'manifest.json') | ||
|
||
if (!fs.existsSync(metamaskManifestPath)) { | ||
await downloadAndExtract(EXTENSION_DOWNLOAD_URL, metamaskDirectory) | ||
} | ||
|
||
return outputPath | ||
} | ||
|
||
async function downloadAndExtract(url: string, destination: string): Promise<void> { | ||
const response = await axios.get(url, { responseType: 'stream' }) | ||
const writer = fs.createWriteStream(destination) | ||
response.data.pipe(writer) | ||
await new Promise((resolve) => writer.on('finish', resolve)) | ||
|
||
await unzipArchive(destination) | ||
} | ||
|
||
async function unzipArchive(archivePath: string): Promise<void> { | ||
const archiveFileExtension = path.extname(archivePath) | ||
const outputPath = archivePath.replace(archiveFileExtension, '') | ||
|
||
await fs.ensureDir(outputPath) | ||
|
||
try { | ||
await new Promise<void>((resolve, reject) => { | ||
const stream = fs.createReadStream(archivePath).pipe(unzipper.Parse()) | ||
|
||
stream.on( | ||
'entry', | ||
async (entry: { path: string; type: string; pipe: (arg: unknown) => void; autodrain: () => void }) => { | ||
const fileName = entry.path | ||
const type = entry.type as 'Directory' | 'File' | ||
|
||
if (type === 'Directory') { | ||
await fs.mkdir(path.join(outputPath, fileName), { recursive: true }) | ||
entry.autodrain() | ||
return | ||
} | ||
|
||
if (type === 'File') { | ||
const writeStream = fs.createWriteStream(path.join(outputPath, fileName)) | ||
entry.pipe(writeStream) | ||
|
||
await new Promise<void>((res, rej) => { | ||
writeStream.on('finish', res) | ||
writeStream.on('error', rej) | ||
}) | ||
} | ||
} | ||
) | ||
stream.on('finish', resolve) | ||
stream.on('error', reject) | ||
}) | ||
} catch (error: unknown) { | ||
console.error(`[unzipArchive] Error unzipping archive: ${(error as { message: string }).message}`) | ||
throw error | ||
} | ||
} | ||
|
||
export async function cachelessSetupMetaMask(metamaskVersion?: string): Promise<BrowserContext> { | ||
const metamaskPath = await prepareDownloadDirectory(metamaskVersion || DEFAULT_METAMASK_VERSION) | ||
const browserArgs = [`--load-extension=${metamaskPath}`, `--disable-extensions-except=${metamaskPath}`] | ||
|
||
if (process.env.HEADLESS) { | ||
browserArgs.push('--headless=new') | ||
} | ||
const context = await chromium.launchPersistentContext('', { | ||
headless: false, | ||
args: browserArgs | ||
}) | ||
return context | ||
} |
This file contains 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 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.