Skip to content

Commit

Permalink
fix: move setupNetworks to wallet (#161)
Browse files Browse the repository at this point in the history
* fix: move setupNetworks to wallet

* fix: add alpha branch

* fix: moved init locators

* fix: return develop
  • Loading branch information
Vorobeyko authored Jun 14, 2024
1 parent b7b39e3 commit eed7da7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
55 changes: 52 additions & 3 deletions packages/wallets/src/metamask/metamask.page.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
import { WalletConfig } from '../wallets.constants';
import { WalletPage } from '../wallet.page';
import expect from 'expect';
import { test, BrowserContext, Page } from '@playwright/test';
import { test, BrowserContext, Page, Locator } from '@playwright/test';

export class MetamaskPage implements WalletPage {
page: Page | undefined;
networkDisplay: Locator;
networkDisplayDialog: Locator;
networkDisplayCloseBtn: Locator;
networkItemBtn: Locator;
networkItemText: Locator;

constructor(
private browserContext: BrowserContext,
private extensionUrl: string,
public config: WalletConfig,
) {}

async initLocators() {
this.networkDisplay = this.page.getByTestId('network-display');
this.networkDisplayDialog = this.page.locator('[role = "dialog"]');
this.networkDisplayCloseBtn = this.networkDisplayDialog
.locator('[aria-label="Close"]')
.first();

this.networkItemBtn =
this.networkDisplayDialog.locator('div[role="button"]');
this.networkItemText = this.networkItemBtn.locator('p');
}

async navigate() {
await test.step('Navigate to metamask', async () => {
this.page = await this.browserContext.newPage();
await this.initLocators();
await this.page.goto(
this.extensionUrl + this.config.COMMON.EXTENSION_START_PATH,
{ waitUntil: 'load' },
Expand All @@ -23,7 +41,7 @@ export class MetamaskPage implements WalletPage {
.locator('button[data-testid="app-header-logo"]')
.waitFor({ state: 'visible' });
await this.unlock();
if (await this.page.getByTestId('network-display').isVisible()) {
if (await this.networkDisplay.isVisible()) {
await this.closePopover();
}
});
Expand All @@ -38,7 +56,7 @@ export class MetamaskPage implements WalletPage {
// added explicit route to #onboarding due to unexpected first time route from /home.html to /onboarding -> page is close
await this.navigate();
if (!this.page) throw "Page isn't ready";
if (!(await this.page.getByTestId('network-display').isVisible())) {
if (!(await this.networkDisplay.isVisible())) {
await this.firstTimeSetup();
}
});
Expand Down Expand Up @@ -159,6 +177,37 @@ export class MetamaskPage implements WalletPage {
await this.page.close();
}

async setupNetwork(standConfig: Record<string, any>) {
const networkDisplayText = await this.networkDisplay.textContent();

if (!networkDisplayText.includes(standConfig.chainName)) {
await this.networkDisplay.click();

const networkListText = await this.getNetworkListText();
if (networkListText.includes(standConfig.chainName)) {
await this.networkItemBtn.getByText(standConfig.chainName).click();
} else {
await this.networkDisplayCloseBtn.click();
await this.addNetwork(
standConfig.chainName,
standConfig.rpcUrl,
standConfig.chainId,
standConfig.tokenSymbol,
standConfig.scan,
);
}
}
}

async getNetworkListText() {
const networkList = await this.networkItemText.all();
return Promise.all(
networkList.map(async (networkType) => {
return await networkType.textContent();
}),
);
}

async addNetwork(
networkName: string,
networkUrl: string,
Expand Down
2 changes: 2 additions & 0 deletions packages/wallets/src/wallet.page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export interface WalletPage {

getWalletAddress?(): Promise<string>;

setupNetwork?(standConfig: Record<string, any>): Promise<void>;

addNetwork(
networkName: string,
networkUrl: string,
Expand Down

0 comments on commit eed7da7

Please sign in to comment.