From 1e41dcf7290332513dcb34fcb91a9133a73822c8 Mon Sep 17 00:00:00 2001 From: Daniel Izdebski Date: Fri, 6 Oct 2023 16:01:38 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(metamask):=20Add=20`getExtensi?= =?UTF-8?q?onId`=20helper=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pnpm-lock.yaml | 7 +++++ wallets/metamask/package.json | 3 +- wallets/metamask/src/utils/getExtensionId.ts | 32 ++++++++++++++++++++ wallets/metamask/test/e2e/metamask.spec.ts | 19 +++++++++++- 4 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 wallets/metamask/src/utils/getExtensionId.ts diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index bb11e1acd..6acec17aa 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -128,6 +128,9 @@ importers: core: specifier: workspace:* version: link:../../packages/core + zod: + specifier: ^3.22.4 + version: 3.22.4 devDependencies: '@playwright/test': specifier: ^1.38.1 @@ -4034,3 +4037,7 @@ packages: compress-commons: 5.0.1 readable-stream: 3.6.2 dev: true + + /zod@3.22.4: + resolution: {integrity: sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==} + dev: false diff --git a/wallets/metamask/package.json b/wallets/metamask/package.json index 248553213..e70ff6819 100644 --- a/wallets/metamask/package.json +++ b/wallets/metamask/package.json @@ -29,7 +29,8 @@ "types:check": "tsc --noEmit" }, "dependencies": { - "core": "workspace:*" + "core": "workspace:*", + "zod": "^3.22.4" }, "devDependencies": { "@playwright/test": "^1.38.1", diff --git a/wallets/metamask/src/utils/getExtensionId.ts b/wallets/metamask/src/utils/getExtensionId.ts new file mode 100644 index 000000000..6e3f20d7a --- /dev/null +++ b/wallets/metamask/src/utils/getExtensionId.ts @@ -0,0 +1,32 @@ +import type { BrowserContext } from '@playwright/test' +import { z } from 'zod' + +const Extension = z.object({ + id: z.string(), + name: z.string() +}) + +const Extensions = z.array(Extension) + +export async function getExtensionId(context: BrowserContext, extensionName: string) { + const page = await context.newPage() + await page.goto('chrome://extensions') + + const unparsedExtensions = await page.evaluate('chrome.management.getAll()') + + const allExtensions = Extensions.parse(unparsedExtensions) + const extension = allExtensions.find((extension) => extension.name.toLowerCase() === extensionName.toLowerCase()) + + if (!extension) { + throw new Error( + [ + `[GetExtensionId] Extension with name ${extensionName} not found.`, + `Available extensions: ${allExtensions.map((extension) => extension.name).join(', ')}` + ].join('\n') + ) + } + + await page.close() + + return extension.id +} diff --git a/wallets/metamask/test/e2e/metamask.spec.ts b/wallets/metamask/test/e2e/metamask.spec.ts index 31abe01f3..f2bb1c25c 100644 --- a/wallets/metamask/test/e2e/metamask.spec.ts +++ b/wallets/metamask/test/e2e/metamask.spec.ts @@ -1,13 +1,22 @@ -import { type Page, chromium, test as base } from '@playwright/test' +import { type BrowserContext, type Page, chromium, test as base } from '@playwright/test' import { OnboardingPage } from '../../src/pages' import { prepareExtension } from '../../src/prepareExtension' +import { getExtensionId } from '../../src/utils/getExtensionId' const DEFAULT_SEED_PHRASE = 'test test test test test test test test test test test junk' const DEFAULT_PASSWORD = 'Tester@1234' +let sharedContext: BrowserContext | undefined + // Fixture for the test. const test = base.extend({ context: async ({ context: _ }, use) => { + if (sharedContext) { + await use(sharedContext) + + return + } + const metamaskPath = await prepareExtension() // biome-ignore format: the array should not be formatted @@ -31,6 +40,7 @@ const test = base.extend({ throw new Error('[FIXTURE] MetaMask extension did not load in time') } + sharedContext = context await use(context) }, page: async ({ context }, use) => { @@ -53,4 +63,11 @@ describe('MetaMask', () => { await expect(page.getByText('0xf39...2266')).toBeVisible() }) }) + + describe('getExtensionId', () => { + test('should return the extension id', async ({ context }) => { + const extensionId = await getExtensionId(context, 'MetaMask') + expect(extensionId).toMatch(/^[a-z]{32}$/) + }) + }) })