1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00
metamask-extension/test/e2e/helpers.js

305 lines
9.2 KiB
JavaScript
Raw Normal View History

const path = require('path');
3box Replacement (#15243) * Backup user data Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Tests for prependZero (utils.js) Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Fix advancedtab test Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> backup controller tests Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Backup controller don't have a store. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Restore from file. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Advanced Tab tests Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fix Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> e2e tests for backup unit tests for restore. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Fix comments on PR. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> restore style Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> We should move the exportAsFile to a utility file in the shared/ directory Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Move export as file to shared folder Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Refactor create download folder methods Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Move the backup/restore buttons closer to 3box Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Change descriptions Add to search Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> refactor code to use if instead of && Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Restore button should change cursor to pointer. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Fix restore not uploading same file twice. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Do not backup these items in preferences identities lostIdentities selectedAddress Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Only update what is needed. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Fixed test for search Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * remove txError as it currently does nothing. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Remove dispatch, not needed since we're not dispatching any actions. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Event should be title case. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Make backup/restore normal async functions rename event as per product suggestion. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Use success Actionable message for success message and danger for error message Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * change event name to match with backup Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * fix e2e Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>
2022-08-09 20:36:32 +02:00
const { promises: fs } = require('fs');
const BigNumber = require('bignumber.js');
const mockttp = require('mockttp');
const createStaticServer = require('../../development/create-static-server');
const { setupMocking } = require('./mock-e2e');
const Ganache = require('./ganache');
const FixtureServer = require('./fixture-server');
2022-05-06 00:28:48 +02:00
const PhishingWarningPageServer = require('./phishing-warning-page-server');
const { buildWebDriver } = require('./webdriver');
const { PAGES } = require('./webdriver/driver');
Fix 'yarn setup' on M1 Macs (#11887) There are a few issues encountered when running `yarn setup` on new Apple Silicon (aka M1, aka arm64) Macs: * The script halts when attempting to run the install step for the `chromedriver` package with the message "Only Mac 64 bits supported". This is somewhat misleading as it seems to indicate that chromedriver can only be installed on a 64-bit Mac. However, what I think is happening is that the installation script for `chromedriver` is not able to detect that an arm64 CPU *is* a 64-bit CPU. After looking through the `chromedriver` repo, it appears that 87.0.1 is the first version that adds a proper check ([1]). Note that upgrading chromedriver caused the Chrome-specific tests to fail intermittently on CI. I was not able to 100% work out the reason for this, but ensuring that X (which provides a way for Chrome to run in a GUI setting from the command line) is available seems to fix these issues. * The script also halts when attempting to run the install step for the `electron` package. This happens because for the version of `electron` we are using (9.4.2), there is no available binary for arm64. It appears that Electron 11.x was the first version to support arm64 Macs ([2]). This is a bit trickier to resolve because we don't explicitly rely on `electron` — that's brought in by `react-devtools`. The first version of `react-devtools` that relies on `electron` 11.x is 4.11.0 ([3]). [1]: https://github.com/giggio/node-chromedriver/commit/469dd0a6ee23540bfa832b5f09b4cbde3e152010 [2]: https://www.electronjs.org/blog/apple-silicon [3]: https://github.com/facebook/react/blob/main/packages/react-devtools/CHANGELOG.md#4110-april-9-2021
2021-09-01 18:40:40 +02:00
const { ensureXServerIsRunning } = require('./x-server');
const GanacheSeeder = require('./seeder/ganache-seeder');
const tinyDelayMs = 200;
const regularDelayMs = tinyDelayMs * 2;
const largeDelayMs = regularDelayMs * 2;
const veryLargeDelayMs = largeDelayMs * 2;
const dappBasePort = 8080;
3box Replacement (#15243) * Backup user data Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Tests for prependZero (utils.js) Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Fix advancedtab test Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> backup controller tests Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Backup controller don't have a store. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Restore from file. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Advanced Tab tests Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fix Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> e2e tests for backup unit tests for restore. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Fix comments on PR. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> restore style Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> We should move the exportAsFile to a utility file in the shared/ directory Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Move export as file to shared folder Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Refactor create download folder methods Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Move the backup/restore buttons closer to 3box Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Change descriptions Add to search Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> refactor code to use if instead of && Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Restore button should change cursor to pointer. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Fix restore not uploading same file twice. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Do not backup these items in preferences identities lostIdentities selectedAddress Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Only update what is needed. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Fixed test for search Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * remove txError as it currently does nothing. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Remove dispatch, not needed since we're not dispatching any actions. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Event should be title case. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Make backup/restore normal async functions rename event as per product suggestion. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Use success Actionable message for success message and danger for error message Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * change event name to match with backup Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * fix e2e Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>
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 });
};
const convertToHexValue = (val) => `0x${new BigNumber(val, 10).toString(16)}`;
2020-11-03 00:41:28 +01:00
async function withFixtures(options, testSuite) {
const {
dapp,
fixtures,
ganacheOptions,
smartContract,
driverOptions,
dappOptions,
title,
failOnConsoleError = true,
dappPath = undefined,
2022-05-06 00:28:48 +02:00
dappPaths,
testSpecificMock = function () {
// do nothing.
},
} = options;
const fixtureServer = new FixtureServer();
const ganacheServer = new Ganache();
const https = await mockttp.generateCACertificate();
const mockServer = mockttp.getLocal({ https, cors: true });
let secondaryGanacheServer;
let numberOfDapps = dapp ? 1 : 0;
const dappServer = [];
2022-05-06 00:28:48 +02:00
const phishingPageServer = new PhishingWarningPageServer();
let webDriver;
let driver;
let failed = false;
try {
await ganacheServer.start(ganacheOptions);
let contractRegistry;
if (smartContract) {
const ganacheSeeder = new GanacheSeeder(ganacheServer.getProvider());
await ganacheSeeder.deploySmartContract(smartContract);
contractRegistry = ganacheSeeder.getContractRegistry();
}
if (ganacheOptions?.concurrent) {
const { port, chainId, ganacheOptions2 } = ganacheOptions.concurrent;
secondaryGanacheServer = new Ganache();
await secondaryGanacheServer.start({
blockTime: 2,
chain: { chainId },
port,
vmErrorsOnRPCResponse: false,
...ganacheOptions2,
});
}
await fixtureServer.start();
fixtureServer.loadJsonState(fixtures);
2022-05-06 00:28:48 +02:00
await phishingPageServer.start();
if (dapp) {
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]);
} 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);
});
}
}
await setupMocking(mockServer, testSpecificMock);
await mockServer.start(8000);
if (
process.env.SELENIUM_BROWSER === 'chrome' &&
process.env.CI === 'true'
) {
Fix 'yarn setup' on M1 Macs (#11887) There are a few issues encountered when running `yarn setup` on new Apple Silicon (aka M1, aka arm64) Macs: * The script halts when attempting to run the install step for the `chromedriver` package with the message "Only Mac 64 bits supported". This is somewhat misleading as it seems to indicate that chromedriver can only be installed on a 64-bit Mac. However, what I think is happening is that the installation script for `chromedriver` is not able to detect that an arm64 CPU *is* a 64-bit CPU. After looking through the `chromedriver` repo, it appears that 87.0.1 is the first version that adds a proper check ([1]). Note that upgrading chromedriver caused the Chrome-specific tests to fail intermittently on CI. I was not able to 100% work out the reason for this, but ensuring that X (which provides a way for Chrome to run in a GUI setting from the command line) is available seems to fix these issues. * The script also halts when attempting to run the install step for the `electron` package. This happens because for the version of `electron` we are using (9.4.2), there is no available binary for arm64. It appears that Electron 11.x was the first version to support arm64 Macs ([2]). This is a bit trickier to resolve because we don't explicitly rely on `electron` — that's brought in by `react-devtools`. The first version of `react-devtools` that relies on `electron` 11.x is 4.11.0 ([3]). [1]: https://github.com/giggio/node-chromedriver/commit/469dd0a6ee23540bfa832b5f09b4cbde3e152010 [2]: https://www.electronjs.org/blog/apple-silicon [3]: https://github.com/facebook/react/blob/main/packages/react-devtools/CHANGELOG.md#4110-april-9-2021
2021-09-01 18:40:40 +02:00
await ensureXServerIsRunning();
}
driver = (await buildWebDriver(driverOptions)).driver;
webDriver = driver.driver;
if (process.env.SELENIUM_BROWSER === 'chrome') {
await driver.checkBrowserForExceptions(failOnConsoleError);
await driver.checkBrowserForConsoleErrors(failOnConsoleError);
}
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);
},
});
}
await testSuite({
driver: driverProxy ?? driver,
mockServer,
contractRegistry,
});
} catch (error) {
failed = true;
if (webDriver) {
try {
await driver.verboseReportOnFailure(title);
} catch (verboseReportError) {
console.error(verboseReportError);
}
if (driver.errors.length > 0 || driver.exceptions.length > 0) {
/**
* Navigate to the background
* forcing background exceptions to be captured
* proving more helpful context
*/
await driver.navigate(PAGES.BACKGROUND);
}
}
throw error;
} finally {
if (!failed || process.env.E2E_LEAVE_RUNNING !== 'true') {
await fixtureServer.stop();
await ganacheServer.quit();
if (ganacheOptions?.concurrent) {
await secondaryGanacheServer.quit();
}
if (webDriver) {
await driver.quit();
}
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();
});
});
}
}
}
2022-05-06 00:28:48 +02:00
if (phishingPageServer.isRunning()) {
await phishingPageServer.quit();
}
await mockServer.stop();
}
}
}
/**
* @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 };
};
const importSRPOnboardingFlow = async (driver, seedPhrase, password) => {
// 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,
);
await driver.clickElement('[data-testid="import-srp-confirm"]');
// 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"]');
};
const completeImportSRPOnboardingFlow = async (
driver,
seedPhrase,
password,
) => {
await importSRPOnboardingFlow(driver, seedPhrase, password);
// complete
await driver.clickElement('[data-testid="onboarding-complete-done"]');
// pin extension
await driver.clickElement('[data-testid="pin-extension-next"]');
await driver.clickElement('[data-testid="pin-extension-done"]');
};
const completeImportSRPOnboardingFlowWordByWord = async (
driver,
seedPhrase,
password,
) => {
// welcome
await driver.clickElement('[data-testid="onboarding-import-wallet"]');
// metrics
await driver.clickElement('[data-testid="metametrics-no-thanks"]');
// import with recovery phrase, word by word
const words = seedPhrase.split(' ');
for (const word of words) {
await driver.pasteIntoField(
`[data-testid="import-srp__srp-word-${words.indexOf(word)}"]`,
word,
);
}
await driver.clickElement('[data-testid="import-srp-confirm"]');
// 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"]');
// complete
await driver.clickElement('[data-testid="onboarding-complete-done"]');
// pin extension
await driver.clickElement('[data-testid="pin-extension-next"]');
await driver.clickElement('[data-testid="pin-extension-done"]');
};
2018-05-25 03:17:26 +02:00
module.exports = {
getWindowHandles,
convertToHexValue,
tinyDelayMs,
regularDelayMs,
largeDelayMs,
veryLargeDelayMs,
withFixtures,
importSRPOnboardingFlow,
completeImportSRPOnboardingFlow,
completeImportSRPOnboardingFlowWordByWord,
3box Replacement (#15243) * Backup user data Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Tests for prependZero (utils.js) Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Fix advancedtab test Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> backup controller tests Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Backup controller don't have a store. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Restore from file. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Advanced Tab tests Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fix Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> e2e tests for backup unit tests for restore. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Fix comments on PR. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> restore style Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> We should move the exportAsFile to a utility file in the shared/ directory Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Move export as file to shared folder Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Refactor create download folder methods Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Move the backup/restore buttons closer to 3box Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Change descriptions Add to search Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> refactor code to use if instead of && Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Restore button should change cursor to pointer. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Fix restore not uploading same file twice. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Do not backup these items in preferences identities lostIdentities selectedAddress Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> lint fixes. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Only update what is needed. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Fixed test for search Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * remove txError as it currently does nothing. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Remove dispatch, not needed since we're not dispatching any actions. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Event should be title case. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Make backup/restore normal async functions rename event as per product suggestion. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Use success Actionable message for success message and danger for error message Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * change event name to match with backup Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * lint fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * fix e2e Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com>
2022-08-09 20:36:32 +02:00
createDownloadFolder,
};