1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 09:57:02 +01:00

fix flakiness in mv3 multiple restart test (#19708)

* fix flakiness in mv3 multiple restart test

* tweak msgs

* refactor

* refactor

* remove unused function
This commit is contained in:
Pedro Figueiredo 2023-07-12 19:27:44 +01:00 committed by GitHub
parent c7782563c8
commit 42c3a3958b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 47 deletions

View File

@ -692,12 +692,6 @@ function generateRandNumBetween(x, y) {
return randomNumber;
}
async function switchToWindow(driver, windowTitle) {
const windowHandles = await driver.getAllWindowHandles();
return await driver.switchToWindowWithTitle(windowTitle, windowHandles);
}
async function sleepSeconds(sec) {
return new Promise((resolve) => setTimeout(resolve, sec * 1000));
}
@ -714,7 +708,8 @@ async function terminateServiceWorker(driver) {
tag: 'button',
});
const serviceWorkerElements = await driver.findElements({
await driver.delay(tinyDelayMs);
const serviceWorkerElements = await driver.findClickableElements({
text: 'terminate',
tag: 'span',
});
@ -722,8 +717,7 @@ async function terminateServiceWorker(driver) {
// 1st one is app-init.js; while 2nd one is service-worker.js
await serviceWorkerElements[serviceWorkerElements.length - 1].click();
const serviceWorkerTab = await switchToWindow(
driver,
const serviceWorkerTab = await driver.switchToWindowWithTitle(
WINDOW_TITLES.ServiceWorkerSettings,
);
@ -810,7 +804,6 @@ module.exports = {
generateETHBalance,
roundToXDecimalPlaces,
generateRandNumBetween,
switchToWindow,
sleepSeconds,
terminateServiceWorker,
switchToNotificationWindow,

View File

@ -9,10 +9,10 @@ const {
generateETHBalance,
roundToXDecimalPlaces,
generateRandNumBetween,
switchToWindow,
sleepSeconds,
terminateServiceWorker,
unlockWallet,
largeDelayMs,
} = require('../helpers');
const FixtureBuilder = require('../fixture-builder');
@ -114,7 +114,9 @@ describe('MV3 - Restart service worker multiple times', function () {
);
async function simpleSendETH(driver, value, recipient) {
await switchToWindow(driver, WINDOW_TITLES.ExtensionInFullScreenView);
await driver.switchToWindowWithTitle(
WINDOW_TITLES.ExtensionInFullScreenView,
);
await driver.clickElement('[data-testid="eth-overview-send"]');
await driver.fill('[data-testid="ens-input"]', recipient);
@ -138,7 +140,9 @@ describe('MV3 - Restart service worker multiple times', function () {
}
async function assertETHBalance(driver, expectedBalance) {
await switchToWindow(driver, WINDOW_TITLES.ExtensionInFullScreenView);
await driver.switchToWindowWithTitle(
WINDOW_TITLES.ExtensionInFullScreenView,
);
const isETHBalanceOverviewPresentAndVisible =
await driver.isElementPresentAndVisible({
@ -175,12 +179,11 @@ describe('MV3 - Restart service worker multiple times', function () {
await openDapp(driver);
// Click add Ethereum chain
await switchToWindow(driver, WINDOW_TITLES.TestDApp);
await driver.switchToWindowWithTitle(WINDOW_TITLES.TestDApp);
await driver.clickElement('#addEthereumChain');
await driver.waitUntilXWindowHandles(2);
// Notification pop up opens
await switchToWindow(driver, WINDOW_TITLES.Notification);
await driver.switchToWindowWithTitle(WINDOW_TITLES.Notification);
let notification = await driver.isElementPresent({
text: 'Allow this site to add a network?',
tag: 'h3',
@ -189,19 +192,18 @@ describe('MV3 - Restart service worker multiple times', function () {
// Cancel Notification
await driver.clickElement({ text: 'Cancel', tag: 'button' });
await driver.waitUntilXWindowHandles(2);
// Terminate Service Worker
await switchToWindow(driver, WINDOW_TITLES.TestDApp);
await driver.switchToWindowWithTitle(WINDOW_TITLES.TestDApp);
await terminateServiceWorker(driver);
// Click add Ethereum chain #2
await switchToWindow(driver, WINDOW_TITLES.TestDApp);
await driver.switchToWindowWithTitle(WINDOW_TITLES.TestDApp);
await driver.clickElement('#addEthereumChain');
await driver.waitUntilXWindowHandles(2);
// Notification pop up opens
await switchToWindow(driver, WINDOW_TITLES.Notification);
await driver.switchToWindowWithTitle(WINDOW_TITLES.Notification);
notification = await driver.isElementPresent({
text: 'Allow this site to add a network?',
tag: 'h3',
@ -210,19 +212,17 @@ describe('MV3 - Restart service worker multiple times', function () {
// Cancel Notification
await driver.clickElement({ text: 'Cancel', tag: 'button' });
await driver.waitUntilXWindowHandles(2);
// Terminate Service Worker
await switchToWindow(driver, WINDOW_TITLES.TestDApp);
await driver.switchToWindowWithTitle(WINDOW_TITLES.TestDApp);
await terminateServiceWorker(driver);
// Click add Ethereum chain #3
await switchToWindow(driver, WINDOW_TITLES.TestDApp);
await driver.switchToWindowWithTitle(WINDOW_TITLES.TestDApp);
await driver.clickElement('#addEthereumChain');
await driver.waitUntilXWindowHandles(2);
// Notification pop up opens
await switchToWindow(driver, WINDOW_TITLES.Notification);
await driver.switchToWindowWithTitle(WINDOW_TITLES.Notification);
notification = await driver.isElementPresent({
text: 'Allow this site to add a network?',
tag: 'h3',
@ -232,7 +232,6 @@ describe('MV3 - Restart service worker multiple times', function () {
// Accept Notification
await driver.clickElement({ text: 'Approve', tag: 'button' });
await driver.clickElement({ text: 'Switch network', tag: 'button' });
await driver.waitUntilXWindowHandles(2);
},
);
});
@ -257,21 +256,20 @@ describe('MV3 - Restart service worker multiple times', function () {
await openDapp(driver);
await clickSendButton(driver);
await driver.waitUntilXWindowHandles(2);
await switchToWindow(driver, WINDOW_TITLES.TestDApp);
await terminateServiceWorker(driver);
await driver.waitUntilXWindowHandles(2);
await driver.delay(largeDelayMs);
await clickSendButton(driver);
await driver.waitUntilXWindowHandles(2);
await switchToWindow(driver, WINDOW_TITLES.TestDApp);
await driver.switchToWindowWithTitle(WINDOW_TITLES.TestDApp);
await terminateServiceWorker(driver);
await clickSendButton(driver);
await driver.switchToWindowWithTitle(WINDOW_TITLES.TestDApp);
await terminateServiceWorker(driver);
await clickSendButton(driver);
await driver.waitUntilXWindowHandles(2);
await assertNumberOfTransactionsInPopUp(driver, 3);
@ -287,7 +285,7 @@ describe('MV3 - Restart service worker multiple times', function () {
async function clickSendButton(driver) {
// Click send button
await switchToWindow(driver, WINDOW_TITLES.TestDApp);
await driver.switchToWindowWithTitle(WINDOW_TITLES.TestDApp);
await driver.waitForSelector({
css: '#sendButton',
@ -297,7 +295,7 @@ describe('MV3 - Restart service worker multiple times', function () {
}
async function confirmETHSendNotification(driver, amount) {
await switchToWindow(driver, WINDOW_TITLES.Notification);
await driver.switchToWindowWithTitle(WINDOW_TITLES.Notification);
await driver.clickElement({
text: 'Edit',
@ -318,14 +316,16 @@ describe('MV3 - Restart service worker multiple times', function () {
}
async function assertNumberOfTransactionsInPopUp(driver, number) {
await switchToWindow(driver, WINDOW_TITLES.Notification);
const navEl = await driver.findElement(
'.confirm-page-container-navigation__navtext',
);
await driver.delay(largeDelayMs);
const notificationProgress = await navEl.getText();
await driver.switchToWindowWithTitle(WINDOW_TITLES.Notification);
assert.ok(notificationProgress, `1 of ${number}`);
const foundElement = await driver.findElements({
css: '.confirm-page-container-navigation__navtext',
text: `1 of ${number}`,
});
assert.ok(foundElement, true);
}
});
@ -365,7 +365,9 @@ describe('MV3 - Restart service worker multiple times', function () {
);
async function reloadExtension(driver, extensionId) {
await switchToWindow(driver, WINDOW_TITLES.ExtensionInFullScreenView);
await driver.switchToWindowWithTitle(
WINDOW_TITLES.ExtensionInFullScreenView,
);
await driver.openNewPage('chrome://extensions/');

View File

@ -58,7 +58,7 @@ class Driver {
* @param extensionUrl
* @param {number} timeout
*/
constructor(driver, browser, extensionUrl, timeout = 10000) {
constructor(driver, browser, extensionUrl, timeout = 10 * 1000) {
this.driver = driver;
this.browser = browser;
this.extensionUrl = extensionUrl;
@ -376,6 +376,7 @@ class Driver {
let windowHandles = [];
while (timeElapsed <= timeout) {
windowHandles = await this.driver.getAllWindowHandles();
if (windowHandles.length === x) {
return windowHandles;
}
@ -389,7 +390,7 @@ class Driver {
title,
initialWindowHandles,
delayStep = 1000,
timeout = 5000,
timeout = this.timeout,
) {
let windowHandles =
initialWindowHandles || (await this.driver.getAllWindowHandles());
@ -397,6 +398,7 @@ class Driver {
while (timeElapsed <= timeout) {
for (const handle of windowHandles) {
await this.driver.switchTo().window(handle);
const handleTitle = await this.driver.getTitle();
if (handleTitle === title) {
return handle;