-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
metamask.js
401 lines (390 loc) · 13.8 KB
/
metamask.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
const puppeteer = require('./puppeteer');
const { pageElements } = require('../pages/metamask/page');
const {
welcomePageElements,
firstTimeFlowPageElements,
metametricsPageElements,
firstTimeFlowFormPageElements,
secureYourWalletPageElements,
revealSeedPageElements,
endOfFlowPageElements,
} = 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');
const {
settingsPageElements,
advancedPageElements,
resetAccountModalElements,
networksPageElements,
addNetworkPageElements,
} = require('../pages/metamask/settings-page');
const { setNetwork, getNetwork } = require('../helpers');
let walletAddress;
module.exports = {
walletAddress: () => {
return walletAddress;
},
// workaround for metamask random blank page on first run
fixBlankPage: async () => {
await puppeteer.metamaskWindow().waitForTimeout(1000);
for (let times = 0; times < 5; times++) {
if (
(await puppeteer.metamaskWindow().$(welcomePageElements.app)) === null
) {
await puppeteer.metamaskWindow().reload();
await puppeteer.metamaskWindow().waitForTimeout(2000);
} else {
break;
}
}
},
confirmWelcomePage: async () => {
await module.exports.fixBlankPage();
await puppeteer.waitAndClick(welcomePageElements.confirmButton);
return true;
},
unlock: async password => {
await module.exports.fixBlankPage();
await puppeteer.waitAndType(unlockPageElements.passwordInput, password);
await puppeteer.waitAndClick(unlockPageElements.unlockButton);
return true;
},
importWallet: async (secretWords, password) => {
await puppeteer.waitAndClick(firstTimeFlowPageElements.importWalletButton);
await puppeteer.waitAndClick(metametricsPageElements.optOutAnalyticsButton);
await puppeteer.waitAndType(
firstTimeFlowFormPageElements.secretWordsInput,
secretWords,
);
await puppeteer.waitAndType(
firstTimeFlowFormPageElements.passwordInput,
password,
);
await puppeteer.waitAndType(
firstTimeFlowFormPageElements.confirmPasswordInput,
password,
);
await puppeteer.waitAndClick(firstTimeFlowFormPageElements.termsCheckbox);
await puppeteer.waitAndClick(firstTimeFlowFormPageElements.importButton);
await puppeteer.waitFor(pageElements.loadingSpinner);
await puppeteer.waitAndClick(endOfFlowPageElements.allDoneButton);
await puppeteer.waitFor(mainPageElements.walletOverview);
// close popup if present
if (
(await puppeteer.metamaskWindow().$(mainPageElements.popup.container)) !==
null
) {
await puppeteer.waitAndClick(mainPageElements.popup.closeButton);
}
return true;
},
createWallet: async password => {
await puppeteer.waitAndClick(firstTimeFlowPageElements.createWalletButton);
await puppeteer.waitAndClick(metametricsPageElements.optOutAnalyticsButton);
await puppeteer.waitAndType(
firstTimeFlowFormPageElements.newPasswordInput,
password,
);
await puppeteer.waitAndType(
firstTimeFlowFormPageElements.confirmPasswordInput,
password,
);
await puppeteer.waitAndClick(
firstTimeFlowFormPageElements.newSignupCheckbox,
);
await puppeteer.waitAndClick(firstTimeFlowFormPageElements.importButton);
await puppeteer.waitFor(pageElements.loadingSpinner);
await puppeteer.waitAndClick(secureYourWalletPageElements.nextButton);
await puppeteer.waitAndClick(revealSeedPageElements.remindLaterButton);
await puppeteer.waitFor(mainPageElements.walletOverview);
// close popup if present
if (
(await puppeteer.metamaskWindow().$(mainPageElements.popup.container)) !==
null
) {
await puppeteer.waitAndClick(mainPageElements.popup.closeButton);
}
return true;
},
importFromPrivateKey: async privateKey => {
await puppeteer.waitAndClick(mainPageElements.accountMenu.button);
await puppeteer.waitAndClick(
mainPageElements.accountMenu.importAccountButton,
);
await puppeteer.waitAndType(
mainPageElements.importAccount.input,
privateKey,
);
await puppeteer.waitAndClick(mainPageElements.importAccount.importButton);
return true;
},
changeNetwork: async network => {
setNetwork(network);
await puppeteer.waitAndClick(mainPageElements.networkSwitcher.button);
if (network === 'main' || network === 'mainnet') {
await puppeteer.waitAndClick(
mainPageElements.networkSwitcher.networkButton(0),
);
} else if (network === 'ropsten') {
await puppeteer.waitAndClick(
mainPageElements.networkSwitcher.networkButton(1),
);
} else if (network === 'kovan') {
await puppeteer.waitAndClick(
mainPageElements.networkSwitcher.networkButton(2),
);
} else if (network === 'rinkeby') {
await puppeteer.waitAndClick(
mainPageElements.networkSwitcher.networkButton(3),
);
} else if (network === 'goerli') {
await puppeteer.waitAndClick(
mainPageElements.networkSwitcher.networkButton(4),
);
} else if (network === 'localhost') {
await puppeteer.waitAndClick(
mainPageElements.networkSwitcher.networkButton(5),
);
} else if (typeof network === 'object') {
await puppeteer.waitAndClickByText(
mainPageElements.networkSwitcher.dropdownMenuItem,
network.networkName,
);
} else {
await puppeteer.waitAndClickByText(
mainPageElements.networkSwitcher.dropdownMenuItem,
network,
);
}
if (typeof network === 'object') {
await puppeteer.waitForText(
mainPageElements.networkSwitcher.networkName,
network.networkName,
);
} else {
await puppeteer.waitForText(
mainPageElements.networkSwitcher.networkName,
network,
);
}
return true;
},
addNetwork: async network => {
if (
process.env.NETWORK_NAME &&
process.env.RPC_URL &&
process.env.CHAIN_ID
) {
network = {
networkName: process.env.NETWORK_NAME,
rpcUrl: process.env.RPC_URL,
chainId: process.env.CHAIN_ID,
symbol: process.env.SYMBOL,
blockExplorer: process.env.BLOCK_EXPLORER,
isTestnet: process.env.IS_TESTNET,
};
}
await puppeteer.waitAndClick(mainPageElements.accountMenu.button);
await puppeteer.waitAndClick(mainPageElements.accountMenu.settingsButton);
await puppeteer.waitAndClick(settingsPageElements.networksButton);
await puppeteer.waitAndClick(networksPageElements.addNetworkButton);
await puppeteer.waitAndType(
addNetworkPageElements.networkNameInput,
network.networkName,
);
await puppeteer.waitAndType(
addNetworkPageElements.rpcUrlInput,
network.rpcUrl,
);
await puppeteer.waitAndType(
addNetworkPageElements.chainIdInput,
network.chainId,
);
await puppeteer.waitAndType(
addNetworkPageElements.symbolInput,
network.symbol,
);
await puppeteer.waitAndType(
addNetworkPageElements.blockExplorerInput,
network.blockExplorer,
);
await puppeteer.waitAndClick(addNetworkPageElements.saveButton);
await puppeteer.waitAndClick(settingsPageElements.closeButton);
await puppeteer.waitForText(
mainPageElements.networkSwitcher.networkName,
network.networkName,
);
return true;
},
async disconnectWalletFromDapp() {
await puppeteer.switchToMetamaskWindow();
await puppeteer.waitAndClick(mainPageElements.optionsMenu.button);
await puppeteer.waitAndClick(
mainPageElements.optionsMenu.connectedSitesButton,
);
await puppeteer.waitAndClick(mainPageElements.connectedSites.trashButton);
await puppeteer.waitAndClick(
mainPageElements.connectedSites.disconnectButton,
);
await puppeteer.waitAndClick(mainPageElements.connectedSites.closeButton);
await puppeteer.switchToCypressWindow();
return true;
},
async disconnectWalletFromAllDapps() {
await puppeteer.switchToMetamaskWindow();
await puppeteer.waitAndClick(mainPageElements.optionsMenu.button);
await puppeteer.waitAndClick(
mainPageElements.optionsMenu.connectedSitesButton,
);
const trashButtons = await puppeteer
.metamaskWindow()
.$$(mainPageElements.connectedSites.trashButton);
// eslint-disable-next-line no-unused-vars
for (const trashButton of trashButtons) {
await puppeteer.waitAndClick(mainPageElements.connectedSites.trashButton);
await puppeteer.waitAndClick(
mainPageElements.connectedSites.disconnectButton,
);
}
await puppeteer.waitAndClick(mainPageElements.connectedSites.closeButton);
await puppeteer.switchToCypressWindow();
return true;
},
activateCustomNonce: async () => {
await puppeteer.waitAndClick(mainPageElements.accountMenu.button);
await puppeteer.waitAndClick(mainPageElements.accountMenu.settingsButton);
await puppeteer.waitAndClick(settingsPageElements.advancedButton);
if (
(await puppeteer
.metamaskWindow()
.$(advancedPageElements.customNonceToggleOn)) === null
) {
await puppeteer.waitAndClick(advancedPageElements.customNonceToggleOff);
}
await puppeteer.waitAndClick(settingsPageElements.closeButton);
await puppeteer.waitFor(mainPageElements.walletOverview);
return true;
},
resetAccount: async () => {
await puppeteer.waitAndClick(mainPageElements.accountMenu.button);
await puppeteer.waitAndClick(mainPageElements.accountMenu.settingsButton);
await puppeteer.waitAndClick(settingsPageElements.advancedButton);
await puppeteer.waitAndClick(advancedPageElements.resetAccountButton);
await puppeteer.waitAndClick(resetAccountModalElements.resetButton);
await puppeteer.waitAndClick(settingsPageElements.closeButton);
await puppeteer.waitFor(mainPageElements.walletOverview);
return true;
},
confirmPermissionToSpend: async () => {
const notificationPage = await puppeteer.switchToMetamaskNotification();
await puppeteer.waitAndClick(
notificationPageElements.allowToSpendButton,
notificationPage,
);
await puppeteer.metamaskWindow().waitForTimeout(3000);
return true;
},
rejectPermissionToSpend: async () => {
const notificationPage = await puppeteer.switchToMetamaskNotification();
await puppeteer.waitAndClick(
notificationPageElements.rejectToSpendButton,
notificationPage,
);
await puppeteer.metamaskWindow().waitForTimeout(3000);
return true;
},
acceptAccess: async () => {
const notificationPage = await puppeteer.switchToMetamaskNotification();
await puppeteer.waitAndClick(
notificationPageElements.nextButton,
notificationPage,
);
await puppeteer.waitAndClick(
permissionsPageElements.connectButton,
notificationPage,
);
await puppeteer.metamaskWindow().waitForTimeout(3000);
return true;
},
confirmTransaction: async () => {
const isKovanTestnet = getNetwork().networkName === 'kovan';
const notificationPage = await puppeteer.switchToMetamaskNotification();
const currentGasFee = await puppeteer.waitAndGetValue(
confirmPageElements.gasFeeInput,
notificationPage,
);
const newGasFee = isKovanTestnet
? '1'
: (Number(currentGasFee) + 10).toString();
await puppeteer.waitClearAndType(
newGasFee,
confirmPageElements.gasFeeInput,
notificationPage,
);
// metamask reloads popup after changing a fee, you have to wait for this event otherwise transaction will fail
await puppeteer.metamaskWindow().waitForTimeout(3000);
await puppeteer.waitAndClick(
confirmPageElements.confirmButton,
notificationPage,
);
await puppeteer.metamaskWindow().waitForTimeout(3000);
return true;
},
rejectTransaction: async () => {
const notificationPage = await puppeteer.switchToMetamaskNotification();
await puppeteer.waitAndClick(
confirmPageElements.rejectButton,
notificationPage,
);
await puppeteer.metamaskWindow().waitForTimeout(3000);
return true;
},
getWalletAddress: async () => {
await puppeteer.waitAndClick(mainPageElements.optionsMenu.button);
await puppeteer.waitAndClick(
mainPageElements.optionsMenu.accountDetailsButton,
);
walletAddress = await puppeteer.waitAndGetValue(
mainPageElements.accountModal.walletAddressInput,
);
await puppeteer.waitAndClick(mainPageElements.accountModal.closeButton);
return walletAddress;
},
initialSetup: async ({ secretWordsOrPrivateKey, network, password }) => {
const isCustomNetwork =
process.env.NETWORK_NAME && process.env.RPC_URL && process.env.CHAIN_ID;
await puppeteer.init();
await puppeteer.assignWindows();
await puppeteer.metamaskWindow().waitForTimeout(1000);
if (
(await puppeteer.metamaskWindow().$(unlockPageElements.unlockPage)) ===
null
) {
await module.exports.confirmWelcomePage();
if (secretWordsOrPrivateKey.includes(' ')) {
await module.exports.importWallet(secretWordsOrPrivateKey, password);
} else {
await module.exports.createWallet(password);
await module.exports.importFromPrivateKey(secretWordsOrPrivateKey);
}
if (isCustomNetwork) {
await module.exports.addNetwork(network);
} else {
await module.exports.changeNetwork(network);
}
walletAddress = await module.exports.getWalletAddress();
await puppeteer.switchToCypressWindow();
return true;
} else {
await module.exports.unlock(password);
walletAddress = await module.exports.getWalletAddress();
await puppeteer.switchToCypressWindow();
return true;
}
},
};