Skip to content

Commit

Permalink
Added Interaction to get the value of a certain token (#19)
Browse files Browse the repository at this point in the history
* feat: added command to get tokens

* chore: await/async fixes
  • Loading branch information
frazarshad authored Mar 6, 2024
1 parent fa21859 commit d49d84e
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 44 deletions.
44 changes: 42 additions & 2 deletions commands/keplr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,7 +29,6 @@ const keplr = {
},
walletAddress: () => {
return walletAddress;

},
extensionId: () => {
return extensionId;
Expand Down Expand Up @@ -273,6 +272,7 @@ const keplr = {
},

async switchWallet({ walletName }) {
const originalURL = playwright.keplrWindow().url();
await module.exports.switchToKeplrIfNotActive();
await module.exports.goToWalletsPage();

Expand All @@ -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,
playwright.keplrWindow(),
{ number: -1, force: true },
);
await playwright.waitAndClickByText(
homePageElements.addChainsButton,
playwright.keplrWindow(),
true,
);
await playwright.switchToCypressWindow();

return true;
},

async getTokenAmount({ tokenName }) {
await 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;
57 changes: 15 additions & 42 deletions commands/playwright-keplr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand All @@ -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();
Expand All @@ -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,
Expand Down Expand Up @@ -266,48 +265,18 @@ 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;
} catch (error) {
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);
Expand Down Expand Up @@ -366,4 +335,8 @@ module.exports = {
);
}
},
async waitForURLLoad(url, page = keplrWindow) {
await page.waitForURL(url);
await module.exports.waitUntilStable(page);
},
};
13 changes: 13 additions & 0 deletions pages/keplr/home-page.js
Original file line number Diff line number Diff line change
@@ -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,
};
2 changes: 2 additions & 0 deletions plugins/keplr-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ module.exports = (on, config) => {
return true;
},
switchWallet: keplr.switchWallet,
addNewTokensFound: keplr.addNewTokensFound,
getTokenAmount: keplr.getTokenAmount,
});

return config;
Expand Down
8 changes: 8 additions & 0 deletions support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
});
12 changes: 12 additions & 0 deletions tests/e2e/specs/keplr/keplr-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d49d84e

Please sign in to comment.