Skip to content

Commit

Permalink
✨ feat: Get account address helper with missing token transfers (#1107)
Browse files Browse the repository at this point in the history
## Motivation and context

Missing e2e tests for token transfers with a function to get account address.

## Quality checklist

- [x] I have performed a self-review of my code.
- [x] If it is a core feature, I have added thorough e2e tests.

**⚠️👆 Delete any section you see irrelevant before submitting the pull request 👆⚠️**


---

<details open="true"><summary>Generated summary</summary>

> ## TL;DR
> This pull request introduces a new feature to the MetaMask wallet that allows users to retrieve the current account address. This is achieved by adding a new method `getAccountAddress` to the `MetaMask` class and the corresponding action in the `HomePage` class.
> 
> ## What changed
> - A new method `getAccountAddress` was added to the `MetaMask` class.
> - A new action `getAccountAddress` was added to the `HomePage` class.
> - The `HomePage` class was updated to include the new `getAccountAddress` action.
> - The `HomePage` selectors were updated to include the selectors needed for the new action.
> - The e2e tests were updated to include tests for the new feature.
> 
> ## How to test
> 1. Pull the changes from this branch to your local environment.
> 2. Run the e2e tests using the command `npm run test:e2e`.
> 3. All tests should pass, including the new tests for the `getAccountAddress` feature.
> 
> ## Why make this change
> This change is necessary to allow users to retrieve their current account address from the MetaMask wallet. This is a common requirement for many dApps and will improve the usability of the wallet.
</details>
  • Loading branch information
matstyler authored Feb 25, 2024
1 parent e3c6e60 commit 6a65b9e
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
7 changes: 7 additions & 0 deletions wallets/metamask/src/metamask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ export class MetaMask {
await this.homePage.addNetwork(network)
}

/**
* Retrieves the current account address.
*/
async getAccountAddress() {
return await this.homePage.getAccountAddress()
}

/**
* Switches to the network with the given name.
*
Expand Down
13 changes: 13 additions & 0 deletions wallets/metamask/src/pages/HomePage/actions/getAccountAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import type { Page } from '@playwright/test'
import Selectors from '../selectors'

export default async function getAccountAddress(page: Page): Promise<string> {
await page.locator(Selectors.threeDotsMenu.threeDotsButton).click()
await page.locator(Selectors.threeDotsMenu.accountDetailsButton).click()

const account = await page.locator(Selectors.copyAccountAddressButton).last().innerText()

await page.locator(Selectors.threeDotsMenu.accountDetailsCloseButton).click()

return account
}
1 change: 1 addition & 0 deletions wallets/metamask/src/pages/HomePage/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './addNetwork'
export * from './toggleShowTestNetworks'
export * from './addNewAccount'
export * from './transactionDetails'
export { default as getAccountAddress } from './getAccountAddress'
5 changes: 5 additions & 0 deletions wallets/metamask/src/pages/HomePage/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Page } from '@playwright/test'
import {
addNetwork,
addNewAccount,
getAccountAddress,
importWalletFromPrivateKey,
lock,
settings,
Expand Down Expand Up @@ -36,6 +37,10 @@ export class HomePage {
await addNewAccount(this.page, accountName)
}

async getAccountAddress() {
return await getAccountAddress(this.page)
}

async importWalletFromPrivateKey(privateKey: string) {
await importWalletFromPrivateKey(this.page, privateKey)
}
Expand Down
4 changes: 3 additions & 1 deletion wallets/metamask/src/pages/HomePage/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ const accountMenu = {
const threeDotsMenu = {
threeDotsButton: createDataTestSelector('account-options-menu-button'),
settingsButton: createDataTestSelector('global-menu-settings'),
lockButton: createDataTestSelector('global-menu-lock')
lockButton: createDataTestSelector('global-menu-lock'),
accountDetailsButton: createDataTestSelector('account-list-menu-details'),
accountDetailsCloseButton: '.mm-modal-content .mm-modal-header button.mm-button-icon.mm-button-icon--size-sm'
}

const popoverContainer = '.popover-container'
Expand Down
29 changes: 29 additions & 0 deletions wallets/metamask/test/e2e/metamask/confirmTransaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,32 @@ describe('with custom gas setting', () => {
})
})
})

describe('with `from` and `to` specified', () => {
test('should confirm from/to transfer', async ({ page, metamask, deployToken }) => {
await deployToken()

const accountAddress = await metamask.getAccountAddress()
await page.locator('#transferFromSenderInput').fill(accountAddress)
await page.locator('#transferFromRecipientInput').fill('0x70997970C51812dc3A010C7d01b50e0d17dc79C8')

await page.locator('#transferFromTokens').click()
await metamask.confirmTransaction()
})
})

describe('without gas limit', () => {
test('should approve tokens', async ({ page, metamask, deployToken }) => {
await deployToken()

await page.locator('#approveTokensWithoutGas').click()
await metamask.approveTokenPermission()
})

test('should transfer tokens', async ({ page, metamask, deployToken }) => {
await deployToken()

await page.locator('#transferTokensWithoutGas').click()
await metamask.confirmTransaction()
})
})

0 comments on commit 6a65b9e

Please sign in to comment.