Skip to content

Commit

Permalink
Merge pull request #18 from Synthetixio/metamask-accept-reject-tx
Browse files Browse the repository at this point in the history
feat: metamask accept/reject tx
  • Loading branch information
drptbl authored Dec 6, 2020
2 parents 6a37ac6 + 45def8a commit 0786971
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pages/metamask/first-time-flow-page.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const app = '#app-content';
const app = '#app-content .app';

const welcomePage = '.welcome-page';
const confirmButton = `${welcomePage} .first-time-flow__button`;
Expand Down
23 changes: 23 additions & 0 deletions pages/metamask/notification-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ const nextButton = `${notificationPage} .permissions-connect-choose-account__bot
const permissionsPage = '.permissions-connect';
const connectButton = `${permissionsPage} .permission-approval-container__footers button:nth-child(2)`;

const confirmPageHeader = `${notificationPage} .confirm-page-container-header`;
const confirmPageContent = `${notificationPage} .confirm-page-container-content`;
const confirmPageGasFeeSection = `${confirmPageContent} .confirm-page-container-content__gas-fee`;
const gasFeeLabel = `${confirmPageGasFeeSection} .currency-display-component__text`;
const gasFeeInput = `${confirmPageGasFeeSection} .advanced-gas-inputs__gas-edit-row:nth-child(1) .advanced-gas-inputs__gas-edit-row__input`;
const gasLimitInput = `${confirmPageGasFeeSection} .advanced-gas-inputs__gas-edit-row:nth-child(2) .advanced-gas-inputs__gas-edit-row__input`;
const totalLabel = `${confirmPageContent} div:nth-child(2) > .confirm-detail-row .currency-display-component__text`;
const rejectButton = `${confirmPageContent} [data-testid="page-container-footer-cancel"]`;
const confirmButton = `${confirmPageContent} [data-testid="page-container-footer-next"]`;

export const notificationPageElements = {
notificationPage,
nextButton,
Expand All @@ -13,3 +23,16 @@ export const permissionsPageElements = {
permissionsPage,
connectButton,
};

export const confirmPageElements = {
notificationPage,
confirmPageHeader,
confirmPageContent,
confirmPageGasFeeSection,
gasFeeLabel,
gasFeeInput,
gasLimitInput,
totalLabel,
rejectButton,
confirmButton,
};
46 changes: 46 additions & 0 deletions plugins/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const { unlockPageElements } = require('../pages/metamask/unlock-page');
const {
notificationPageElements,
permissionsPageElements,
confirmPageElements,
} = require('../pages/metamask/notification-page');

let puppeteerBrowser;
Expand Down Expand Up @@ -104,6 +105,14 @@ module.exports = (on, config) => {
const accepted = await acceptMetamaskAccess();
return accepted;
},
confirmMetamaskTransaction: async () => {
const confirmed = await confirmMetamaskTransaction();
return confirmed;
},
rejectMetamaskTransaction: async () => {
const rejected = await rejectMetamaskTransaction();
return rejected;
},
getMetamaskWalletAddress: async () => {
const walletAddress = await getMetamaskWalletAddress();
return walletAddress;
Expand Down Expand Up @@ -179,6 +188,16 @@ async function waitAndGetValue(selector, page = metamaskWindow) {
return value;
}

async function waitAndSetValue(text, selector, page = metamaskWindow) {
await waitFor(selector, page);
await page.evaluate(
selector => (document.querySelector(selector).value = ''),
selector,
);
await page.focus(selector);
await page.keyboard.type(text);
}

async function waitForText(selector, text, page = metamaskWindow) {
await waitFor(selector, page);
await page.waitForFunction(
Expand Down Expand Up @@ -311,6 +330,33 @@ async function acceptMetamaskAccess() {
const notificationPage = await switchToMetamaskNotification();
await waitAndClick(notificationPageElements.nextButton, notificationPage);
await waitAndClick(permissionsPageElements.connectButton, notificationPage);
await metamaskWindow.waitForTimeout(3000);
return true;
}

async function confirmMetamaskTransaction() {
await metamaskWindow.waitForTimeout(3000);
const notificationPage = await switchToMetamaskNotification();
const currentGasFee = await waitAndGetValue(
confirmPageElements.gasFeeInput,
notificationPage,
);
const newGasFee = (Number(currentGasFee) + 10).toString();
await waitAndSetValue(
newGasFee,
confirmPageElements.gasFeeInput,
notificationPage,
);
await waitAndClick(confirmPageElements.confirmButton, notificationPage);
await metamaskWindow.waitForTimeout(3000);
return true;
}

async function rejectMetamaskTransaction() {
await metamaskWindow.waitForTimeout(3000);
const notificationPage = await switchToMetamaskNotification();
await waitAndClick(confirmPageElements.rejectButton, notificationPage);
await metamaskWindow.waitForTimeout(3000);
return true;
}

Expand Down
8 changes: 8 additions & 0 deletions support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ Cypress.Commands.add('acceptMetamaskAccess', () => {
return cy.task('acceptMetamaskAccess');
});

Cypress.Commands.add('confirmMetamaskTransaction', () => {
return cy.task('confirmMetamaskTransaction');
});

Cypress.Commands.add('rejectMetamaskTransaction', () => {
return cy.task('rejectMetamaskTransaction');
});

Cypress.Commands.add('switchToMetamaskNotification', () => {
return cy.task('switchToMetamaskNotification');
});
Expand Down
12 changes: 12 additions & 0 deletions support/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ declare namespace Cypress {
* cy.acceptMetamaskAccess()
*/
acceptMetamaskAccess(): Chainable<Subject>;
/**
* Confirm metamask atransaction
* @example
* cy.confirmMetamaskTransaction()
*/
confirmMetamaskTransaction(): Chainable<Subject>;
/**
* Reject metamask transaction
* @example
* cy.rejectMetamaskTransaction()
*/
rejectMetamaskTransaction(): Chainable<Subject>;
/**
* Switch to metamask notification window
* @example
Expand Down

0 comments on commit 0786971

Please sign in to comment.