Skip to content

Commit

Permalink
✨ feat(metamask): Add getExtensionId helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
duckception committed Oct 6, 2023
1 parent 9d56bf0 commit 1e41dcf
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

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

3 changes: 2 additions & 1 deletion wallets/metamask/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"types:check": "tsc --noEmit"
},
"dependencies": {
"core": "workspace:*"
"core": "workspace:*",
"zod": "^3.22.4"
},
"devDependencies": {
"@playwright/test": "^1.38.1",
Expand Down
32 changes: 32 additions & 0 deletions wallets/metamask/src/utils/getExtensionId.ts
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 18 additions & 1 deletion wallets/metamask/test/e2e/metamask.spec.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) => {
Expand All @@ -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}$/)
})
})
})

0 comments on commit 1e41dcf

Please sign in to comment.