Skip to content

Commit

Permalink
Adding Selecting Chain Interaction And Flow Improvements (#21)
Browse files Browse the repository at this point in the history
* chore: changing the flow of test cases; starting by creating a new wallet rather than importing

* feature: adding behavior in import wallet flow to select a chain when importing/creating wallet

* feature: adding helper methods to click elements in a reliable way

* chore: using helper methods inside keplr.js

* chore: handling edge case for grabbing token values when values are large numbers containing commas

* chore: updating selector for getting wallet address and adding test cases to validate the behavior

* chore: addressing PR comments

* chore: addressing PR comments

* chore: replacing Agoric local with Agoric localhost
  • Loading branch information
rabi-siddique authored Mar 7, 2024
1 parent 7d92996 commit bd127a3
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 26 deletions.
76 changes: 58 additions & 18 deletions commands/keplr.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,7 @@ const keplr = {
await playwright.keplrWindow(),
);

await playwright.waitAndClick(
onboardingElements.submitChainButton,
await playwright.keplrWindow(),
);
await module.exports.handleSelectChain();

await playwright.waitForByText(
onboardingElements.phraseAccountCreated,
Expand All @@ -170,6 +167,38 @@ const keplr = {

return true;
},
async handleSelectChain() {
const chainNameExists = await playwright.waitForAndCheckElementExistence(
onboardingElements.chainNameSelector,
);

if (chainNameExists) {
await playwright.waitAndClickByText(
onboardingElements.chainName,
playwright.keplrWindow(),
);
await playwright.waitAndClick(
onboardingElements.submitChainButton,
playwright.keplrWindow(),
);
const importButtonExists =
await playwright.waitForAndCheckElementExistence(
onboardingElements.importButtonSelector,
);

if (importButtonExists) {
await playwright.waitAndClick(
onboardingElements.importButtonSelector,
playwright.keplrWindow(),
);
}
} else {
await playwright.waitAndClick(
onboardingElements.submitChainButton,
playwright.keplrWindow(),
);
}
},
async importWalletWithPhrase(secretWords) {
await playwright.waitAndClickByText(
onboardingElements.phraseCount24,
Expand Down Expand Up @@ -238,12 +267,20 @@ const keplr = {
return true;
},

async getWalletAddress() {
await playwright.switchToKeplrWindow();
async getWalletAddress(chainName) {
playwright.switchToKeplrWindow();
await module.exports.goToHome();
const page = playwright.keplrWindow();
const newTokensSelctorExists = await playwright.waitForAndCheckElementExistence(
homePageElements.newTokensFoundSelector,
);

if (newTokensSelctorExists) {
await module.exports.addNewTokensFound(false);
}

await playwright.waitAndClickByText(notificationPageElements.copyAddress);
await page.click(notificationPageElements.copyWalletAddressSelector);
await playwright.waitAndClick(notificationPageElements.walletSelectors(chainName))

walletAddress = clipboardy.readSync();
await playwright.switchToCypressWindow();
return walletAddress;
Expand Down Expand Up @@ -287,22 +324,25 @@ const keplr = {

return true;
},

async addNewTokensFound() {
module.exports.switchToKeplrIfNotActive();
await module.exports.goToHome();
async addNewTokensFound(switchScreens = true) {
if (switchScreens) {
module.exports.switchToKeplrIfNotActive();
await module.exports.goToHome();
}
await playwright.waitAndClickByText(homePageElements.newTokensFound);
await playwright.waitAndClick(
await playwright.waitAndClickWithRetry(
homePageElements.selectAllTokensCheck,
playwright.keplrWindow(),
{ number: -1, force: true },
);
await playwright.waitAndClickByText(
homePageElements.addChainsButton,
playwright.keplrWindow(),
true,
);
await playwright.switchToCypressWindow();

if (switchScreens) {
await playwright.switchToCypressWindow();
}

return true;
},
Expand All @@ -319,11 +359,11 @@ const keplr = {
);
const innerTexts = await parentElement.allInnerTexts();
const textArray = innerTexts[0].split('\n');
const tokenValue = Number(textArray[3]);

const tokenValue = textArray[3];
const parsedTokenValue = Number(tokenValue.replace(/,/g, ''));
await playwright.switchToCypressWindow();

return tokenValue;
return parsedTokenValue;
},
};

Expand Down
25 changes: 25 additions & 0 deletions commands/playwright-keplr.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ module.exports = {
page = keplrWindow,
) {
try {
await module.exports.waitUntilStable(page);
await page.waitForSelector(selector, { timeout });
return true;
} catch (error) {
Expand Down Expand Up @@ -307,6 +308,30 @@ module.exports = {
await element.type(value);
await module.exports.waitUntilStable(page);
},
async waitAndClickWithRetry(selector, options) {
const maxRetries = 5;
let retries = 0;

while (retries < maxRetries) {
try {
await module.exports.waitAndClick(
selector,
module.exports.keplrWindow(),
options,
);
return;
} catch (error) {
retries++;
}
}

throw new Error(`Failed to click element after ${maxRetries} attempts`);
},
async waitAndClickWithDelay(selector, options, delay) {
const page = module.exports.keplrWindow()
await page.waitForTimeout(delay)
await module.exports.waitAndClick(selector, page, options);
},
async switchToKeplrNotification() {
const keplrExtensionData = (await module.exports.getExtensionsData()).keplr;

Expand Down
6 changes: 6 additions & 0 deletions pages/keplr/first-time-flow-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ const phraseAccountCreated = 'Account Created!';
const finishButton = 'button[type="button"]';
const textAreaSelector = 'textbox';
const submitPhraseButton = 'button[type="submit"]';
const chainName = 'Agoric local'
const chainNameSelector = 'text=Agoric local'
const importButtonSelector = 'button:has-text("Import")'

module.exports.onboardingElements = {
existingWalletButton,
createWalletButton,
importRecoveryPhraseButton,
useRecoveryPhraseButton,
chainNameSelector,
importButtonSelector,
phraseCount24,
phrasePrivateKey,
walletInput,
Expand All @@ -32,4 +37,5 @@ module.exports.onboardingElements = {
finishButton,
textAreaSelector,
submitPhraseButton,
chainName
};
2 changes: 2 additions & 0 deletions pages/keplr/home-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ const tokenParentSelector = '../../../..';
const selectAllTokensCheck = 'input[type="checkbox"]:enabled';
const newTokensFound = 'new token(s) found';
const addChainsButton = 'Add Chains';
const newTokensFoundSelector = 'text=new token(s) found'

module.exports.homePageElements = {
tokenNameLabel,
tokenParentSelector,
selectAllTokensCheck,
newTokensFound,
addChainsButton,
newTokensFoundSelector
};
6 changes: 3 additions & 3 deletions pages/keplr/notification-page.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const approveButton = `button`;
const copyAddress = 'Copy Address';
const copyWalletAddressSelector = 'div.sc-dkzDqf div.sc-hKMtZM.sc-kDDrLX.cyoEAq.dkJSBQ'
const walletSelectors = chainName => `img[alt="${chainName}"]`;

module.exports.notificationPageElements = {
approveButton,
copyAddress,
copyWalletAddressSelector
};
walletSelectors
};
5 changes: 3 additions & 2 deletions support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,11 +455,12 @@ Cypress.Commands.add('disconnectWalletFromDapp', () => {
return cy.task('disconnectWalletFromDapp');
});

Cypress.Commands.add('getWalletAddress', () => {
cy.task('getWalletAddress').then(address => {
Cypress.Commands.add('getWalletAddress', chainName => {
cy.task('getWalletAddress', chainName).then(address => {
return address;
});
});

Cypress.Commands.add('switchWallet', walletName => {
return cy.task('switchWallet', { walletName });
});
Expand Down
20 changes: 17 additions & 3 deletions tests/e2e/specs/keplr/keplr-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ describe('Keplr', () => {
expect(setupFinished).to.be.true;
});
});

it(`should get wallet address while running addNewTokensFound flow`, () => {
cy.getWalletAddress('Agoric localhost').then(walletAddress => {
expect(walletAddress.length).to.be.equal(45);
});

cy.getWalletAddress('Cosmos Hub').then(walletAddress => {
expect(walletAddress.length).to.be.equal(45);
});
});
it(`should switch to new wallet by name`, () => {
cy.switchWallet('My Wallet 2').then(taskCompleted => {
expect(taskCompleted).to.be.true;
Expand All @@ -99,10 +109,14 @@ describe('Keplr', () => {
});
});

it(`should get wallet address`, () => {
cy.getWalletAddress().then(walletAddress => {
it(`should get wallet address without running addNewTokensFound flow`, () => {
cy.getWalletAddress('Agoric localhost').then(walletAddress => {
expect(walletAddress.length).to.be.equal(45);
});

cy.getWalletAddress('Cosmos Hub').then(walletAddress => {
expect(walletAddress.length).to.be.equal(45);
});
});
});
});
});

0 comments on commit bd127a3

Please sign in to comment.