Skip to content

Commit c0018fd

Browse files
committed
Playwright - switchAccount & getAccountAddress
1 parent f3cca9d commit c0018fd

File tree

7 files changed

+131
-17
lines changed

7 files changed

+131
-17
lines changed

wallets/phantom/playwright.config.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ export default defineConfig({
2626
// Concise 'dot' for CI, default 'html' when running locally.
2727
// See https://playwright.dev/docs/test-reporters.
2828
reporter: process.env.CI
29-
? [['html', { open: 'never', outputFolder: `playwright-report-${process.env.HEADLESS ? 'headless' : 'headful'}` }]]
29+
? [
30+
[
31+
'html',
32+
{
33+
open: 'never',
34+
outputFolder: `playwright-report-${process.env.HEADLESS ? 'headless' : 'headful'}`
35+
}
36+
]
37+
]
3038
: 'html',
3139

3240
// Shared settings for all the projects below.
@@ -37,7 +45,9 @@ export default defineConfig({
3745

3846
// Collect all traces on CI, and only traces for failed tests when running locally.
3947
// See https://playwright.dev/docs/trace-viewer.
40-
trace: process.env.CI ? 'on' : 'retain-on-failure'
48+
trace: process.env.CI ? 'on' : 'retain-on-failure',
49+
// Added for getting account address
50+
permissions: ['clipboard-read']
4151
},
4252

4353
// Configure projects for major browsers.
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import type { Page } from '@playwright/test'
22
import Selectors from '../../../../selectors/pages/HomePage'
33

4+
// TODO - .getAccountAddress() to be updated for all networks
45
export default async function getAccountAddress(page: Page): Promise<string> {
5-
await page.locator(Selectors.threeDotsMenu.threeDotsButton).click()
6-
await page.locator(Selectors.threeDotsMenu.accountDetailsButton).click()
6+
// Copy account address to clipboard
7+
await page.locator(Selectors.accountMenu.accountName).hover()
8+
await page.locator(Selectors.ethereumWalletAddress).click()
79

8-
const account = await page.locator(Selectors.copyAccountAddressButton).last().innerText()
9-
10-
await page.locator(Selectors.threeDotsMenu.accountDetailsCloseButton).click()
10+
// Get clipboard content
11+
const handle = await page.evaluateHandle(() => navigator.clipboard.readText())
12+
const account = await handle.jsonValue()
1113

1214
return account
1315
}

wallets/phantom/src/playwright/pages/HomePage/actions/importWalletFromPrivateKey.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,22 @@ export async function importWalletFromPrivateKey(
2525

2626
await page
2727
.locator(Selectors.accountMenu.addAccountMenu.importAccountMenu.nameInput)
28-
.fill(walletName ?? 'MyImportedWallet')
28+
.fill(walletName ?? 'ImportedWallet')
2929

3030
await page.locator(Selectors.accountMenu.addAccountMenu.importAccountMenu.privateKeyInput).fill(privateKey)
3131

3232
const importButton = page.locator(Selectors.accountMenu.addAccountMenu.importAccountMenu.importButton)
33-
await importButton.click()
34-
35-
// TO DO
36-
// Verify that header account name contains `name` or `MyImportedWallet`
37-
// [data-testid="home-header-account-name"]
38-
//
3933

4034
// TODO: Extract & make configurable
41-
const isImportButtonHidden = await waitFor(() => importButton.isHidden(), 1_000, false)
35+
const isImportButtonEnabled = await waitFor(() => importButton.isEnabled(), 1_000, false)
4236

43-
if (!isImportButtonHidden) {
37+
if (!isImportButtonEnabled) {
4438
const errorText = await page.locator(Selectors.accountMenu.addAccountMenu.importAccountMenu.error).textContent({
4539
timeout: 1_000 // TODO: Extract & make configurable
4640
})
4741

4842
throw new Error(`[ImportWalletFromPrivateKey] Importing failed due to error: ${errorText}`)
4943
}
44+
45+
await importButton.click()
5046
}

wallets/phantom/src/selectors/pages/HomePage/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ const importAccountMenu = {
2020
bitcoinNetwork: `[data-label="Bitcoin"]`,
2121
nameInput: `input[name="name"]`,
2222
privateKeyInput: `textarea[placeholder="Private key"]`,
23-
importButton: `button:has-text("Import")`
23+
importButton: `button:has-text("Import")`,
24+
error: `textarea[placeholder="Private key"] + div`
2425
}
2526

2627
const addAccountMenu = {
@@ -90,6 +91,7 @@ const activityTab = {
9091
const singleToken = '.multichain-token-list-item'
9192

9293
export default {
94+
ethereumWalletAddress: createDataTestSelector('account-header-chain-eip155:1'),
9395
logo: `button${createDataTestSelector('app-header-logo')}`,
9496
copyAccountAddressButton: createDataTestSelector('address-copy-button-text'),
9597
currentNetwork: `${createDataTestSelector('network-display')} span:nth-of-type(1)`,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { testWithSynpress } from '@synthetixio/synpress-core'
2+
import { Phantom, phantomFixtures } from '../../../src/playwright'
3+
4+
import basicSetup from '../wallet-setup/basic.setup'
5+
6+
const test = testWithSynpress(phantomFixtures(basicSetup))
7+
8+
const { expect } = test
9+
10+
// TODO - .getAccountAddress() and Tests to be updated for all networks
11+
test('should get account address', async ({ context, phantomPage }) => {
12+
const phantom = new Phantom(context, phantomPage, basicSetup.walletPassword)
13+
14+
await phantom.importWalletFromPrivateKey(
15+
'ethereum',
16+
'ea084c575a01e2bbefcca3db101eaeab1d8af15554640a510c73692db24d0a6a'
17+
)
18+
19+
const accountAddress = await phantom.getAccountAddress()
20+
21+
expect(accountAddress).toEqual('0xa2ce797cA71d0EaE1be5a7EffD27Fd6C38126801')
22+
})
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { testWithSynpress } from '@synthetixio/synpress-core'
2+
import { Phantom, phantomFixtures } from '../../../src/playwright'
3+
4+
import basicSetup from '../wallet-setup/basic.setup'
5+
6+
const test = testWithSynpress(phantomFixtures(basicSetup))
7+
8+
const { expect } = test
9+
10+
test('should import a new wallet from private key', async ({ context, phantomPage }) => {
11+
const phantom = new Phantom(context, phantomPage, basicSetup.walletPassword)
12+
13+
await phantom.importWalletFromPrivateKey(
14+
'ethereum',
15+
'ea084c575a01e2bbefcca3db101eaeab1d8af15554640a510c73692db24d0a6a'
16+
)
17+
18+
await phantomPage.locator(phantom.homePage.selectors.accountMenu.accountName).hover()
19+
await expect(phantomPage.locator(phantom.homePage.selectors.ethereumWalletAddress)).toContainText('0xa2ce...6801')
20+
})
21+
22+
test('should throw an error if trying to import private key for the 2nd time', async ({ context, phantomPage }) => {
23+
const phantom = new Phantom(context, phantomPage, basicSetup.walletPassword)
24+
25+
const privateKey = 'ea084c575a01e2bbefcca3db101eaeab1d8af15554640a510c73692db24d0a6a'
26+
27+
await phantom.importWalletFromPrivateKey('ethereum', privateKey)
28+
29+
const importWalletPromise = phantom.importWalletFromPrivateKey('ethereum', privateKey)
30+
31+
await expect(importWalletPromise).rejects.toThrowError(
32+
'[ImportWalletFromPrivateKey] Importing failed due to error: This account already exists in your wallet'
33+
)
34+
})
35+
36+
test('should throw an error if the private key is invalid', async ({ context, phantomPage }) => {
37+
const phantom = new Phantom(context, phantomPage, basicSetup.walletPassword)
38+
39+
const importWalletPromise = phantom.importWalletFromPrivateKey('ethereum', '0xdeadbeef')
40+
41+
await expect(importWalletPromise).rejects.toThrowError(
42+
'[ImportWalletFromPrivateKey] Importing failed due to error: Incorrect format'
43+
)
44+
})
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { testWithSynpress } from '@synthetixio/synpress-core'
2+
import { Phantom, phantomFixtures } from '../../../src/playwright'
3+
4+
import basicSetup from '../wallet-setup/basic.setup'
5+
6+
const test = testWithSynpress(phantomFixtures(basicSetup))
7+
8+
const { expect } = test
9+
10+
test('should switch account', async ({ context, phantomPage }) => {
11+
const phantom = new Phantom(context, phantomPage, basicSetup.walletPassword)
12+
13+
await phantom.importWalletFromPrivateKey(
14+
'ethereum',
15+
'ea084c575a01e2bbefcca3db101eaeab1d8af15554640a510c73692db24d0a6a'
16+
)
17+
18+
await phantom.importWalletFromPrivateKey(
19+
'ethereum',
20+
'7dd4aab86170c0edbdcf97600eff0ae319fdc94149c5e8c33d5439f8417a40bf'
21+
)
22+
23+
await phantom.switchAccount('Account 1')
24+
25+
await expect(phantomPage.getByText('Account 1')).toBeVisible()
26+
27+
await phantomPage.locator(phantom.homePage.selectors.accountMenu.accountName).hover()
28+
await expect(phantomPage.locator(phantom.homePage.selectors.ethereumWalletAddress)).toContainText('0xf39F...2266')
29+
})
30+
31+
test('should throw an error if there is no account with target name', async ({ context, phantomPage }) => {
32+
const phantom = new Phantom(context, phantomPage, basicSetup.walletPassword)
33+
34+
const accountName = 'Account 420'
35+
const switchAccountPromise = phantom.switchAccount(accountName)
36+
37+
await expect(switchAccountPromise).rejects.toThrowError(`[SwitchAccount] Account with name ${accountName} not found`)
38+
})

0 commit comments

Comments
 (0)