-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
index.js
384 lines (349 loc) · 12.2 KB
/
index.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
const path = require('path');
const fs = require('fs');
const helpers = require('../helpers');
const puppeteer = require('puppeteer-core');
const fetch = require('node-fetch');
const { pageElements } = require('../pages/metamask/page');
const {
welcomePageElements,
firstTimeFlowPageElements,
metametricsPageElements,
firstTimeFlowFormPageElements,
revealSeedPageElements,
} = require('../pages/metamask/first-time-flow-page');
const { mainPageElements } = require('../pages/metamask/main-page');
const { unlockPageElements } = require('../pages/metamask/unlock-page');
const {
notificationPageElements,
permissionsPageElements,
confirmPageElements,
} = require('../pages/metamask/notification-page');
let puppeteerBrowser;
let mainWindow;
let metamaskWindow;
let walletAddress;
/**
* @type {Cypress.PluginConfig}
*/
module.exports = (on, config) => {
// `on` is used to hook into various events Cypress emits
// `config` is the resolved Cypress config
on('before:browser:launch', async (browser = {}, arguments_) => {
if (browser.name === 'chrome' && browser.isHeadless) {
console.log('TRUE'); // required by cypress ¯\_(ツ)_/¯
arguments_.args.push('--window-size=1920,1080');
return arguments_;
}
if (browser.name === 'electron') {
arguments_['width'] = 1920;
arguments_['height'] = 1080;
arguments_['resizable'] = false;
return arguments_;
}
// metamask welcome screen blocks cypress from loading
if (browser.name === 'chrome') {
arguments_.args.push('--disable-background-timer-throttling');
arguments_.args.push('--disable-backgrounding-occluded-windows');
arguments_.args.push('--disable-renderer-backgrounding');
}
// NOTE: extensions cannot be loaded in headless Chrome
const metamaskPath = await prepareMetamask();
arguments_.extensions.push(metamaskPath);
return arguments_;
});
on('task', {
error(message) {
console.error('\u001B[31m', 'ERROR:', message, '\u001B[0m');
},
warn(message) {
console.warn('\u001B[33m', 'WARNING:', message, '\u001B[0m');
},
initPuppeteer: async () => {
const connected = await initPuppeteer();
return connected;
},
assignWindows: async () => {
const assigned = await assignWindows();
return assigned;
},
switchToCypressWindow: async () => {
const switched = await switchToCypressWindow();
return switched;
},
switchToMetamaskWindow: async () => {
const switched = await switchToMetamaskWindow();
return switched;
},
switchToMetamaskNotification: async () => {
const notificationPage = await switchToMetamaskNotification();
return notificationPage;
},
confirmMetamaskWelcomePage: async () => {
const confirmed = await confirmMetamaskWelcomePage();
return confirmed;
},
unlockMetamask: async password => {
const unlocked = await unlockMetamask(password);
return unlocked;
},
importMetamaskWallet: async ({ secretWords, password }) => {
const imported = await importMetamaskWallet(secretWords, password);
return imported;
},
changeMetamaskNetwork: async network => {
const networkChanged = await changeMetamaskNetwork(network);
return networkChanged;
},
acceptMetamaskAccess: async () => {
const accepted = await acceptMetamaskAccess();
return accepted;
},
confirmMetamaskTransaction: async () => {
const confirmed = await confirmMetamaskTransaction();
return confirmed;
},
rejectMetamaskTransaction: async () => {
const rejected = await rejectMetamaskTransaction();
return rejected;
},
getMetamaskWalletAddress: async () => {
const walletAddress = await getMetamaskWalletAddress();
return walletAddress;
},
setupMetamask: async ({ secretWords, network, password }) => {
if (secretWords === undefined && process.env.SECRET_WORDS) {
secretWords = process.env.SECRET_WORDS;
}
await initPuppeteer();
await assignWindows();
await metamaskWindow.waitForTimeout(1000);
if ((await metamaskWindow.$(unlockPageElements.unlockPage)) === null) {
await confirmMetamaskWelcomePage();
await importMetamaskWallet(secretWords, password);
await changeMetamaskNetwork(network);
walletAddress = await getMetamaskWalletAddress();
await switchToCypressWindow();
return true;
} else {
await unlockMetamask(password);
walletAddress = await getMetamaskWalletAddress();
await switchToCypressWindow();
return true;
}
},
fetchMetamaskWalletAddress: async () => {
return walletAddress;
},
});
// setup config
if (process.env.BASE_URL) {
config.baseUrl = process.env.BASE_URL;
}
if (process.env.CI) {
config.retries.runMode = 1;
config.retries.openMode = 1;
}
return config;
};
async function waitFor(selector, page = metamaskWindow) {
await page.waitForFunction(
`document.querySelector('${selector}') && document.querySelector('${selector}').clientHeight != 0`,
{ visible: true },
);
// puppeteer going too fast breaks metamask in corner cases
await page.waitForTimeout(300);
}
async function waitAndClick(selector, page = metamaskWindow) {
await waitFor(selector, page);
await page.evaluate(
selector => document.querySelector(selector).click(),
selector,
);
}
async function waitAndType(selector, value, page = metamaskWindow) {
await waitFor(selector, page);
const element = await page.$(selector);
await element.type(value);
}
async function waitAndGetValue(selector, page = metamaskWindow) {
await waitFor(selector, page);
const element = await page.$(selector);
const property = await element.getProperty('value');
const value = await property.jsonValue();
return value;
}
async function waitAndSetValue(text, selector, page = metamaskWindow) {
await waitFor(selector, page);
await page.evaluate(
selector => (document.querySelector(selector).value = ''),
selector,
);
await page.focus(selector);
await page.keyboard.type(text);
}
async function waitForText(selector, text, page = metamaskWindow) {
await waitFor(selector, page);
await page.waitForFunction(
`document.querySelector('${selector}').innerText.toLowerCase().includes('${text}')`,
);
}
async function prepareMetamask() {
const release = await helpers.getMetamaskReleases();
const downloadsDirectory = path.resolve(__dirname, 'downloads');
if (!fs.existsSync(downloadsDirectory)) {
fs.mkdirSync(downloadsDirectory);
}
const downloadDestination = path.join(downloadsDirectory, release.filename);
await helpers.download(release.downloadUrl, downloadDestination);
const metamaskDirectory = path.join(downloadsDirectory, 'metamask');
await helpers.extract(downloadDestination, metamaskDirectory);
return metamaskDirectory;
}
async function initPuppeteer() {
const debuggerDetails = await fetch('http://localhost:9222/json/version');
const debuggerDetailsConfig = await debuggerDetails.json();
const webSocketDebuggerUrl = debuggerDetailsConfig.webSocketDebuggerUrl;
puppeteerBrowser = await puppeteer.connect({
browserWSEndpoint: webSocketDebuggerUrl,
ignoreHTTPSErrors: true,
// eslint-disable-next-line unicorn/no-null
defaultViewport: null,
});
return puppeteerBrowser.isConnected();
}
async function assignWindows() {
let pages = await puppeteerBrowser.pages();
for (const page of pages) {
if (page.url().includes('integration')) {
mainWindow = page;
} else if (page.url().includes('extension')) {
metamaskWindow = page;
}
}
return true;
}
async function switchToCypressWindow() {
await mainWindow.bringToFront();
return true;
}
async function switchToMetamaskWindow() {
await metamaskWindow.bringToFront();
return true;
}
async function switchToMetamaskNotification() {
let pages = await puppeteerBrowser.pages();
for (const page of pages) {
if (page.url().includes('notification')) {
await page.bringToFront();
return page;
}
}
}
async function confirmMetamaskWelcomePage() {
await fixBlankMetamask();
await waitAndClick(welcomePageElements.confirmButton);
return true;
}
async function unlockMetamask(password) {
await fixBlankMetamask();
await waitAndType(unlockPageElements.passwordInput, password);
await waitAndClick(unlockPageElements.unlockButton);
return true;
}
async function importMetamaskWallet(secretWords, password) {
await waitAndClick(firstTimeFlowPageElements.importWalletButton);
await waitAndClick(metametricsPageElements.optOutAnalyticsButton);
await waitAndType(
firstTimeFlowFormPageElements.secretWordsInput,
secretWords,
);
await waitAndType(firstTimeFlowFormPageElements.passwordInput, password);
await waitAndType(
firstTimeFlowFormPageElements.confirmPasswordInput,
password,
);
await waitAndClick(firstTimeFlowFormPageElements.termsCheckbox);
await waitAndClick(firstTimeFlowFormPageElements.importButton);
// metamask hangs, reload as workaround
// await waitAndClick(endOfFlowPageElements.allDoneButton);
await waitFor(pageElements.loadingSpinner);
await metamaskWindow.reload();
await waitAndClick(revealSeedPageElements.remindLaterButton);
await waitFor(mainPageElements.walletOverview);
// close popup if present
if ((await metamaskWindow.$(mainPageElements.popup.container)) !== null) {
await waitAndClick(mainPageElements.popup.closeButton);
}
return true;
}
async function changeMetamaskNetwork(network) {
await waitAndClick(mainPageElements.networkSwitcher.button);
if (network === 'main') {
await waitAndClick(mainPageElements.networkSwitcher.networkButton(0));
} else if (network === 'ropsten') {
await waitAndClick(mainPageElements.networkSwitcher.networkButton(1));
} else if (network === 'kovan') {
await waitAndClick(mainPageElements.networkSwitcher.networkButton(2));
} else if (network === 'rinkeby') {
await waitAndClick(mainPageElements.networkSwitcher.networkButton(3));
} else if (network === 'goerli') {
await waitAndClick(mainPageElements.networkSwitcher.networkButton(4));
} else if (network === 'localhost') {
await waitAndClick(mainPageElements.networkSwitcher.networkButton(5));
}
await waitForText(mainPageElements.networkSwitcher.networkName, network);
return true;
}
async function acceptMetamaskAccess() {
await metamaskWindow.waitForTimeout(3000);
const notificationPage = await switchToMetamaskNotification();
await waitAndClick(notificationPageElements.nextButton, notificationPage);
await waitAndClick(permissionsPageElements.connectButton, notificationPage);
await metamaskWindow.waitForTimeout(3000);
return true;
}
async function confirmMetamaskTransaction() {
await metamaskWindow.waitForTimeout(3000);
const notificationPage = await switchToMetamaskNotification();
const currentGasFee = await waitAndGetValue(
confirmPageElements.gasFeeInput,
notificationPage,
);
const newGasFee = (Number(currentGasFee) + 10).toString();
await waitAndSetValue(
newGasFee,
confirmPageElements.gasFeeInput,
notificationPage,
);
await waitAndClick(confirmPageElements.confirmButton, notificationPage);
await metamaskWindow.waitForTimeout(3000);
return true;
}
async function rejectMetamaskTransaction() {
await metamaskWindow.waitForTimeout(3000);
const notificationPage = await switchToMetamaskNotification();
await waitAndClick(confirmPageElements.rejectButton, notificationPage);
await metamaskWindow.waitForTimeout(3000);
return true;
}
async function getMetamaskWalletAddress() {
await waitAndClick(mainPageElements.options.button);
await waitAndClick(mainPageElements.options.accountDetailsButton);
const walletAddress = await waitAndGetValue(
mainPageElements.accountModal.walletAddressInput,
);
await waitAndClick(mainPageElements.accountModal.closeButton);
return walletAddress;
}
// workaround for metamask random blank page on first run
async function fixBlankMetamask() {
await metamaskWindow.waitForTimeout(1000);
for (let times = 0; times < 5; times++) {
if ((await metamaskWindow.$(welcomePageElements.app)) === null) {
await metamaskWindow.reload();
await metamaskWindow.waitForTimeout(2000);
} else {
break;
}
}
}