diff --git a/commands/keplr.js b/commands/keplr.js index 612024e07..b31d8b597 100644 --- a/commands/keplr.js +++ b/commands/keplr.js @@ -4,9 +4,9 @@ const { onboardingElements } = require('../pages/keplr/first-time-flow-page'); const { notificationPageElements, } = require('../pages/keplr/notification-page'); +const { homePageElements } = require('../pages/keplr/home-page'); const clipboardy = require('clipboardy'); - let extensionId; let extensionVersion; let registrationUrl; @@ -29,7 +29,6 @@ const keplr = { }, walletAddress: () => { return walletAddress; - }, extensionId: () => { return extensionId; @@ -273,6 +272,7 @@ const keplr = { }, async switchWallet({ walletName }) { + const originalURL = (await playwright.keplrWindow()).url(); await module.exports.switchToKeplrIfNotActive(); await module.exports.goToWalletsPage(); @@ -281,10 +281,50 @@ const keplr = { playwright.keplrWindow(), true, ); + + await playwright.waitForURLLoad(originalURL); + await playwright.switchToCypressWindow(); + + return true; + }, + + async addNewTokensFound() { + module.exports.switchToKeplrIfNotActive(); + await module.exports.goToHome(); + await playwright.waitAndClickByText(homePageElements.newTokensFound); + await playwright.waitAndClick( + homePageElements.selectAllTokensCheck, + await playwright.keplrWindow(), + { number: -1, force: true }, + ); + await playwright.waitAndClickByText( + homePageElements.addChainsButton, + await playwright.keplrWindow(), + true, + ); await playwright.switchToCypressWindow(); return true; }, + + async getTokenAmount({ tokenName }) { + module.exports.switchToKeplrIfNotActive(); + await module.exports.goToHome(); + + const tokenLabel = await playwright.waitFor( + homePageElements.tokenNameLabel(tokenName), + ); + const parentElement = tokenLabel.locator( + homePageElements.tokenParentSelector, + ); + const innerTexts = await parentElement.allInnerTexts(); + const textArray = innerTexts[0].split('\n'); + const tokenValue = Number(textArray[3]); + + await playwright.switchToCypressWindow(); + + return tokenValue; + }, }; module.exports = keplr; diff --git a/commands/playwright-keplr.js b/commands/playwright-keplr.js index 385ab7f55..ba2ec17eb 100644 --- a/commands/playwright-keplr.js +++ b/commands/playwright-keplr.js @@ -52,7 +52,8 @@ module.exports = { module.exports.assignActiveTabName('keplr'); return true; }, - async switchToCypressWindow() { + async switchToCypressWindow(page = keplrWindow) { + await module.exports.waitUntilStable(page); if (mainWindow) { await mainWindow.bringToFront(); module.exports.assignActiveTabName('cypress'); @@ -193,9 +194,9 @@ module.exports = { } return element; }, - async waitForByText(text, page = keplrWindow) { + async waitForByText(text, page = keplrWindow, exact = false) { await module.exports.waitUntilStable(page); - const element = page.getByText(text).first(); + const element = page.getByText(text, { exact: exact }).first(); await element.waitFor(); await element.focus(); if (process.env.STABLE_MODE) { @@ -208,7 +209,7 @@ module.exports = { return element; }, async waitAndClickByText(text, page = keplrWindow, exact = false) { - await module.exports.waitForByText(text, page); + await module.exports.waitForByText(text, page, exact); const element = `:is(:text-is("${text}")${exact ? '' : `, :text("${text}")`})`; await page.click(element); await module.exports.waitUntilStable(); @@ -221,9 +222,7 @@ module.exports = { await module.exports.waitUntilStable(page); }, async waitAndGetValue(selector, page = keplrWindow) { - const expect = expectInstance - ? expectInstance - : require('@playwright/test').expect; + const expect = require('@playwright/test').expect; const element = await module.exports.waitFor(selector, page); await expect(element).toHaveText(/[a-zA-Z0-9]/, { ignoreCase: true, @@ -266,7 +265,11 @@ module.exports = { await module.exports.waitUntilStable(); return element; }, - async waitForAndCheckElementExistence(selector, timeout = 1000, page = keplrWindow) { + async waitForAndCheckElementExistence( + selector, + timeout = 1000, + page = keplrWindow, + ) { try { await page.waitForSelector(selector, { timeout }); return true; @@ -274,40 +277,6 @@ module.exports = { return false; } }, - async waitAndClick(selector, page = keplrWindow, args = {}) { - const element = await module.exports.waitFor( - selector, - page, - args.number || 0, - ); - if (args.numberOfClicks && !args.waitForEvent) { - await element.click({ - clickCount: args.numberOfClicks, - force: args.force, - }); - } else if (args.numberOfClicks && args.waitForEvent) { - await Promise.all([ - page.waitForEvent(args.waitForEvent), - element.click({ clickCount: args.numberOfClicks, force: args.force }), - ]); - } else if (args.waitForEvent) { - if (args.waitForEvent.includes('navi')) { - await Promise.all([ - page.waitForNavigation(), - element.click({ force: args.force }), - ]); - } else { - await Promise.all([ - page.waitForEvent(args.waitForEvent), - element.click({ force: args.force }), - ]); - } - } else { - await element.click({ force: args.force }); - } - await module.exports.waitUntilStable(); - return element; - }, async waitForByRole(role, number = 0, page = keplrWindow) { await module.exports.waitUntilStable(page); const element = page.getByRole(role).nth(number); @@ -366,4 +335,8 @@ module.exports = { ); } }, + async waitForURLLoad(url, page = keplrWindow) { + await page.waitForURL(url); + await module.exports.waitUntilStable(page); + }, }; diff --git a/pages/keplr/home-page.js b/pages/keplr/home-page.js new file mode 100644 index 000000000..a0c339ca3 --- /dev/null +++ b/pages/keplr/home-page.js @@ -0,0 +1,13 @@ +const tokenNameLabel = tokenName => `:text-is("${tokenName}")`; +const tokenParentSelector = '../../../..'; +const selectAllTokensCheck = 'input[type="checkbox"]:enabled'; +const newTokensFound = 'new token(s) found'; +const addChainsButton = 'Add Chains'; + +module.exports.homePageElements = { + tokenNameLabel, + tokenParentSelector, + selectAllTokensCheck, + newTokensFound, + addChainsButton, +}; diff --git a/plugins/keplr-plugin.js b/plugins/keplr-plugin.js index 7da74b18b..f20f35c1f 100644 --- a/plugins/keplr-plugin.js +++ b/plugins/keplr-plugin.js @@ -87,6 +87,8 @@ module.exports = (on, config) => { return true; }, switchWallet: keplr.switchWallet, + addNewTokensFound: keplr.addNewTokensFound, + getTokenAmount: keplr.getTokenAmount, }); return config; diff --git a/support/commands.js b/support/commands.js index 19c296bdd..a0ca6ecdc 100644 --- a/support/commands.js +++ b/support/commands.js @@ -463,3 +463,11 @@ Cypress.Commands.add('getWalletAddress', () => { Cypress.Commands.add('switchWallet', walletName => { return cy.task('switchWallet', { walletName }); }); + +Cypress.Commands.add('addNewTokensFound', () => { + return cy.task('addNewTokensFound'); +}); + +Cypress.Commands.add('getTokenAmount', tokenName => { + return cy.task('getTokenAmount', { tokenName }); +}); diff --git a/tests/e2e/specs/keplr/keplr-spec.js b/tests/e2e/specs/keplr/keplr-spec.js index 81a6af071..a03522c30 100644 --- a/tests/e2e/specs/keplr/keplr-spec.js +++ b/tests/e2e/specs/keplr/keplr-spec.js @@ -81,6 +81,18 @@ describe('Keplr', () => { }); // TODO: Add some more robust check later }); + it(`should get the accurate values for the tokens in the wallet`, () => { + cy.switchWallet('My Wallet').then(taskCompleted => { + expect(taskCompleted).to.be.true; + }); + cy.addNewTokensFound(); + cy.getTokenAmount('ATOM').then(tokenValue => { + expect(tokenValue).to.equal(0); + }); + cy.getTokenAmount('BLD').then(tokenValue => { + expect(tokenValue).to.equal(331); + }); + }); it(`should disconnect the wallet from all the connected DAPPs`, () => { cy.disconnectWalletFromDapp().then(taskCompleted => { expect(taskCompleted).to.be.true;