From 70efbff4866c73ce55b823be0703cbd800734c69 Mon Sep 17 00:00:00 2001 From: Nelito Junior Date: Tue, 19 Nov 2024 20:28:36 -0300 Subject: [PATCH] test(e2e): Improve burner wallet tests FE-859 (#428) --- e2e-tests/runner/common/common.ts | 12 ++-- e2e-tests/runner/common/types.ts | 1 + .../BurnerWalletConnector.test.ts | 66 ++++++++++++++++++- e2e-tests/runner/examples/connectors/setup.ts | 12 +--- 4 files changed, 75 insertions(+), 16 deletions(-) diff --git a/e2e-tests/runner/common/common.ts b/e2e-tests/runner/common/common.ts index e8505300..587c67cd 100644 --- a/e2e-tests/runner/common/common.ts +++ b/e2e-tests/runner/common/common.ts @@ -48,9 +48,9 @@ export const sessionTests = async ( // biome-ignore lint/suspicious/noExportsInTest: export const transferTests = async ( page: Page, - { connect, approveTransfer }: ConnectorFunctions, + { connect, approveTransfer, keepSession }: ConnectorFunctions, ) => { - await connect(page); + !keepSession && (await connect(page)); await page.click('text=Transfer 0.0001 ETH'); await approveTransfer(page); @@ -59,16 +59,16 @@ export const transferTests = async ( await page.waitForSelector('text=Transferred successfully!'), ).toBeTruthy(); - await page.click('text=Disconnect'); + !keepSession && (await page.click('text=Disconnect')); }; // biome-ignore lint/suspicious/noExportsInTest: export const incrementTests = async ( page: Page, - { approveTransfer, connect }: ConnectorFunctions, + { approveTransfer, connect, keepSession }: ConnectorFunctions, ) => { await test.step('should connect and increment', async () => { - await connect(page); + !keepSession && (await connect(page)); const incrementButton = await page.getByRole('button', { name: 'Increment', @@ -81,5 +81,7 @@ export const incrementTests = async ( expect( await page.waitForSelector('text=Counter Incremented!'), ).toBeTruthy(); + + !keepSession && (await page.click('text=Disconnect')); }); }; diff --git a/e2e-tests/runner/common/types.ts b/e2e-tests/runner/common/types.ts index 68ce87a7..bd804467 100644 --- a/e2e-tests/runner/common/types.ts +++ b/e2e-tests/runner/common/types.ts @@ -11,4 +11,5 @@ export type ApproveTransferFunction = (page: Page) => Promise; export interface ConnectorFunctions { connect: ConnectFunction; approveTransfer: ApproveTransferFunction; + keepSession?: boolean; } diff --git a/e2e-tests/runner/examples/connectors/BurnerWalletConnector/BurnerWalletConnector.test.ts b/e2e-tests/runner/examples/connectors/BurnerWalletConnector/BurnerWalletConnector.test.ts index cf63c348..79a66812 100644 --- a/e2e-tests/runner/examples/connectors/BurnerWalletConnector/BurnerWalletConnector.test.ts +++ b/e2e-tests/runner/examples/connectors/BurnerWalletConnector/BurnerWalletConnector.test.ts @@ -1,7 +1,24 @@ -import { getButtonByText, getByAriaLabel, test } from '@fuels/playwright-utils'; +import { + getButtonByText, + getByAriaLabel, + seedWallet, + test, +} from '@fuels/playwright-utils'; import { type Page, expect } from '@playwright/test'; -import { sessionTests, skipBridgeFunds } from '../../../common/common'; +import { type BN, Provider, Wallet, bn } from 'fuels'; +import { + incrementTests, + sessionTests, + skipBridgeFunds, + transferTests, +} from '../../../common/common'; import type { ConnectFunction } from '../../../common/types'; +import { transferMaxBalance } from '../setup'; + +const { VITE_FUEL_PROVIDER_URL, VITE_WALLET_SECRET } = process.env as Record< + string, + string +>; const connect: ConnectFunction = async (page: Page) => { await page.bringToFront(); @@ -22,5 +39,50 @@ test.describe('BurnerWalletConnector', async () => { test('BurnerWallet Tests', async ({ page }) => { await sessionTests(page, { connect, approveTransfer: async () => {} }); + await connect(page); + + const addressElement = await page.locator('css=#address'); + + const address = await addressElement.getAttribute('data-address'); + const amount: BN = bn(100_000_000); + + if (address) { + await seedWallet( + address, + amount, + VITE_FUEL_PROVIDER_URL || '', + VITE_WALLET_SECRET || '', + ); + } else { + throw new Error('Address is null'); + } + + await incrementTests(page, { + connect, + approveTransfer: async () => {}, + keepSession: true, + }); + + await transferTests(page, { + connect, + approveTransfer: async () => {}, + keepSession: true, + }); + + const privateKey = await page.evaluate(() => + localStorage.getItem('burner-wallet-private-key'), + ); + + if (!privateKey) { + throw new Error('Private key is null'); + } + + const burnerWallet = Wallet.fromPrivateKey(privateKey); + const fuelProvider = await Provider.create(VITE_FUEL_PROVIDER_URL); + burnerWallet.connect(fuelProvider); + await transferMaxBalance({ + fromWallet: burnerWallet, + toWallet: Wallet.fromPrivateKey(VITE_WALLET_SECRET || ''), + }); }); }); diff --git a/e2e-tests/runner/examples/connectors/setup.ts b/e2e-tests/runner/examples/connectors/setup.ts index 1cf74caf..37447d03 100644 --- a/e2e-tests/runner/examples/connectors/setup.ts +++ b/e2e-tests/runner/examples/connectors/setup.ts @@ -67,18 +67,12 @@ export const testSetup = async ({ }; export const fundWallet = async ({ publicKey }: { publicKey: string }) => { - const fuelProvider = await Provider.create(VITE_FUEL_PROVIDER_URL); - const masterWallet = Wallet.fromMnemonic(VITE_MASTER_WALLET_MNEMONIC); - masterWallet.connect(fuelProvider); - - const txResponse = await masterWallet.transfer( + await seedWallet( publicKey, bn.parseUnits('0.1'), + VITE_FUEL_PROVIDER_URL, + VITE_WALLET_SECRET, ); - - await txResponse.waitForResult(); - - return true; }; export const transferMaxBalance = async ({