Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

fix: fix prompt clicking flakiness, fix multiple snap key permissions #194

Merged
merged 11 commits into from
Nov 28, 2022
5 changes: 3 additions & 2 deletions src/helpers/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ export const clickOnElement = async (

export const clickOnButton = async (
page: DappeteerPage,
text: string
text: string,
options?: { timeout?: number; visible?: boolean }
): Promise<void> => {
const button = await getElementByContent(page, text, "button");
const button = await getElementByContent(page, text, "button", options);
await button.click();
};

Expand Down
1 change: 1 addition & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./actions";
export * from "./selectors";
export { retry } from "./utils";
4 changes: 3 additions & 1 deletion src/helpers/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ import { DappeteerPage, Serializable } from "../page";
export const getElementByContent = (
page: DappeteerPage,
text: string,
type = "*"
type = "*",
options?: { timeout?: number; visible?: boolean }
): Promise<DappeteerElementHandle | null> =>
page.waitForXPath(`//${type}[contains(text(), '${text}')]`, {
timeout: 20000,
visible: true,
...options,
});

export const getInputByLabel = (
Expand Down
14 changes: 14 additions & 0 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export async function retry<R>(
fn: () => Promise<R>,
count: number
): Promise<R> {
let error;
for (let i = 0; i < count; i++) {
try {
return await fn();
} catch (e: unknown) {
error = e;
}
}
throw error;
}
14 changes: 8 additions & 6 deletions src/metamask/approve.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { clickOnButton } from "../helpers";
import { clickOnButton, retry } from "../helpers";
import { DappeteerPage } from "../page";

// TODO: thing about renaming this method?
export const approve = (page: DappeteerPage) => async (): Promise<void> => {
await page.bringToFront();
await page.reload();
await retry(async () => {
await page.bringToFront();
await page.reload();

// TODO: step 1 of connect chose account to connect?
await clickOnButton(page, "Next");
await clickOnButton(page, "Connect");
// TODO: step 1 of connect chose account to connect?
await clickOnButton(page, "Next", { timeout: 100 });
await clickOnButton(page, "Connect", { timeout: 2000 });
}, 5);
};
19 changes: 16 additions & 3 deletions src/metamask/confirmTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { TransactionOptions } from "..";
import { clickOnButton, typeOnInputField } from "../helpers";
import {
clickOnButton,
getElementByContent,
retry,
typeOnInputField,
} from "../helpers";
import { DappeteerPage } from "../page";

import { GetSingedIn } from "./index";
Expand All @@ -13,8 +18,16 @@ export const confirmTransaction =
if (!(await getSingedIn())) {
throw new Error("You haven't signed in yet");
}
await page.waitForTimeout(500);
await page.reload();

//retry till we get prompt
await retry(async () => {
await page.bringToFront();
await page.reload();
await getElementByContent(page, "Edit", "button", {
timeout: 500,
visible: false,
});
}, 15);

if (options) {
await clickOnButton(page, "Edit");
Expand Down
10 changes: 7 additions & 3 deletions src/metamask/sign.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { clickOnButton } from "../helpers";
import { clickOnButton, getElementByContent, retry } from "../helpers";

import { DappeteerPage } from "../page";
import { GetSingedIn } from ".";
Expand All @@ -11,8 +11,12 @@ export const sign =
throw new Error("You haven't signed in yet");
}

await page.waitForTimeout(500);
await page.reload();
//retry till we get prompt
await retry(async () => {
await page.bringToFront();
await page.reload();
await getElementByContent(page, "Sign", "button", { timeout: 100 });
}, 5);

await clickOnButton(page, "Sign");
};
10 changes: 6 additions & 4 deletions src/snap/acceptDialog.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { clickOnButton } from "../helpers";
import { clickOnButton, retry } from "../helpers";
import { DappeteerPage } from "../page";

export const acceptDialog =
(page: DappeteerPage) => async (): Promise<void> => {
await page.bringToFront();
await page.reload();
await clickOnButton(page, "Approve");
await retry(async () => {
await page.bringToFront();
await page.reload();
await clickOnButton(page, "Approve", { timeout: 100 });
}, 5);
};
6 changes: 4 additions & 2 deletions src/snap/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ export const installSnap =
if (opts.hasPermissions) {
await clickOnButton(page, "Approve & install");
if (opts.hasKeyPermissions) {
const checkbox = await page.waitForSelector(".checkbox-label", {
await page.waitForSelector(".checkbox-label", {
visible: true,
});
await checkbox.click();
for await (const checkbox of await page.$$(".checkbox-label")) {
await checkbox.click();
}
await clickOnButton(page, "Confirm");
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/snap/invokeSnap.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DappeteerPage, Serializable, Unboxed } from "../page";
import { flaskOnly } from "./utils";
import { flaskOnly, isMetamaskErrorObject } from "./utils";

export async function invokeSnap<
R = unknown,
Expand Down Expand Up @@ -30,7 +30,7 @@ export async function invokeSnap<
},
{ snapId, method, params }
);
if (result instanceof Error) {
if (result instanceof Error || isMetamaskErrorObject(result)) {
throw result;
} else {
return result;
Expand Down
10 changes: 6 additions & 4 deletions src/snap/rejectDialog.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { clickOnButton } from "../helpers";
import { clickOnButton, retry } from "../helpers";
import { DappeteerPage } from "../page";

export const rejectDialog =
(page: DappeteerPage) => async (): Promise<void> => {
await page.bringToFront();
await page.reload();
await clickOnButton(page, "Reject");
await retry(async () => {
await page.bringToFront();
await page.reload();
await clickOnButton(page, "Reject", { timeout: 100 });
}, 5);
};
10 changes: 10 additions & 0 deletions src/snap/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ export function flaskOnly(page: DappeteerPage): void {
);
}
}

export function isMetamaskErrorObject(e: unknown): boolean {
if (e == undefined) return false;
if (!(e instanceof Object)) return false;
if (!("code" in e)) return false;
if (!("message" in e)) return false;
if (!("data" in e)) return false;
if (!("originalError" in e["data"])) return false;
return true;
}
5 changes: 1 addition & 4 deletions test/contract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { DappeteerPage } from "../src/page";

import { TestContext } from "./constant";
import { Contract } from "./deploy";
import { clickElement, pause } from "./utils/utils";
import { clickElement } from "./utils/utils";

describe("contract interactions", function () {
let contract: Contract;
Expand Down Expand Up @@ -33,15 +33,12 @@ describe("contract interactions", function () {
});

it("should have increased count", async () => {
await pause(1);
const counterBefore = await getCounterNumber(contract);
// click increase button
await clickElement(testPage, ".increase-button");
await pause(1);
// submit tx
await metamask.confirmTransaction();
await testPage.waitForSelector("#txSent", { visible: false });
await pause(1);

const counterAfter = await getCounterNumber(contract);

Expand Down
5 changes: 3 additions & 2 deletions test/flask/keys-snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
}
},
"initialPermissions": {
"snap_getBip44Entropy_1": {}
"snap_getBip44Entropy_1": {},
"snap_getBip44Entropy_47": {}
mpetrunic marked this conversation as resolved.
Show resolved Hide resolved
},
"manifestVersion": "0.1"
}
}