-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathpopup.ts
134 lines (111 loc) · 4.11 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
129
130
131
132
133
134
import { type Page, expect } 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);
return accounts.find((account) => account.name === name);
}
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 is already selected return it
// to avoid unnecessary changes
if (account.isCurrent) {
return account;
}
await popupPage.waitForTimeout(2000);
await getByAriaLabel(popupPage, 'Accounts').click();
await expect
.poll(
() =>
hasText(popupPage, name)
.then(() => true)
.catch(() => false),
{ timeout: 15000 }
)
.toBeTruthy();
// Add position to click on the element and not on copy button
await getByAriaLabel(popupPage, name).click({
position: {
x: 10,
y: 10,
},
});
await waitAriaLabel(popupPage, `${name} selected`);
// Return account to be used on tests
account = await getAccountByName(popupPage, name);
return account;
}
export async function hideAccount(popupPage: Page, name: string) {
await getByAriaLabel(popupPage, 'Accounts').click();
// Add position to click on the element and not on copy button
await getByAriaLabel(popupPage, `Account Actions ${name}`).click();
await getByAriaLabel(popupPage, `Hide ${name}`).click();
await hasText(popupPage, 'Show hidden accounts');
await popupPage.getByText(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); // log the text to the console
return text;
} catch (error) {
console.error('Failed to read clipboard contents:', error);
return ''; // Return empty string or handle the error as needed
}
}
export async function getAddressForAccountNumber(
popupPage: Page,
accountName: string,
accountNumber: number
): Promise<string> {
await getByAriaLabel(popupPage, 'Accounts').click();
// Construct the XPath selector dynamically based on the order of the account in the list
const xpathSelector = `/html/body/div[2]/div/div/div/div/article[${accountNumber}]/div[1]/div[2]/div/div/button`;
// Click on the element
await popupPage.click(`xpath=${xpathSelector}`);
await getByAriaLabel(popupPage, accountName).click({
position: {
x: 10,
y: 10,
},
});
return getClipboardText(popupPage);
}