2023-03-16 16:03:03 +01:00
|
|
|
const { strict: assert } = require('assert');
|
2021-02-04 19:15:23 +01:00
|
|
|
const path = require('path');
|
2022-08-09 20:36:32 +02:00
|
|
|
const { promises: fs } = require('fs');
|
2022-01-19 00:08:41 +01:00
|
|
|
const BigNumber = require('bignumber.js');
|
2022-02-16 15:21:41 +01:00
|
|
|
const mockttp = require('mockttp');
|
2021-02-04 19:15:23 +01:00
|
|
|
const createStaticServer = require('../../development/create-static-server');
|
2023-06-20 20:27:10 +02:00
|
|
|
const { tEn } = require('../lib/i18n-helpers');
|
2022-03-15 17:17:48 +01:00
|
|
|
const { setupMocking } = require('./mock-e2e');
|
2021-02-04 19:15:23 +01:00
|
|
|
const Ganache = require('./ganache');
|
|
|
|
const FixtureServer = require('./fixture-server');
|
2022-05-06 00:28:48 +02:00
|
|
|
const PhishingWarningPageServer = require('./phishing-warning-page-server');
|
2021-02-04 19:15:23 +01:00
|
|
|
const { buildWebDriver } = require('./webdriver');
|
2022-12-08 13:58:34 +01:00
|
|
|
const { PAGES } = require('./webdriver/driver');
|
2022-07-21 19:08:01 +02:00
|
|
|
const GanacheSeeder = require('./seeder/ganache-seeder');
|
2020-01-20 19:50:25 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const tinyDelayMs = 200;
|
|
|
|
const regularDelayMs = tinyDelayMs * 2;
|
|
|
|
const largeDelayMs = regularDelayMs * 2;
|
2022-03-09 15:38:12 +01:00
|
|
|
const veryLargeDelayMs = largeDelayMs * 2;
|
2022-03-29 17:47:45 +02:00
|
|
|
const dappBasePort = 8080;
|
2020-07-10 05:57:54 +02:00
|
|
|
|
2022-08-09 20:36:32 +02:00
|
|
|
const createDownloadFolder = async (downloadsFolder) => {
|
|
|
|
await fs.rm(downloadsFolder, { recursive: true, force: true });
|
|
|
|
await fs.mkdir(downloadsFolder, { recursive: true });
|
|
|
|
};
|
|
|
|
|
2022-01-19 00:08:41 +01:00
|
|
|
const convertToHexValue = (val) => `0x${new BigNumber(val, 10).toString(16)}`;
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
async function withFixtures(options, testSuite) {
|
2020-12-01 22:24:56 +01:00
|
|
|
const {
|
|
|
|
dapp,
|
|
|
|
fixtures,
|
|
|
|
ganacheOptions,
|
2022-07-21 19:08:01 +02:00
|
|
|
smartContract,
|
2020-12-01 22:24:56 +01:00
|
|
|
driverOptions,
|
2022-03-29 17:47:45 +02:00
|
|
|
dappOptions,
|
2020-12-01 22:24:56 +01:00
|
|
|
title,
|
2021-04-15 15:58:51 +02:00
|
|
|
failOnConsoleError = true,
|
2021-04-26 17:02:29 +02:00
|
|
|
dappPath = undefined,
|
2022-05-06 00:28:48 +02:00
|
|
|
dappPaths,
|
2022-02-22 17:48:12 +01:00
|
|
|
testSpecificMock = function () {
|
|
|
|
// do nothing.
|
|
|
|
},
|
2021-02-04 19:15:23 +01:00
|
|
|
} = options;
|
|
|
|
const fixtureServer = new FixtureServer();
|
|
|
|
const ganacheServer = new Ganache();
|
2022-03-07 20:23:04 +01:00
|
|
|
const https = await mockttp.generateCACertificate();
|
2022-03-15 17:17:48 +01:00
|
|
|
const mockServer = mockttp.getLocal({ https, cors: true });
|
2021-06-04 15:52:07 +02:00
|
|
|
let secondaryGanacheServer;
|
2022-03-29 17:47:45 +02:00
|
|
|
let numberOfDapps = dapp ? 1 : 0;
|
|
|
|
const dappServer = [];
|
2022-05-06 00:28:48 +02:00
|
|
|
const phishingPageServer = new PhishingWarningPageServer();
|
2020-01-20 19:50:25 +01:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
let webDriver;
|
2022-11-14 15:35:08 +01:00
|
|
|
let driver;
|
2021-06-16 17:12:20 +02:00
|
|
|
let failed = false;
|
2020-01-20 19:50:25 +01:00
|
|
|
try {
|
2021-02-04 19:15:23 +01:00
|
|
|
await ganacheServer.start(ganacheOptions);
|
2022-07-21 19:08:01 +02:00
|
|
|
let contractRegistry;
|
|
|
|
|
|
|
|
if (smartContract) {
|
2022-08-09 15:59:20 +02:00
|
|
|
const ganacheSeeder = new GanacheSeeder(ganacheServer.getProvider());
|
2022-07-21 19:08:01 +02:00
|
|
|
await ganacheSeeder.deploySmartContract(smartContract);
|
|
|
|
contractRegistry = ganacheSeeder.getContractRegistry();
|
|
|
|
}
|
|
|
|
|
2021-06-04 15:52:07 +02:00
|
|
|
if (ganacheOptions?.concurrent) {
|
2023-01-31 13:58:39 +01:00
|
|
|
const { port, chainId, ganacheOptions2 } = ganacheOptions.concurrent;
|
2021-06-04 15:52:07 +02:00
|
|
|
secondaryGanacheServer = new Ganache();
|
|
|
|
await secondaryGanacheServer.start({
|
|
|
|
blockTime: 2,
|
2022-01-19 00:08:41 +01:00
|
|
|
chain: { chainId },
|
2021-06-04 15:52:07 +02:00
|
|
|
port,
|
|
|
|
vmErrorsOnRPCResponse: false,
|
2023-01-31 13:58:39 +01:00
|
|
|
...ganacheOptions2,
|
2021-06-04 15:52:07 +02:00
|
|
|
});
|
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
await fixtureServer.start();
|
2023-03-13 15:31:14 +01:00
|
|
|
fixtureServer.loadJsonState(fixtures, contractRegistry);
|
2022-05-06 00:28:48 +02:00
|
|
|
await phishingPageServer.start();
|
2020-07-10 05:57:54 +02:00
|
|
|
if (dapp) {
|
2022-03-29 17:47:45 +02:00
|
|
|
if (dappOptions?.numberOfDapps) {
|
|
|
|
numberOfDapps = dappOptions.numberOfDapps;
|
|
|
|
}
|
|
|
|
for (let i = 0; i < numberOfDapps; i++) {
|
|
|
|
let dappDirectory;
|
2022-05-06 00:28:48 +02:00
|
|
|
if (dappPath || (dappPaths && dappPaths[i])) {
|
|
|
|
dappDirectory = path.resolve(__dirname, dappPath || dappPaths[i]);
|
2022-03-29 17:47:45 +02:00
|
|
|
} else {
|
|
|
|
dappDirectory = path.resolve(
|
|
|
|
__dirname,
|
|
|
|
'..',
|
|
|
|
'..',
|
|
|
|
'node_modules',
|
|
|
|
'@metamask',
|
|
|
|
'test-dapp',
|
|
|
|
'dist',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
dappServer.push(createStaticServer(dappDirectory));
|
|
|
|
dappServer[i].listen(`${dappBasePort + i}`);
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
dappServer[i].on('listening', resolve);
|
|
|
|
dappServer[i].on('error', reject);
|
|
|
|
});
|
2021-04-26 17:02:29 +02:00
|
|
|
}
|
2020-07-10 05:57:54 +02:00
|
|
|
}
|
2023-07-10 18:39:39 +02:00
|
|
|
const mockedEndpoint = await setupMocking(mockServer, testSpecificMock, {
|
|
|
|
chainId: ganacheOptions?.chainId || 1337,
|
|
|
|
});
|
2022-02-16 15:21:41 +01:00
|
|
|
await mockServer.start(8000);
|
2023-05-02 17:10:53 +02:00
|
|
|
|
2022-11-14 15:35:08 +01:00
|
|
|
driver = (await buildWebDriver(driverOptions)).driver;
|
|
|
|
webDriver = driver.driver;
|
|
|
|
|
|
|
|
if (process.env.SELENIUM_BROWSER === 'chrome') {
|
2023-02-23 15:27:36 +01:00
|
|
|
await driver.checkBrowserForExceptions(failOnConsoleError);
|
|
|
|
await driver.checkBrowserForConsoleErrors(failOnConsoleError);
|
2022-11-14 15:35:08 +01:00
|
|
|
}
|
2020-01-20 19:50:25 +01:00
|
|
|
|
2022-12-13 20:30:47 +01:00
|
|
|
let driverProxy;
|
|
|
|
if (process.env.E2E_DEBUG === 'true') {
|
|
|
|
driverProxy = new Proxy(driver, {
|
|
|
|
get(target, prop, receiver) {
|
|
|
|
const originalProperty = target[prop];
|
|
|
|
if (typeof originalProperty === 'function') {
|
|
|
|
return (...args) => {
|
|
|
|
console.log(
|
|
|
|
`[driver] Called '${prop}' with arguments ${JSON.stringify(
|
|
|
|
args,
|
|
|
|
)}`,
|
|
|
|
);
|
|
|
|
return originalProperty.bind(target)(...args);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return Reflect.get(target, prop, receiver);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-10-21 20:24:51 +02:00
|
|
|
await testSuite({
|
2022-12-13 20:30:47 +01:00
|
|
|
driver: driverProxy ?? driver,
|
2022-07-21 19:08:01 +02:00
|
|
|
contractRegistry,
|
2023-03-20 10:29:18 +01:00
|
|
|
ganacheServer,
|
|
|
|
secondaryGanacheServer,
|
2023-04-19 16:36:23 +02:00
|
|
|
mockedEndpoint,
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
2020-07-09 17:42:56 +02:00
|
|
|
} catch (error) {
|
2021-06-16 17:12:20 +02:00
|
|
|
failed = true;
|
2020-07-22 20:25:04 +02:00
|
|
|
if (webDriver) {
|
2020-12-01 22:24:56 +01:00
|
|
|
try {
|
2022-11-14 15:35:08 +01:00
|
|
|
await driver.verboseReportOnFailure(title);
|
2020-12-01 22:24:56 +01:00
|
|
|
} catch (verboseReportError) {
|
2021-02-04 19:15:23 +01:00
|
|
|
console.error(verboseReportError);
|
2020-12-01 22:24:56 +01:00
|
|
|
}
|
2023-02-23 15:27:36 +01:00
|
|
|
if (driver.errors.length > 0 || driver.exceptions.length > 0) {
|
2022-12-08 13:58:34 +01:00
|
|
|
/**
|
|
|
|
* Navigate to the background
|
|
|
|
* forcing background exceptions to be captured
|
|
|
|
* proving more helpful context
|
|
|
|
*/
|
|
|
|
await driver.navigate(PAGES.BACKGROUND);
|
2022-11-14 15:35:08 +01:00
|
|
|
}
|
2020-07-22 20:25:04 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
throw error;
|
2020-01-20 19:50:25 +01:00
|
|
|
} finally {
|
2021-06-16 17:12:20 +02:00
|
|
|
if (!failed || process.env.E2E_LEAVE_RUNNING !== 'true') {
|
|
|
|
await fixtureServer.stop();
|
|
|
|
await ganacheServer.quit();
|
|
|
|
if (ganacheOptions?.concurrent) {
|
|
|
|
await secondaryGanacheServer.quit();
|
|
|
|
}
|
|
|
|
if (webDriver) {
|
2022-11-14 15:35:08 +01:00
|
|
|
await driver.quit();
|
2021-06-16 17:12:20 +02:00
|
|
|
}
|
2022-03-29 17:47:45 +02:00
|
|
|
if (dapp) {
|
|
|
|
for (let i = 0; i < numberOfDapps; i++) {
|
|
|
|
if (dappServer[i] && dappServer[i].listening) {
|
|
|
|
await new Promise((resolve, reject) => {
|
|
|
|
dappServer[i].close((error) => {
|
|
|
|
if (error) {
|
|
|
|
return reject(error);
|
|
|
|
}
|
|
|
|
return resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2021-06-16 17:12:20 +02:00
|
|
|
}
|
2022-05-06 00:28:48 +02:00
|
|
|
if (phishingPageServer.isRunning()) {
|
|
|
|
await phishingPageServer.quit();
|
|
|
|
}
|
2022-03-07 20:23:04 +01:00
|
|
|
await mockServer.stop();
|
2020-12-01 22:24:56 +01:00
|
|
|
}
|
2020-01-20 19:50:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-19 19:34:43 +01:00
|
|
|
/**
|
|
|
|
* @param {*} driver - selinium driver
|
|
|
|
* @param {*} handlesCount - total count of windows that should be loaded
|
|
|
|
* @returns handles - an object with window handles, properties in object represent windows:
|
|
|
|
* 1. extension: metamask extension window
|
|
|
|
* 2. dapp: test-app window
|
|
|
|
* 3. popup: metsmask extension popup window
|
|
|
|
*/
|
|
|
|
const getWindowHandles = async (driver, handlesCount) => {
|
|
|
|
await driver.waitUntilXWindowHandles(handlesCount);
|
|
|
|
const windowHandles = await driver.getAllWindowHandles();
|
|
|
|
|
|
|
|
const extension = windowHandles[0];
|
|
|
|
const dapp = await driver.switchToWindowWithTitle(
|
|
|
|
'E2E Test Dapp',
|
|
|
|
windowHandles,
|
|
|
|
);
|
|
|
|
const popup = windowHandles.find(
|
|
|
|
(handle) => handle !== extension && handle !== dapp,
|
|
|
|
);
|
|
|
|
return { extension, dapp, popup };
|
|
|
|
};
|
|
|
|
|
2023-01-31 13:58:39 +01:00
|
|
|
const importSRPOnboardingFlow = async (driver, seedPhrase, password) => {
|
2023-04-14 18:51:13 +02:00
|
|
|
// agree to terms of use
|
|
|
|
await driver.clickElement('[data-testid="onboarding-terms-checkbox"]');
|
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// welcome
|
|
|
|
await driver.clickElement('[data-testid="onboarding-import-wallet"]');
|
2022-02-23 18:27:13 +01:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// metrics
|
|
|
|
await driver.clickElement('[data-testid="metametrics-no-thanks"]');
|
2022-02-23 18:27:13 +01:00
|
|
|
|
2023-06-08 15:21:21 +02:00
|
|
|
await driver.waitForSelector('.import-srp__actions');
|
2023-01-13 17:25:01 +01:00
|
|
|
// import with recovery phrase
|
|
|
|
await driver.pasteIntoField(
|
|
|
|
'[data-testid="import-srp__srp-word-0"]',
|
|
|
|
seedPhrase,
|
|
|
|
);
|
|
|
|
await driver.clickElement('[data-testid="import-srp-confirm"]');
|
2022-02-23 18:27:13 +01:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// create password
|
|
|
|
await driver.fill('[data-testid="create-password-new"]', password);
|
|
|
|
await driver.fill('[data-testid="create-password-confirm"]', password);
|
|
|
|
await driver.clickElement('[data-testid="create-password-terms"]');
|
|
|
|
await driver.clickElement('[data-testid="create-password-import"]');
|
2023-01-31 13:58:39 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const completeImportSRPOnboardingFlow = async (
|
|
|
|
driver,
|
|
|
|
seedPhrase,
|
|
|
|
password,
|
|
|
|
) => {
|
|
|
|
await importSRPOnboardingFlow(driver, seedPhrase, password);
|
2022-02-23 18:27:13 +01:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// complete
|
|
|
|
await driver.clickElement('[data-testid="onboarding-complete-done"]');
|
2022-02-23 18:27:13 +01:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// pin extension
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-next"]');
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-done"]');
|
2022-02-23 18:27:13 +01:00
|
|
|
};
|
|
|
|
|
2022-05-09 14:46:20 +02:00
|
|
|
const completeImportSRPOnboardingFlowWordByWord = async (
|
|
|
|
driver,
|
|
|
|
seedPhrase,
|
|
|
|
password,
|
|
|
|
) => {
|
2023-04-14 18:51:13 +02:00
|
|
|
// agree to terms of use
|
|
|
|
await driver.clickElement('[data-testid="onboarding-terms-checkbox"]');
|
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// welcome
|
|
|
|
await driver.clickElement('[data-testid="onboarding-import-wallet"]');
|
2022-05-09 14:46:20 +02:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// metrics
|
|
|
|
await driver.clickElement('[data-testid="metametrics-no-thanks"]');
|
2022-07-22 22:39:48 +02:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// import with recovery phrase, word by word
|
2022-05-09 14:46:20 +02:00
|
|
|
const words = seedPhrase.split(' ');
|
|
|
|
for (const word of words) {
|
|
|
|
await driver.pasteIntoField(
|
|
|
|
`[data-testid="import-srp__srp-word-${words.indexOf(word)}"]`,
|
|
|
|
word,
|
|
|
|
);
|
|
|
|
}
|
2023-01-13 17:25:01 +01:00
|
|
|
await driver.clickElement('[data-testid="import-srp-confirm"]');
|
2022-05-09 14:46:20 +02:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// create password
|
|
|
|
await driver.fill('[data-testid="create-password-new"]', password);
|
|
|
|
await driver.fill('[data-testid="create-password-confirm"]', password);
|
|
|
|
await driver.clickElement('[data-testid="create-password-terms"]');
|
|
|
|
await driver.clickElement('[data-testid="create-password-import"]');
|
2022-05-09 14:46:20 +02:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// complete
|
|
|
|
await driver.clickElement('[data-testid="onboarding-complete-done"]');
|
2022-05-09 14:46:20 +02:00
|
|
|
|
2023-01-13 17:25:01 +01:00
|
|
|
// pin extension
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-next"]');
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-done"]');
|
2022-05-09 14:46:20 +02:00
|
|
|
};
|
|
|
|
|
2023-06-29 18:01:54 +02:00
|
|
|
/**
|
|
|
|
* Begin the create new wallet flow on onboarding screen.
|
|
|
|
*
|
|
|
|
* @param {WebDriver} driver
|
|
|
|
*/
|
|
|
|
const onboardingBeginCreateNewWallet = async (driver) => {
|
2023-04-14 18:51:13 +02:00
|
|
|
// agree to terms of use
|
|
|
|
await driver.clickElement('[data-testid="onboarding-terms-checkbox"]');
|
|
|
|
|
2023-03-16 16:03:03 +01:00
|
|
|
// welcome
|
|
|
|
await driver.clickElement('[data-testid="onboarding-create-wallet"]');
|
2023-06-29 18:01:54 +02:00
|
|
|
};
|
2023-03-16 16:03:03 +01:00
|
|
|
|
2023-06-29 18:01:54 +02:00
|
|
|
/**
|
|
|
|
* Choose either "I Agree" or "No Thanks" on the MetaMetrics onboarding screen
|
|
|
|
*
|
|
|
|
* @param {WebDriver} driver
|
|
|
|
* @param {boolean} optin - true to opt into metrics, default is false
|
|
|
|
*/
|
|
|
|
const onboardingChooseMetametricsOption = async (driver, optin = false) => {
|
|
|
|
const optionIdentifier = optin ? 'i-agree' : 'no-thanks';
|
2023-03-16 16:03:03 +01:00
|
|
|
// metrics
|
2023-06-29 18:01:54 +02:00
|
|
|
await driver.clickElement(`[data-testid="metametrics-${optionIdentifier}"]`);
|
|
|
|
};
|
2023-03-16 16:03:03 +01:00
|
|
|
|
2023-06-29 18:01:54 +02:00
|
|
|
/**
|
|
|
|
* Set a password for MetaMask during onboarding
|
|
|
|
*
|
|
|
|
* @param {WebDriver} driver
|
|
|
|
* @param {string} password - Password to set
|
|
|
|
*/
|
|
|
|
const onboardingCreatePassword = async (driver, password) => {
|
2023-03-16 16:03:03 +01:00
|
|
|
// create password
|
|
|
|
await driver.fill('[data-testid="create-password-new"]', password);
|
|
|
|
await driver.fill('[data-testid="create-password-confirm"]', password);
|
|
|
|
await driver.clickElement('[data-testid="create-password-terms"]');
|
|
|
|
await driver.clickElement('[data-testid="create-password-wallet"]');
|
2023-06-29 18:01:54 +02:00
|
|
|
};
|
2023-03-16 16:03:03 +01:00
|
|
|
|
2023-06-29 18:01:54 +02:00
|
|
|
/**
|
|
|
|
* Choose to secure wallet, and then get recovery phrase and confirm the SRP
|
|
|
|
* during onboarding flow.
|
|
|
|
*
|
|
|
|
* @param {WebDriver} driver
|
|
|
|
*/
|
|
|
|
const onboardingRevealAndConfirmSRP = async (driver) => {
|
2023-03-16 16:03:03 +01:00
|
|
|
// secure my wallet
|
|
|
|
await driver.clickElement('[data-testid="secure-wallet-recommended"]');
|
|
|
|
|
|
|
|
// reveal SRP
|
|
|
|
await driver.clickElement('[data-testid="recovery-phrase-reveal"]');
|
|
|
|
|
|
|
|
const revealedSeedPhrase = await driver.findElement(
|
|
|
|
'[data-testid="recovery-phrase-chips"]',
|
|
|
|
);
|
|
|
|
|
|
|
|
const recoveryPhrase = await revealedSeedPhrase.getText();
|
|
|
|
|
|
|
|
await driver.clickElement('[data-testid="recovery-phrase-next"]');
|
|
|
|
|
|
|
|
// confirm SRP
|
|
|
|
const words = recoveryPhrase.split(/\s*(?:[0-9)]+|\n|\.|^$|$)\s*/u);
|
|
|
|
const finalWords = words.filter((str) => str !== '');
|
|
|
|
assert.equal(finalWords.length, 12);
|
|
|
|
|
|
|
|
await driver.fill('[data-testid="recovery-phrase-input-2"]', finalWords[2]);
|
|
|
|
await driver.fill('[data-testid="recovery-phrase-input-3"]', finalWords[3]);
|
|
|
|
await driver.fill('[data-testid="recovery-phrase-input-7"]', finalWords[7]);
|
|
|
|
|
|
|
|
await driver.clickElement('[data-testid="confirm-recovery-phrase"]');
|
|
|
|
|
|
|
|
await driver.clickElement({ text: 'Confirm', tag: 'button' });
|
2023-06-29 18:01:54 +02:00
|
|
|
};
|
2023-03-16 16:03:03 +01:00
|
|
|
|
2023-06-29 18:01:54 +02:00
|
|
|
/**
|
|
|
|
* Complete the onboarding flow by confirming completion. Final step before the
|
|
|
|
* reminder to pin the extension.
|
|
|
|
*
|
|
|
|
* @param {WebDriver} driver
|
|
|
|
*/
|
|
|
|
const onboardingCompleteWalletCreation = async (driver) => {
|
2023-03-16 16:03:03 +01:00
|
|
|
// complete
|
|
|
|
await driver.findElement({ text: 'Wallet creation successful', tag: 'h2' });
|
|
|
|
await driver.clickElement('[data-testid="onboarding-complete-done"]');
|
2023-06-29 18:01:54 +02:00
|
|
|
};
|
2023-03-16 16:03:03 +01:00
|
|
|
|
2023-06-29 18:01:54 +02:00
|
|
|
/**
|
|
|
|
* Move through the steps of pinning extension after successful onboarding
|
|
|
|
*
|
|
|
|
* @param {WebDriver} driver
|
|
|
|
*/
|
|
|
|
const onboardingPinExtension = async (driver) => {
|
2023-03-16 16:03:03 +01:00
|
|
|
// pin extension
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-next"]');
|
|
|
|
await driver.clickElement('[data-testid="pin-extension-done"]');
|
|
|
|
};
|
|
|
|
|
2023-06-29 18:01:54 +02:00
|
|
|
const completeCreateNewWalletOnboardingFlow = async (driver, password) => {
|
|
|
|
await onboardingBeginCreateNewWallet(driver);
|
|
|
|
await onboardingChooseMetametricsOption(driver, false);
|
|
|
|
await onboardingCreatePassword(driver, password);
|
|
|
|
await onboardingRevealAndConfirmSRP(driver);
|
|
|
|
await onboardingCompleteWalletCreation(driver);
|
|
|
|
await onboardingPinExtension(driver);
|
|
|
|
};
|
|
|
|
|
2023-03-16 16:03:03 +01:00
|
|
|
const importWrongSRPOnboardingFlow = async (driver, seedPhrase) => {
|
2023-04-14 18:51:13 +02:00
|
|
|
// agree to terms of use
|
|
|
|
await driver.clickElement('[data-testid="onboarding-terms-checkbox"]');
|
|
|
|
|
2023-03-16 16:03:03 +01:00
|
|
|
// welcome
|
|
|
|
await driver.clickElement('[data-testid="onboarding-import-wallet"]');
|
|
|
|
|
|
|
|
// metrics
|
|
|
|
await driver.clickElement('[data-testid="metametrics-no-thanks"]');
|
|
|
|
|
|
|
|
// import with recovery phrase
|
|
|
|
await driver.pasteIntoField(
|
|
|
|
'[data-testid="import-srp__srp-word-0"]',
|
|
|
|
seedPhrase,
|
|
|
|
);
|
|
|
|
|
|
|
|
const warningText = 'Invalid Secret Recovery Phrase';
|
|
|
|
const warnings = await driver.findElements('.actionable-message__message');
|
|
|
|
const warning = warnings[1];
|
|
|
|
|
|
|
|
assert.equal(await warning.getText(), warningText);
|
|
|
|
};
|
|
|
|
|
|
|
|
const selectDropdownByNum = async (elements, index) => {
|
|
|
|
await elements[index].click();
|
|
|
|
};
|
|
|
|
|
|
|
|
const testSRPDropdownIterations = async (options, driver, iterations) => {
|
|
|
|
for (let i = 0; i < iterations; i++) {
|
|
|
|
await selectDropdownByNum(options, i);
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
|
|
|
|
|
|
const formFields = await driver.findElements('.import-srp__srp-word-label');
|
|
|
|
const expectedNumFields = 12 + i * 3;
|
|
|
|
const actualNumFields = formFields.length;
|
|
|
|
assert.equal(actualNumFields, expectedNumFields);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-06-20 20:27:10 +02:00
|
|
|
const passwordUnlockOpenSRPRevealQuiz = async (driver) => {
|
|
|
|
await driver.navigate();
|
|
|
|
await driver.fill('#password', 'correct horse battery staple');
|
|
|
|
await driver.press('#password', driver.Key.ENTER);
|
|
|
|
|
|
|
|
// navigate settings to reveal SRP
|
|
|
|
await driver.clickElement('[data-testid="account-options-menu-button"]');
|
|
|
|
await driver.clickElement({ text: 'Settings', tag: 'div' });
|
|
|
|
await driver.clickElement({ text: 'Security & privacy', tag: 'div' });
|
|
|
|
await driver.clickElement('[data-testid="reveal-seed-words"]');
|
|
|
|
};
|
|
|
|
|
|
|
|
const completeSRPRevealQuiz = async (driver) => {
|
|
|
|
// start quiz
|
|
|
|
await driver.clickElement('[data-testid="srp-quiz-get-started"]');
|
|
|
|
|
|
|
|
// tap correct answer 1
|
|
|
|
await driver.clickElement('[data-testid="srp-quiz-right-answer"]');
|
|
|
|
|
|
|
|
// tap Continue 1
|
|
|
|
await driver.clickElement('[data-testid="srp-quiz-continue"]');
|
|
|
|
|
|
|
|
// tap correct answer 2
|
|
|
|
await driver.clickElement('[data-testid="srp-quiz-right-answer"]');
|
|
|
|
|
|
|
|
// tap Continue 2
|
|
|
|
await driver.clickElement('[data-testid="srp-quiz-continue"]');
|
|
|
|
};
|
|
|
|
|
|
|
|
const tapAndHoldToRevealSRP = async (driver) => {
|
|
|
|
await driver.holdMouseDownOnElement(
|
|
|
|
{
|
|
|
|
text: tEn('holdToRevealSRP'),
|
|
|
|
tag: 'span',
|
|
|
|
},
|
|
|
|
2000,
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const closeSRPReveal = async (driver) => {
|
|
|
|
await driver.clickElement({
|
|
|
|
text: tEn('close'),
|
|
|
|
tag: 'button',
|
|
|
|
});
|
|
|
|
await driver.findVisibleElement({
|
|
|
|
text: tEn('tokens'),
|
|
|
|
tag: 'button',
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-05-05 15:56:08 +02:00
|
|
|
const DAPP_URL = 'http://127.0.0.1:8080';
|
|
|
|
const DAPP_ONE_URL = 'http://127.0.0.1:8081';
|
|
|
|
|
|
|
|
const openDapp = async (driver, contract = null, dappURL = DAPP_URL) => {
|
|
|
|
contract
|
|
|
|
? await driver.openNewPage(`${dappURL}/?contract=${contract}`)
|
|
|
|
: await driver.openNewPage(dappURL);
|
|
|
|
};
|
2023-05-19 12:17:53 +02:00
|
|
|
|
|
|
|
const PRIVATE_KEY =
|
|
|
|
'0x7C9529A67102755B7E6102D6D950AC5D5863C98713805CEC576B945B15B71EAC';
|
|
|
|
|
|
|
|
const generateETHBalance = (eth) => convertToHexValue(eth * 10 ** 18);
|
|
|
|
const defaultGanacheOptions = {
|
|
|
|
accounts: [{ secretKey: PRIVATE_KEY, balance: generateETHBalance(25) }],
|
|
|
|
};
|
|
|
|
|
|
|
|
const SERVICE_WORKER_URL = 'chrome://inspect/#service-workers';
|
2023-05-05 15:56:08 +02:00
|
|
|
|
2023-05-24 19:50:31 +02:00
|
|
|
const sendTransaction = async (driver, recipientAddress, quantity) => {
|
|
|
|
await driver.clickElement('[data-testid="eth-overview-send"]');
|
|
|
|
await driver.fill('[data-testid="ens-input"]', recipientAddress);
|
|
|
|
await driver.fill('.unit-input__input', quantity);
|
|
|
|
await driver.clickElement('[data-testid="page-container-footer-next"]');
|
|
|
|
await driver.clickElement('[data-testid="page-container-footer-next"]');
|
|
|
|
await driver.clickElement('[data-testid="home__activity-tab"]');
|
2023-06-08 15:21:21 +02:00
|
|
|
await driver.waitForElementNotPresent('.transaction-list-item--unconfirmed');
|
2023-05-24 19:50:31 +02:00
|
|
|
await driver.findElement('.transaction-list-item');
|
|
|
|
};
|
|
|
|
|
|
|
|
const findAnotherAccountFromAccountList = async (
|
|
|
|
driver,
|
|
|
|
itemNumber,
|
|
|
|
accountName,
|
|
|
|
) => {
|
2023-06-01 23:14:38 +02:00
|
|
|
await driver.clickElement('[data-testid="account-menu-icon"]');
|
|
|
|
const accountMenuItemSelector = `.multichain-account-list-item:nth-child(${itemNumber})`;
|
2023-06-20 15:45:40 +02:00
|
|
|
|
|
|
|
await driver.findElement({
|
|
|
|
css: `${accountMenuItemSelector} .multichain-account-list-item__account-name__button`,
|
|
|
|
text: accountName,
|
|
|
|
});
|
|
|
|
|
2023-05-24 19:50:31 +02:00
|
|
|
return accountMenuItemSelector;
|
|
|
|
};
|
|
|
|
|
2023-05-26 15:05:34 +02:00
|
|
|
const TEST_SEED_PHRASE =
|
|
|
|
'forum vessel pink push lonely enact gentle tail admit parrot grunt dress';
|
|
|
|
|
|
|
|
const TEST_SEED_PHRASE_TWO =
|
|
|
|
'phrase upgrade clock rough situate wedding elder clever doctor stamp excess tent';
|
|
|
|
|
2023-05-30 14:44:56 +02:00
|
|
|
// Usually happens when onboarded to make sure the state is retrieved from metamaskState properly
|
|
|
|
const assertAccountBalanceForDOM = async (driver, ganacheServer) => {
|
|
|
|
const balance = await ganacheServer.getBalance();
|
|
|
|
const balanceElement = await driver.findElement(
|
|
|
|
'[data-testid="eth-overview__primary-currency"]',
|
|
|
|
);
|
|
|
|
assert.equal(`${balance}\nETH`, await balanceElement.getText());
|
|
|
|
};
|
|
|
|
|
|
|
|
// Usually happens after txn is made
|
|
|
|
const locateAccountBalanceDOM = async (driver, ganacheServer) => {
|
|
|
|
const balance = await ganacheServer.getBalance();
|
|
|
|
await driver.waitForSelector({
|
|
|
|
css: '[data-testid="eth-overview__primary-currency"]',
|
|
|
|
text: `${balance} ETH`,
|
|
|
|
});
|
|
|
|
};
|
2023-06-20 12:17:08 +02:00
|
|
|
const DEFAULT_PRIVATE_KEY =
|
|
|
|
'0x7C9529A67102755B7E6102D6D950AC5D5863C98713805CEC576B945B15B71EAC';
|
|
|
|
const WALLET_PASSWORD = 'correct horse battery staple';
|
2023-05-30 14:44:56 +02:00
|
|
|
|
2023-06-20 12:17:08 +02:00
|
|
|
const DEFAULT_GANACHE_OPTIONS = {
|
|
|
|
accounts: [
|
|
|
|
{
|
|
|
|
secretKey: DEFAULT_PRIVATE_KEY,
|
|
|
|
balance: generateETHBalance(25),
|
|
|
|
},
|
|
|
|
],
|
2023-06-08 15:21:21 +02:00
|
|
|
};
|
|
|
|
|
2023-06-20 12:17:08 +02:00
|
|
|
const generateGanacheOptions = (overrides) => ({
|
|
|
|
...DEFAULT_GANACHE_OPTIONS,
|
|
|
|
...overrides,
|
|
|
|
});
|
|
|
|
|
2023-06-08 15:21:21 +02:00
|
|
|
async function waitForAccountRendered(driver) {
|
|
|
|
await driver.waitForSelector(
|
|
|
|
'[data-testid="eth-overview__primary-currency"]',
|
|
|
|
);
|
|
|
|
}
|
2023-06-20 12:17:08 +02:00
|
|
|
const WINDOW_TITLES = Object.freeze({
|
|
|
|
ExtensionInFullScreenView: 'MetaMask',
|
|
|
|
TestDApp: 'E2E Test Dapp',
|
|
|
|
Notification: 'MetaMask Notification',
|
|
|
|
ServiceWorkerSettings: 'Inspect with Chrome Developer Tools',
|
|
|
|
InstalledExtensions: 'Extensions',
|
|
|
|
});
|
|
|
|
|
|
|
|
const unlockWallet = async (driver) => {
|
2023-06-09 16:01:12 +02:00
|
|
|
await driver.fill('#password', 'correct horse battery staple');
|
|
|
|
await driver.press('#password', driver.Key.ENTER);
|
2023-06-15 14:13:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const logInWithBalanceValidation = async (driver, ganacheServer) => {
|
2023-06-20 12:17:08 +02:00
|
|
|
await unlockWallet(driver);
|
2023-06-09 16:01:12 +02:00
|
|
|
await assertAccountBalanceForDOM(driver, ganacheServer);
|
|
|
|
};
|
|
|
|
|
2023-06-20 12:17:08 +02:00
|
|
|
function roundToXDecimalPlaces(number, decimalPlaces) {
|
|
|
|
return Math.round(number * 10 ** decimalPlaces) / 10 ** decimalPlaces;
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateRandNumBetween(x, y) {
|
|
|
|
const min = Math.min(x, y);
|
|
|
|
const max = Math.max(x, y);
|
|
|
|
const randomNumber = Math.random() * (max - min) + min;
|
|
|
|
|
|
|
|
return randomNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function sleepSeconds(sec) {
|
|
|
|
return new Promise((resolve) => setTimeout(resolve, sec * 1000));
|
|
|
|
}
|
|
|
|
|
|
|
|
async function terminateServiceWorker(driver) {
|
|
|
|
await driver.openNewPage(SERVICE_WORKER_URL);
|
|
|
|
|
|
|
|
await driver.waitForSelector({
|
|
|
|
text: 'Service workers',
|
|
|
|
tag: 'button',
|
|
|
|
});
|
|
|
|
await driver.clickElement({
|
|
|
|
text: 'Service workers',
|
|
|
|
tag: 'button',
|
|
|
|
});
|
|
|
|
|
2023-07-12 20:27:44 +02:00
|
|
|
await driver.delay(tinyDelayMs);
|
|
|
|
const serviceWorkerElements = await driver.findClickableElements({
|
2023-06-20 12:17:08 +02:00
|
|
|
text: 'terminate',
|
|
|
|
tag: 'span',
|
|
|
|
});
|
|
|
|
|
|
|
|
// 1st one is app-init.js; while 2nd one is service-worker.js
|
|
|
|
await serviceWorkerElements[serviceWorkerElements.length - 1].click();
|
|
|
|
|
2023-07-12 20:27:44 +02:00
|
|
|
const serviceWorkerTab = await driver.switchToWindowWithTitle(
|
2023-06-20 12:17:08 +02:00
|
|
|
WINDOW_TITLES.ServiceWorkerSettings,
|
|
|
|
);
|
|
|
|
|
|
|
|
await driver.closeWindowHandle(serviceWorkerTab);
|
|
|
|
}
|
|
|
|
|
2023-06-23 16:48:44 +02:00
|
|
|
/**
|
|
|
|
* This method assumes the extension is open, the dapp is open and waits for a
|
|
|
|
* third window handle to open (the notification window). Once it does it
|
|
|
|
* switches to the new window.
|
|
|
|
*
|
|
|
|
* @param {WebDriver} driver
|
|
|
|
*/
|
|
|
|
async function switchToNotificationWindow(driver) {
|
|
|
|
await driver.waitUntilXWindowHandles(3);
|
|
|
|
const windowHandles = await driver.getAllWindowHandles();
|
|
|
|
await driver.switchToWindowWithTitle('MetaMask Notification', windowHandles);
|
|
|
|
}
|
|
|
|
|
2023-06-29 18:01:54 +02:00
|
|
|
/**
|
|
|
|
* When mocking the segment server and returning an array of mocks from the
|
|
|
|
* mockServer method, this method will allow getting all of the seen requests
|
|
|
|
* for each mock in the array.
|
|
|
|
*
|
|
|
|
* @param {WebDriver} driver
|
|
|
|
* @param {import('mockttp').Mockttp} mockedEndpoints
|
|
|
|
* @returns {import('mockttp/dist/pluggable-admin').MockttpClientResponse[]}
|
|
|
|
*/
|
|
|
|
async function getEventPayloads(driver, mockedEndpoints) {
|
|
|
|
await driver.wait(async () => {
|
|
|
|
let isPending = true;
|
|
|
|
for (const mockedEndpoint of mockedEndpoints) {
|
|
|
|
isPending = await mockedEndpoint.isPending();
|
|
|
|
}
|
|
|
|
return isPending === false;
|
|
|
|
}, 10000);
|
|
|
|
const mockedRequests = [];
|
|
|
|
for (const mockedEndpoint of mockedEndpoints) {
|
|
|
|
mockedRequests.push(...(await mockedEndpoint.getSeenRequests()));
|
|
|
|
}
|
|
|
|
return mockedRequests.map((req) => req.body.json.batch).flat();
|
|
|
|
}
|
|
|
|
|
2018-05-25 03:17:26 +02:00
|
|
|
module.exports = {
|
2023-05-05 15:56:08 +02:00
|
|
|
DAPP_URL,
|
|
|
|
DAPP_ONE_URL,
|
2023-05-19 12:17:53 +02:00
|
|
|
SERVICE_WORKER_URL,
|
2023-05-26 15:05:34 +02:00
|
|
|
TEST_SEED_PHRASE,
|
|
|
|
TEST_SEED_PHRASE_TWO,
|
2023-06-08 15:21:21 +02:00
|
|
|
PRIVATE_KEY,
|
2022-01-19 19:34:43 +01:00
|
|
|
getWindowHandles,
|
2022-01-19 00:08:41 +01:00
|
|
|
convertToHexValue,
|
2019-09-07 09:13:58 +02:00
|
|
|
tinyDelayMs,
|
|
|
|
regularDelayMs,
|
|
|
|
largeDelayMs,
|
2022-03-09 15:38:12 +01:00
|
|
|
veryLargeDelayMs,
|
2020-01-20 19:50:25 +01:00
|
|
|
withFixtures,
|
2023-01-31 13:58:39 +01:00
|
|
|
importSRPOnboardingFlow,
|
2022-02-23 18:27:13 +01:00
|
|
|
completeImportSRPOnboardingFlow,
|
2022-05-09 14:46:20 +02:00
|
|
|
completeImportSRPOnboardingFlowWordByWord,
|
2023-03-16 16:03:03 +01:00
|
|
|
completeCreateNewWalletOnboardingFlow,
|
2023-06-20 20:27:10 +02:00
|
|
|
passwordUnlockOpenSRPRevealQuiz,
|
|
|
|
completeSRPRevealQuiz,
|
|
|
|
closeSRPReveal,
|
|
|
|
tapAndHoldToRevealSRP,
|
2022-08-09 20:36:32 +02:00
|
|
|
createDownloadFolder,
|
2023-03-16 16:03:03 +01:00
|
|
|
importWrongSRPOnboardingFlow,
|
|
|
|
testSRPDropdownIterations,
|
2023-05-05 15:56:08 +02:00
|
|
|
openDapp,
|
2023-05-19 12:17:53 +02:00
|
|
|
defaultGanacheOptions,
|
2023-05-24 19:50:31 +02:00
|
|
|
sendTransaction,
|
|
|
|
findAnotherAccountFromAccountList,
|
2023-06-20 12:17:08 +02:00
|
|
|
unlockWallet,
|
2023-06-09 16:01:12 +02:00
|
|
|
logInWithBalanceValidation,
|
2023-05-30 14:44:56 +02:00
|
|
|
assertAccountBalanceForDOM,
|
|
|
|
locateAccountBalanceDOM,
|
2023-06-08 15:21:21 +02:00
|
|
|
waitForAccountRendered,
|
2023-06-20 12:17:08 +02:00
|
|
|
generateGanacheOptions,
|
|
|
|
WALLET_PASSWORD,
|
|
|
|
WINDOW_TITLES,
|
|
|
|
DEFAULT_GANACHE_OPTIONS,
|
|
|
|
generateETHBalance,
|
|
|
|
roundToXDecimalPlaces,
|
|
|
|
generateRandNumBetween,
|
|
|
|
sleepSeconds,
|
|
|
|
terminateServiceWorker,
|
2023-06-23 16:48:44 +02:00
|
|
|
switchToNotificationWindow,
|
2023-06-29 18:01:54 +02:00
|
|
|
getEventPayloads,
|
|
|
|
onboardingBeginCreateNewWallet,
|
|
|
|
onboardingChooseMetametricsOption,
|
|
|
|
onboardingCreatePassword,
|
|
|
|
onboardingRevealAndConfirmSRP,
|
|
|
|
onboardingCompleteWalletCreation,
|
|
|
|
onboardingPinExtension,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|