Skip to content

Commit

Permalink
feature: allow playwright browser context
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneskares authored Aug 3, 2024
1 parent c9cc6fd commit fa0ec96
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 32 deletions.
18 changes: 12 additions & 6 deletions src/installMockWallet.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
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";

let wallets: Map<string, Wallet> = 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() {
Expand Down Expand Up @@ -55,6 +57,10 @@ export async function installMockWallet({
window.addEventListener("eip6963:requestProvider", () => {
announceMockWallet();
});

window.addEventListener("DOMContentLoaded", () => {
announceMockWallet();
});
},
{ uuid },
);
Expand Down
32 changes: 6 additions & 26 deletions tests/transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit fa0ec96

Please sign in to comment.