-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathpopup.ts
128 lines (105 loc) · 3.82 KB
/
popup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import type { Page } from '@playwright/test';
import { expect } from '@fuels/playwright-utils';
import { getByAriaLabel, hasText, visit, waitAriaLabel } from '../../commons';
import type { MockData } from '../../mocks';
export async function getAccountByName(popupPage: Page, name: string) {
const accounts = await getWalletAccounts(popupPage);
// Added check to throw an error if account is not found
const account = accounts.find((account) => account.name === name);
if (!account) {
throw new Error(`Account with name "${name}" not found`);
}
return account;
}
export async function getWalletAccounts(popupPage: Page) {
const accounts = await popupPage.evaluate(async () => {
const fuelDB = window.fuelDB;
return fuelDB.accounts.toArray();
});
return accounts;
}
export async function switchAccount(popupPage: Page, name: string) {
let account = await getAccountByName(popupPage, name);
if (account.isCurrent) {
return account;
}
await popupPage.waitForTimeout(2000);
await getByAriaLabel(popupPage, 'Accounts').click();
await hasText(popupPage, name);
await popupPage.waitForTimeout(5000);
await getByAriaLabel(popupPage, name).click();
await expect
.poll(
() =>
waitAriaLabel(popupPage, `${name} selected`)
.then(() => true)
.catch(() => false),
{ timeout: 30000 }
)
.toBeTruthy();
account = await getAccountByName(popupPage, name);
return account;
}
export async function hideAccount(popupPage: Page, name: string) {
await getByAriaLabel(popupPage, 'Accounts').click();
await getByAriaLabel(popupPage, `Account Actions ${name}`).click();
await getByAriaLabel(popupPage, `Hide ${name}`).click();
await hasText(popupPage, 'Show hidden accounts');
await popupPage.locator(`text=${name}`).isHidden();
await getByAriaLabel(popupPage, 'Close dialog').click();
}
export async function waitAccountPage(popupPage: Page, name: string) {
await popupPage.waitForSelector(`[data-account-name="${name}"]`);
}
export async function waitWalletToLoad(popupPage: Page) {
await popupPage.waitForSelector('[data-account-name]');
}
export async function createAccount(popupPage: Page) {
await waitWalletToLoad(popupPage);
const accounts = await getWalletAccounts(popupPage);
await getByAriaLabel(popupPage, 'Accounts').click();
await getByAriaLabel(popupPage, 'Add account').click();
await waitAccountPage(popupPage, `Account ${accounts.length + 1}`);
}
export async function createAccountFromPrivateKey(
popupPage: Page,
privateKey: string,
name: string
) {
await waitWalletToLoad(popupPage);
await getByAriaLabel(popupPage, 'Accounts').click();
await getByAriaLabel(popupPage, 'Import from private key').click();
await getByAriaLabel(popupPage, 'Private Key').fill(privateKey);
if (name) {
await getByAriaLabel(popupPage, 'Account Name').clear();
await getByAriaLabel(popupPage, 'Account Name').fill(name);
}
await getByAriaLabel(popupPage, 'Import').click();
await waitAccountPage(popupPage, name);
}
export async function getClipboardText(popupPage: Page): Promise<string> {
try {
const text = await popupPage.evaluate(() => navigator.clipboard.readText());
console.log('Clipboard text:', text);
return text;
} catch (error) {
console.error('Failed to read clipboard contents:', error);
return '';
}
}
export async function getAddressForAccountNumber(
popupPage: Page,
accountName: string,
accountNumber: number
): Promise<string> {
await getByAriaLabel(popupPage, 'Accounts').click();
const xpathSelector = `/html/body/div[2]/div/div/div/div/article[${accountNumber}]/div[1]/div[2]/div/div/button`;
await popupPage.click(`xpath=${xpathSelector}`);
await getByAriaLabel(popupPage, accountName).click({
position: {
x: 10,
y: 10,
},
});
return getClipboardText(popupPage);
}