|
1 |
| -import type { Page } from '@playwright/test' |
| 1 | +import type { Locator, Page } from '@playwright/test' |
2 | 2 | import Selectors from '../selectors'
|
| 3 | +import { allTextContents } from '../../../utils/allTextContents' |
3 | 4 |
|
4 |
| -export async function connectToDapp(notificationPage: Page) { |
5 |
| - // Click `Next`. |
6 |
| - await notificationPage.locator(Selectors.ActionFooter.confirmActionButton).click() |
| 5 | +async function selectAccounts(accountsToSelect: string[], accountLocators: Locator[], availableAccountNames: string[]) { |
| 6 | + for (const account of accountsToSelect) { |
| 7 | + const accountNameIndex = availableAccountNames.findIndex((name) => name.startsWith(account)) |
| 8 | + if (accountNameIndex < 0) throw new Error(`[ConnectToDapp] Account with name ${account} not found`) |
| 9 | + await accountLocators[accountNameIndex]!.locator(Selectors.ConnectPage.accountCheckbox).check() |
| 10 | + } |
| 11 | +} |
7 | 12 |
|
8 |
| - // Click `Connect`. |
| 13 | +async function confirmConnection(notificationPage: Page) { |
9 | 14 | await notificationPage.locator(Selectors.ActionFooter.confirmActionButton).click()
|
| 15 | + await notificationPage.locator(Selectors.ActionFooter.confirmActionButton).click() |
| 16 | +} |
| 17 | + |
| 18 | +// By default, only the last account will be selected. If you want to select a specific account, pass `accounts` parameter. |
| 19 | +export async function connectToDapp(notificationPage: Page, accounts?: string[]) { |
| 20 | + if (accounts && accounts.length > 0) { |
| 21 | + // Wait for the accounts to be loaded as 'all()' doesnt not wait for the results - https://playwright.dev/docs/api/class-locator#locator-all |
| 22 | + // Additionally disable default account to reuse necessary delay |
| 23 | + await notificationPage |
| 24 | + .locator(Selectors.ConnectPage.accountOption) |
| 25 | + .locator(Selectors.ConnectPage.accountCheckbox) |
| 26 | + .last() |
| 27 | + .setChecked(false) |
| 28 | + |
| 29 | + const accountLocators = await notificationPage.locator(Selectors.ConnectPage.accountOption).all() |
| 30 | + const accountNames = await allTextContents(accountLocators) |
| 31 | + |
| 32 | + await selectAccounts(accounts, accountLocators, accountNames) |
| 33 | + } |
| 34 | + |
| 35 | + await confirmConnection(notificationPage) |
10 | 36 | }
|
0 commit comments