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

Commit

Permalink
feat: create account method (#253)
Browse files Browse the repository at this point in the history
* feat: create account method

* update test

* update tests

* typo
  • Loading branch information
Lykhoyda authored Jan 26, 2023
1 parent 225efaa commit ec7e492
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 9 deletions.
14 changes: 14 additions & 0 deletions src/metamask/createAccount.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { clickOnElement, profileDropdownClick, retry } from "../helpers";
import { DappeteerPage } from "../page";

export const createAccount =
(page: DappeteerPage) =>
async (accountName: string): Promise<void> => {
await retry(async () => {
await page.bringToFront();
await profileDropdownClick(page);
await clickOnElement(page, "Create account");
await page.type(".new-account-create-form__input", accountName);
await clickOnElement(page, "Create");
}, 5);
};
2 changes: 2 additions & 0 deletions src/metamask/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { switchAccount } from "./switchAccount";
import { switchNetwork } from "./switchNetwork";
import { unlock } from "./unlock";
import { acceptAddToken, rejectAddToken } from "./addToken";
import { createAccount } from "./createAccount";

export type SetSignedIn = (state: boolean) => Promise<void>;
export type GetSingedIn = () => Promise<boolean>;
Expand Down Expand Up @@ -53,6 +54,7 @@ export const getMetaMask = (page: DappeteerPage): Promise<Dappeteer> => {
unlock: unlock(page, setSignedIn, getSingedIn),
acceptAddToken: acceptAddToken(page),
rejectAddToken: rejectAddToken(page),
createAccount: createAccount(page),
helpers: {
getTokenBalance: getTokenBalance(page),
deleteAccount: deleteAccount(page),
Expand Down
6 changes: 6 additions & 0 deletions src/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ export interface DappeteerPage<P = unknown> {
name: string,
callback: Function | { default: Function }
): Promise<void>;

type(
selector: string,
text: string,
options?: { delay: number }
): Promise<void>;
}

export type Unboxed<Arg> = Arg extends DappeteerElementHandle<any, infer T>
Expand Down
8 changes: 8 additions & 0 deletions src/playwright/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,12 @@ export class DPlaywrightPage implements DappeteerPage<Page> {
): Promise<void> {
return this.page.exposeFunction(name, <Function>callback);
}

type(
selector: string,
text: string,
options?: { delay: number }
): Promise<void> {
return this.page.type(selector, text, options);
}
}
8 changes: 8 additions & 0 deletions src/puppeteer/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,12 @@ export class DPupeteerPage implements DappeteerPage<Page> {
exposeFunction(name: string, callback: Function): Promise<void> {
return this.page.exposeFunction(name, callback);
}

type(
selector: string,
text: string,
options?: { delay: number }
): Promise<void> {
return this.page.type(selector, text, options);
}
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export type Dappeteer = {
sign: () => Promise<void>;
signTypedData: () => Promise<void>;
approve: () => Promise<void>;
createAccount: (accountName: string) => Promise<void>;
helpers: {
getTokenBalance: (tokenSymbol: string) => Promise<number>;
deleteAccount: (accountNumber: number) => Promise<void>;
Expand Down
36 changes: 29 additions & 7 deletions test/basic.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { expect, use } from "chai";
import chaiAsPromised from "chai-as-promised";
import * as dappeteer from "../src";
import { profileDropdownClick } from "../src/helpers";
import { DappeteerPage } from "../src/page";
import { clickOnLogo, profileDropdownClick } from "../src/helpers";
import { DappeteerPage } from "../src";

import {
PASSWORD,
TestContext,
EXPECTED_MESSAGE_SIGNATURE,
ACCOUNT_ADDRESS,
MESSAGE_TO_SIGN,
EXAMPLE_WEBSITE,
EXPECTED_LONG_TYPED_DATA_SIGNATURE,
EXPECTED_MESSAGE_SIGNATURE,
EXPECTED_SHORT_TYPED_DATA_SIGNATURE,
EXAMPLE_WEBSITE,
MESSAGE_TO_SIGN,
PASSWORD,
TestContext,
} from "./constant";
import {
addNetwork,
Expand Down Expand Up @@ -179,4 +179,26 @@ describe("basic interactions", function () {
);
expect(accountSwitcher).to.not.be.undefined;
});

it("should create an account and switch back to the default", async () => {
await metaMask.createAccount("Account 2");
await metaMask.switchAccount(1);
await profileDropdownClick(metaMaskPage);
await metaMaskPage.waitForSelector(".account-menu__check-mark svg");

const firstAccountSelected = await metaMaskPage.evaluate(() => {
return !!document.querySelector(
".account-menu__accounts .account-menu__account:nth-child(1) .account-menu__check-mark svg"
);
});
const secondAccountSelected = await metaMaskPage.evaluate(() => {
return !!document.querySelector(
".account-menu__accounts .account-menu__account:nth-child(2) .account-menu__check-mark svg"
);
});
expect((await metaMaskPage.$$(".account-menu__account")).length).to.eq(2);
expect(firstAccountSelected).to.be.true;
expect(secondAccountSelected).to.be.false;
await clickOnLogo(metaMaskPage);
});
});
3 changes: 1 addition & 2 deletions test/contract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { expect } from "chai";

import Web3 from "web3";
import { Contract } from "web3-eth-contract";
import { Dappeteer } from "../src";
import { DappeteerPage } from "../src/page";
import { Dappeteer, DappeteerPage } from "../src";

import { ACCOUNT_ADDRESS, EXAMPLE_WEBSITE, TestContext } from "./constant";
import { ContractInfo } from "./contract/contractInfo";
Expand Down

0 comments on commit ec7e492

Please sign in to comment.