From fa0ec96634045c02b34b3057e329801853af59e9 Mon Sep 17 00:00:00 2001 From: Johannes Kares Date: Sat, 3 Aug 2024 19:33:13 +0200 Subject: [PATCH] feature: allow playwright browser context --- src/installMockWallet.ts | 18 ++++++++++++------ tests/transaction.spec.ts | 32 ++++++-------------------------- 2 files changed, 18 insertions(+), 32 deletions(-) diff --git a/src/installMockWallet.ts b/src/installMockWallet.ts index 4fc0c42..2f9ab15 100644 --- a/src/installMockWallet.ts +++ b/src/installMockWallet.ts @@ -1,4 +1,4 @@ -import type { Page } from "@playwright/test"; +import type { BrowserContext, Page } from "@playwright/test"; import { Wallet, createWallet } from "./createWallet"; import { LocalAccount, Transport } from "viem"; import { randomUUID } from "crypto"; @@ -6,22 +6,24 @@ import { randomUUID } from "crypto"; let wallets: Map = new Map(); export async function installMockWallet({ - page, account, transport, + ...params }: { - page: Page; account: LocalAccount; transport: Transport; -}) { +} & ({ page: Page } | { browserContext: BrowserContext })) { + const browserOrPage = + "browserContext" in params ? params.browserContext : params.page; + // Connecting the browser context to the Node.js playwright context - await page.exposeFunction("eip1193Request", eip1193Request); + await browserOrPage.exposeFunction("eip1193Request", eip1193Request); // Everytime we call installMockWallet, we create a new uuid to identify the wallet. const uuid = randomUUID(); wallets.set(uuid, createWallet(account, transport)); - await page.addInitScript( + await browserOrPage.addInitScript( ({ uuid }) => { // This function needs to be declared in the browser context function announceMockWallet() { @@ -55,6 +57,10 @@ export async function installMockWallet({ window.addEventListener("eip6963:requestProvider", () => { announceMockWallet(); }); + + window.addEventListener("DOMContentLoaded", () => { + announceMockWallet(); + }); }, { uuid }, ); diff --git a/tests/transaction.spec.ts b/tests/transaction.spec.ts index c657192..dc25483 100644 --- a/tests/transaction.spec.ts +++ b/tests/transaction.spec.ts @@ -20,33 +20,13 @@ test.beforeEach(async ({ page }) => { }); }); -test("talentir", async ({ page }) => { - const baseUrl = "https://dev.talentir.com"; - // const baseUrl = "http://localhost:3000"; +test("Metamask Wallet Test Dapp", async ({ page }) => { + const baseUrl = "https://metamask.github.io/test-dapp/"; await page.goto(baseUrl); - - await page.getByRole("button", { name: "Accept all" }).click(); - await page.getByRole("button", { name: "Log In" }).click(); - await page.getByRole("button", { name: "Choose Wallet" }).click(); - await page.getByRole("menuitem", { name: "Mock Wallet" }).last().click(); - - await expect(page).toHaveURL(baseUrl + "/dashboard/assets"); - await page.getByRole("link", { name: "Wallet" }).click(); - - await page.waitForTimeout(2000); - - await page.getByLabel("Transfer").click(); - await page - .getByPlaceholder("0xe0a942ff2e1724A2fe10627728bE327a43fE8C23") - .fill("0xe0a942ff2e1724A2fe10627728bE327a43fE8C26"); - - await page.getByPlaceholder("-").fill("- 0.01"); - - await page.getByRole("button", { name: "Send USDC" }).click(); - await page.getByLabel("I take full responsibility").click(); - await page.getByRole("button", { name: "Confirm" }).click(); + await page.getByRole("button", { name: "USE MOCK WALLET" }).click(); await expect( - page.getByRole("heading", { name: "Transaction Successful" }), - ).toBeVisible({ timeout: 30000 }); + page.getByRole("heading", { name: "Active Provider" }), + ).toBeVisible(); + await expect(page.getByText("Name: Mock Wallet")).toBeVisible(); });