Skip to content

Commit

Permalink
Add shouldWaitForPopupClosure option to approvals and txs
Browse files Browse the repository at this point in the history
  • Loading branch information
duckception committed Jan 24, 2024
1 parent 00abaad commit 7d7a776
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 44 deletions.
14 changes: 10 additions & 4 deletions commands/metamask.js
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,10 @@ const metamask = {
);
return true;
},
async confirmPermissionToSpend(spendLimit) {
async confirmPermissionToSpend({
spendLimit,
shouldWaitForPopupClosure = false,
} = {}) {
const notificationPage = await playwright.switchToMetamaskNotification();
// experimental mode on
if (
Expand All @@ -874,7 +877,7 @@ const metamask = {
await playwright.waitAndClick(
notificationPageElements.allowToSpendButton,
notificationPage,
{ waitForEvent: 'close' },
shouldWaitForPopupClosure ? undefined : { waitForEvent: 'close' },
);
return true;
},
Expand Down Expand Up @@ -963,7 +966,10 @@ const metamask = {
);
return true;
},
async confirmTransaction(gasConfig) {
async confirmTransaction({
gasConfig,
shouldWaitForPopupClosure = false,
} = {}) {
let txData = {};
const notificationPage = await playwright.switchToMetamaskNotification();
if (gasConfig) {
Expand Down Expand Up @@ -1189,7 +1195,7 @@ const metamask = {
await playwright.waitAndClick(
confirmPageElements.confirmButton,
notificationPage,
{ waitForEvent: 'close' },
shouldWaitForPopupClosure ? undefined : { waitForEvent: 'close' },
);
txData.confirmed = true;
log('[confirmTransaction] Transaction confirmed!');
Expand Down
26 changes: 15 additions & 11 deletions docs/synpress-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,10 @@ rejectMetamaskAddToken(): Chainable<boolean>;
Confirm metamask permission to spend asset.

```ts
confirmMetamaskPermissionToSpend(spendLimit?: string): Chainable<string>;
confirmMetamaskPermissionToSpend(options: {
spendLimit?: string
shouldWaitForPopupClosure?: boolean
}): Chainable<string>;
```

#### `cy.confirmMetamaskPermissionToApproveAll()`
Expand Down Expand Up @@ -415,22 +418,23 @@ rejectMetamaskAccess(): Chainable<boolean>;
Confirm metamask transaction (auto-detects eip-1559 and legacy transactions).

```ts
confirmMetamaskTransaction(
gasConfig?:
confirmMetamaskTransaction(options: {
gasConfig:
| {
gasLimit?: number;
baseFee?: number;
priorityFee?: number;
gasLimit?: number;
baseFee?: number;
priorityFee?: number;
}
| {
gasLimit?: number;
gasPrice?: number;
}
gasLimit?: number;
gasPrice?: number;
}
| 'low'
| 'market'
| 'aggressive'
| 'site',
): Chainable<Subject>;
| 'site',
shouldWaitForPopupClosure?: boolean
}): Chainable<Subject>;
```

#### `cy.confirmMetamaskTransactionAndWaitForMining()`
Expand Down
22 changes: 17 additions & 5 deletions support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,14 @@ Cypress.Commands.add('rejectMetamaskAddToken', () => {

Cypress.Commands.add(
'confirmMetamaskPermissionToSpend',
(spendLimit = '999999999999999999') => {
return cy.task('confirmMetamaskPermissionToSpend', spendLimit);
({
spendLimit = '999999999999999999',
shouldWaitForPopupClosure = false,
} = {}) => {
return cy.task('confirmMetamaskPermissionToSpend', {
spendLimit,
shouldWaitForPopupClosure,
});
},
);

Expand All @@ -192,9 +198,15 @@ Cypress.Commands.add('rejectMetamaskAccess', () => {
return cy.task('rejectMetamaskAccess');
});

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

Cypress.Commands.add(
'confirmMetamaskTransactionAndWaitForMining',
Expand Down
36 changes: 21 additions & 15 deletions support/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,13 @@ declare namespace Cypress {
* Confirm metamask permission to spend asset
* @example
* cy.confirmMetamaskPermissionToSpend()
* cy.confirmMetamaskPermissionToSpend('999999999')
* cy.confirmMetamaskPermissionToSpend({ spendLimit: '999999999' })
* cy.confirmMetamaskPermissionToSpend({ spendLimit: '999999999', shouldWaitForPopupClosure: false })
*/
confirmMetamaskPermissionToSpend(spendLimit?: string): Chainable<string>;
confirmMetamaskPermissionToSpend(options: {
spendLimit?: string
shouldWaitForPopupClosure?: boolean
}): Chainable<string>;
/**
* Confirm metamask permission to access all elements (example: collectibles)
* @example
Expand Down Expand Up @@ -327,26 +331,28 @@ declare namespace Cypress {
* Confirm metamask transaction (auto-detects eip-1559 and legacy transactions)
* @example
* cy.confirmMetamaskTransaction()
* cy.confirmMetamaskTransaction({ gasLimit: 1000000, baseFee: 20, priorityFee: 20 }) // eip-1559
* cy.confirmMetamaskTransaction({ gasLimit: 1000000, gasPrice: 20 }) // legacy
* cy.confirmMetamaskTransaction('aggressive') // eip-1559 only! => available options: 'low', 'market', 'aggressive', 'site' (site is usually by default)
* cy.confirmMetamaskTransaction({ gasConfig: { gasLimit: 1000000, baseFee: 20, priorityFee: 20 } }) // eip-1559
* cy.confirmMetamaskTransaction({ gasConfig: { gasLimit: 1000000, gasPrice: 20 } }) // legacy
* cy.confirmMetamaskTransaction({ gasConfig: 'aggressive' }) // eip-1559 only! => available options: 'low', 'market', 'aggressive', 'site' (site is usually by default)
* cy.confirmMetamaskTransaction({ shouldWaitForPopupClosure: false })
*/
confirmMetamaskTransaction(
gasConfig?:
confirmMetamaskTransaction(options: {
gasConfig:
| {
gasLimit?: number;
baseFee?: number;
priorityFee?: number;
}
gasLimit?: number;
baseFee?: number;
priorityFee?: number;
}
| {
gasLimit?: number;
gasPrice?: number;
}
gasLimit?: number;
gasPrice?: number;
}
| 'low'
| 'market'
| 'aggressive'
| 'site',
): Chainable<Subject>;
shouldWaitForPopupClosure?: boolean
}): Chainable<Subject>;
/**
* Confirm metamask transaction (auto-detects eip-1559 and legacy transactions) and wait for ALL pending transactions to be mined
* @example
Expand Down
54 changes: 45 additions & 9 deletions tests/e2e/specs/metamask-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,12 +347,26 @@ describe('Metamask', () => {
it(`confirmMetamaskTransaction should confirm legacy transaction using advanced gas settings`, () => {
cy.get('#sendButton').click();
cy.confirmMetamaskTransaction({
gasLimit: 210000,
gasPrice: 100,
gasConfig: {
gasLimit: 210000,
gasPrice: 100,
},
}).then(txData => {
expect(txData.confirmed).to.be.true;
});
});
it(`confirmMetamaskTransaction should work for serial transactions`, () => {
cy.get('#sendEIP1559Button').click();
cy.get('#sendEIP1559Button').click();
cy.confirmMetamaskTransaction({
shouldWaitForPopupClosure: true,
}).then(txData => {
expect(txData.confirmed).to.be.true;
});
cy.confirmMetamaskTransaction().then(txData => {
expect(txData.confirmed).to.be.true;
});
});
it(`confirmMetamaskTransaction should confirm legacy ETH transfer to yourself`, () => {
cy.get('#fromInput').type('0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266');
cy.get('#toInput').type('0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266');
Expand All @@ -373,28 +387,38 @@ describe('Metamask', () => {
});
it(`confirmMetamaskTransaction should confirm eip-1559 transaction using pre-defined (low, market, aggressive, site) gas settings`, () => {
cy.get('#sendEIP1559Button').click();
cy.confirmMetamaskTransaction('low').then(txData => {
cy.confirmMetamaskTransaction({
gasConfig: 'low',
}).then(txData => {
expect(txData.confirmed).to.be.true;
});
cy.get('#sendEIP1559Button').click();
cy.confirmMetamaskTransaction('market').then(txData => {
cy.confirmMetamaskTransaction({
gasConfig: 'market',
}).then(txData => {
expect(txData.confirmed).to.be.true;
});
cy.get('#sendEIP1559Button').click();
cy.confirmMetamaskTransaction('aggressive').then(txData => {
cy.confirmMetamaskTransaction({
gasConfig: 'aggressive',
}).then(txData => {
expect(txData.confirmed).to.be.true;
});
cy.get('#sendEIP1559Button').click();
cy.confirmMetamaskTransaction('site').then(txData => {
cy.confirmMetamaskTransaction({
gasConfig: 'site',
}).then(txData => {
expect(txData.confirmed).to.be.true;
});
});
it(`confirmMetamaskTransaction should confirm eip-1559 transaction using advanced gas settings`, () => {
cy.get('#sendEIP1559Button').click();
cy.confirmMetamaskTransaction({
gasLimit: 210000,
baseFee: 100,
priorityFee: 10,
gasConfig: {
gasLimit: 210000,
baseFee: 100,
priorityFee: 10,
},
}).then(txData => {
expect(txData.confirmed).to.be.true;
});
Expand Down Expand Up @@ -526,6 +550,18 @@ describe('Metamask', () => {
expect(approved).to.be.true;
});
});
it(`confirmMetamaskPermissionToSpend should work for serial transactions`, () => {
cy.get('#approveTokens').click();
cy.get('#approveTokens').click();
cy.confirmMetamaskPermissionToSpend({
shouldWaitForPopupClosure: true,
}).then(approved => {
expect(approved).to.be.true;
});
cy.confirmMetamaskPermissionToSpend().then(approved => {
expect(approved).to.be.true;
});
});
it(`rejectMetamaskToAddNetwork should reject permission to add network`, () => {
cy.get('#addEthereumChain').click();
cy.rejectMetamaskToAddNetwork().then(rejected => {
Expand Down

0 comments on commit 7d7a776

Please sign in to comment.