TenKeyLabs / dappwright

🏌🏼‍E2E testing for dApps using Playwright + MetaMask & Coinbase Wallet
https://www.npmjs.com/package/@tenkeylabs/dappwright
Other
89 stars 26 forks source link

Metamask approve spending #234

Open panteo opened 1 year ago

panteo commented 1 year ago

Problem Till Metamask v10 the spending approval was done in one step, so the confirmTransaction action could be used for this, but now, with Metamask v11.3.0 it is done in two steps and a new action is needed.

Screenshot from 2023-10-23 14-23-09 Screenshot from 2023-10-23 14-23-15

Possible solution Add a new action: approveSpending, similar to the confirmTransaction, but with two clicks on page-container-footer-next.

Alternative considered I considered to simply call twice the confirmTransaction in my test, but it is not working since it is expecting to close the popup after the first one, and this is not the case.

Code snippet

If options are not required in this case, the action could be as easy as follow:

import { Page } from 'playwright-core';

import { performPopupAction } from './util';

export const approveSpending = (page: Page) => async (): Promise<void> => {
  await performPopupAction(page, async (popup) => {
    await popup.getByTestId('page-container-footer-next').click(); // Next
    await popup.getByTestId('page-container-footer-next').click(); // Approve
  });
};
osis commented 1 year ago

Hmmmmm that's odd since the tests pass. Mind setting up a PR? Would rather select the right element to click though.

panteo commented 1 year ago

Sure, I'll do it :muscle:

The tests pass probably because the spending approval is not tested, or at least I was not able to find the test.

alejogranerorequest commented 1 year ago

@panteo . Could it be related to https://github.com/TenKeyLabs/dappwright/issues/227#issuecomment-1775141409?

panteo commented 1 year ago

@panteo . Could it be related to https://github.com/TenKeyLabs/dappwright/issues/227#issuecomment-1775141409?

Don't think so @alejogranerorequest .

panteo commented 1 year ago

I solved it by manually interacting with the popup in my tests. Until the library includes a new action to approve spending, you can use this helper:

const approveSpending = async (page: Page) => {
  const popup = await page.context().waitForEvent("page");
  await popup.getByRole("button", { name: "Next" }).click();
  await popup.getByRole("button", { name: "Approve" }).click();
};

I am not using the data-testid because it doesn't change between "Next" and "Approve" and sometimes we end up in an unpredictable race condition, generating flaky results.

osis commented 1 year ago

Looks like it'll be a good addition! 👏🏾

Mouse-Coach commented 2 months ago

Hi guys, Is there an update here?

osis commented 2 months ago

I think @panteo lined up a solution for a PR but I'm not sure they got around to it?

Mouse-Coach commented 2 months ago

I made the same thing he has shown here - thank you all. Buy now I faced with another one from another issue - still repro + with circle CI too (i've been doing with both) https://github.com/TenKeyLabs/dappwright/issues/269

GetRaider commented 2 months ago

It'd be nice to fix that - you just can't approve on CI