From 5983dc1822aea87f1b13c3d56648c4c2bdd8fc01 Mon Sep 17 00:00:00 2001 From: seaona <54408225+seaona@users.noreply.github.com> Date: Thu, 24 Oct 2024 14:57:04 +0200 Subject: [PATCH] fix: flaky anti-pattern getText + assert 3 (#28062) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## **Description** Continuing the work of removing the e2e anti-pattern of finding the element and then asserting its text. There are more occurrences, but this work is split in several PRs, for an easy review and a faster ci. Once all occurrences have been fixed, we'll be able to merge @HowardBraham 's [PR ](https://github.com/MetaMask/metamask-extension/pull/27591)for adding a lint rule which prevents introducing it again. [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/28062?quickstart=1) ## **Related issues** Fixes: (but doesn't yet closes) https://github.com/MetaMask/metamask-extension/issues/19870 ## **Manual testing steps** 1. Check ci ## **Screenshots/Recordings** ### **Before** ### **After** ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Extension Coding Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I’ve included tests if applicable - [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I’ve applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. --- test/e2e/tests/settings/auto-lock.spec.js | 12 +++---- test/e2e/tests/settings/localization.spec.js | 32 +++++++++++++++---- .../tests/settings/settings-general.spec.js | 15 ++------- .../settings-security-reveal-srp.spec.js | 8 ++--- test/e2e/tests/settings/show-hex-data.spec.js | 14 +++----- 5 files changed, 40 insertions(+), 41 deletions(-) diff --git a/test/e2e/tests/settings/auto-lock.spec.js b/test/e2e/tests/settings/auto-lock.spec.js index 7d7a159d4a1b..46021ed0bb46 100644 --- a/test/e2e/tests/settings/auto-lock.spec.js +++ b/test/e2e/tests/settings/auto-lock.spec.js @@ -1,4 +1,3 @@ -const { strict: assert } = require('assert'); const { defaultGanacheOptions, openMenuSafe, @@ -41,12 +40,11 @@ describe('Auto-Lock Timer', function () { '[data-testid="advanced-setting-auto-lock"] button', ); // Verify the wallet is locked - const pageTitle = await driver.findElement( - '[data-testid="unlock-page-title"]', - ); - const unlockButton = await driver.findElement('.unlock-page button'); - assert.equal(await pageTitle.getText(), 'Welcome back!'); - assert.equal(await unlockButton.isDisplayed(), true); + await driver.waitForSelector({ + css: '[data-testid="unlock-page-title"]', + text: 'Welcome back!', + }); + await driver.waitForSelector('.unlock-page button'); }, ); }); diff --git a/test/e2e/tests/settings/localization.spec.js b/test/e2e/tests/settings/localization.spec.js index 57dbfd5f68cf..1fb1e8d1e8a6 100644 --- a/test/e2e/tests/settings/localization.spec.js +++ b/test/e2e/tests/settings/localization.spec.js @@ -1,4 +1,3 @@ -const { strict: assert } = require('assert'); const { defaultGanacheOptions, withFixtures, @@ -6,6 +5,21 @@ const { } = require('../../helpers'); const FixtureBuilder = require('../../fixture-builder'); +async function mockPhpConversion(mockServer) { + return await mockServer + .forGet('https://min-api.cryptocompare.com/data/price') + .withQuery({ fsym: 'ETH', tsyms: 'PHP,USD' }) + .thenCallback(() => { + return { + statusCode: 200, + json: { + PHP: '100000', + USD: '2500', + }, + }; + }); +} + describe('Localization', function () { it('can correctly display Philippine peso symbol and code', async function () { await withFixtures( @@ -22,18 +36,22 @@ describe('Localization', function () { }) .build(), ganacheOptions: defaultGanacheOptions, + testSpecificMock: mockPhpConversion, title: this.test.fullTitle(), }, async ({ driver }) => { await unlockWallet(driver); // After the removal of displaying secondary currency in coin-overview.tsx, we will test localization on main balance with showNativeTokenAsMainBalance = false - const primaryBalance = await driver.findElement( - '[data-testid="eth-overview__primary-currency"]', - ); - const balanceText = await primaryBalance.getText(); - assert.ok(balanceText.startsWith('₱')); - assert.ok(balanceText.endsWith('PHP')); + await driver.waitForSelector({ + tag: 'span', + text: 'PHP', + }); + + await driver.waitForSelector({ + tag: 'span', + text: '₱2,500,000.00', + }); }, ); }); diff --git a/test/e2e/tests/settings/settings-general.spec.js b/test/e2e/tests/settings/settings-general.spec.js index 5e75c857a7f8..ef07a53d01f2 100644 --- a/test/e2e/tests/settings/settings-general.spec.js +++ b/test/e2e/tests/settings/settings-general.spec.js @@ -1,4 +1,3 @@ -const { strict: assert } = require('assert'); const { defaultGanacheOptions, openMenuSafe, @@ -28,25 +27,15 @@ describe('Settings', function () { '[data-testid="jazz_icon"] .settings-page__content-item__identicon__item__icon--active', ); - const jazziconText = await driver.findElement({ + await driver.waitForSelector({ tag: 'h6', text: 'Jazzicons', }); - assert.equal( - await jazziconText.getText(), - 'Jazzicons', - 'Text for icon should be Jazzicons', - ); - const blockiesText = await driver.findElement({ + await driver.waitForSelector({ tag: 'h6', text: 'Blockies', }); - assert.equal( - await blockiesText.getText(), - 'Blockies', - 'Text for icon should be Blockies', - ); }, ); }); diff --git a/test/e2e/tests/settings/settings-security-reveal-srp.spec.js b/test/e2e/tests/settings/settings-security-reveal-srp.spec.js index 7cbdaefe73ed..7f68f53f8dc7 100644 --- a/test/e2e/tests/settings/settings-security-reveal-srp.spec.js +++ b/test/e2e/tests/settings/settings-security-reveal-srp.spec.js @@ -52,10 +52,10 @@ describe('Reveal SRP through settings', function () { await tapAndHoldToRevealSRP(driver); // confirm SRP text matches expected - const displayedSRP = await driver.findVisibleElement( - '[data-testid="srp_text"]', - ); - assert.equal(await displayedSRP.getText(), E2E_SRP); + await driver.waitForSelector({ + css: '[data-testid="srp_text"]', + text: E2E_SRP, + }); // copy SRP text to clipboard await driver.clickElement({ diff --git a/test/e2e/tests/settings/show-hex-data.spec.js b/test/e2e/tests/settings/show-hex-data.spec.js index 5e0ae9a133a0..4bef79ca0a3b 100644 --- a/test/e2e/tests/settings/show-hex-data.spec.js +++ b/test/e2e/tests/settings/show-hex-data.spec.js @@ -1,4 +1,3 @@ -const { strict: assert } = require('assert'); const { defaultGanacheOptions, withFixtures, @@ -84,15 +83,10 @@ describe('Check the toggle for hex data', function () { await sendTransactionAndVerifyHexData(driver); // Verify hex data in the container content - const pageContentContainer = await driver.findElement( - selectors.containerContent, - ); - const pageContentContainerText = await pageContentContainer.getText(); - assert.equal( - pageContentContainerText.includes(inputData.hexDataText), - true, - 'Hex data is incorrect', - ); + await driver.waitForSelector({ + tag: 'p', + text: '0x0abc', + }); }, ); });