Skip to content

Commit

Permalink
address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
chloeYue committed Sep 25, 2024
1 parent 0867c16 commit 74f6504
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 64 deletions.
38 changes: 26 additions & 12 deletions test/e2e/page-objects/flows/login.flow.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
import LoginPage from '../pages/login-page';
import HomePage from '../pages/homepage';
import { Driver } from '../../webdriver/driver';
import { DEFAULT_GANACHE_ETH_BALANCE_DEC } from '../../constants';
import { WALLET_PASSWORD } from '../../helpers';
import { Ganache } from '../../seeder/ganache';

/**
* This method unlocks the wallet and verifies that the user lands on the homepage with the expected balance. It is designed to be the initial step in setting up a test environment.
* This method unlocks the wallet and lands the user on the homepage.
*
* @param driver - The webdriver instance.
* @param expectedBalance - The expected balance to be displayed on the homepage after successful login. Defaults to DEFAULT_GANACHE_ETH_BALANCE_DEC, reflecting common usage in test setups.
* @param password - The password used to unlock the wallet. Defaults to WALLET_PASSWORD.
* @param password - The password used to unlock the wallet.
*/
export const loginWithBalanceValidation = async (
export const loginWithoutBalanceValidation = async (
driver: Driver,
expectedBalance: string = DEFAULT_GANACHE_ETH_BALANCE_DEC,
password: string = WALLET_PASSWORD,
password?: string,
) => {
console.log('Navigate to unlock page and try to login with pasword');
console.log('Navigate to unlock page and try to login with password');
await driver.navigate();
const loginPage = new LoginPage(driver);
await loginPage.check_pageIsLoaded();
await loginPage.fillPassword(password);
await loginPage.clickUnlockButton();
await loginPage.loginToHomepage(password);

// user should land on homepage after successfully logging in with password
const homePage = new HomePage(driver);
await homePage.check_pageIsLoaded();
await homePage.check_expectedBalanceIsDisplayed(expectedBalance);
};

/**
* This method unlocks the wallet and verifies that the user lands on the homepage with the expected balance. It is designed to be the initial step in setting up a test environment.
*
* @param driver - The webdriver instance.
* @param ganacheServer - The ganache server instance
* @param password - The password used to unlock the wallet.
*/
export const loginWithBalanceValidation = async (
driver: Driver,
ganacheServer?: Ganache,
password?: string,
) => {
await loginWithoutBalanceValidation(driver, password);
// Verify the expected balance on the homepage
if (ganacheServer) {
await new HomePage(driver).check_ganacheBalanceIsDisplayed(ganacheServer);
}
};
55 changes: 34 additions & 21 deletions test/e2e/page-objects/pages/homepage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { strict as assert } from 'assert';
import { Driver } from '../../webdriver/driver';
import { DEFAULT_GANACHE_ETH_BALANCE_DEC } from '../../constants';
import { Ganache } from '../../seeder/ganache';
import HeaderNavbar from './header-navbar';

class HomePage {
Expand Down Expand Up @@ -52,26 +52,6 @@ class HomePage {
console.log('Home page is loaded');
}

async check_expectedBalanceIsDisplayed(
expectedBalance: string = DEFAULT_GANACHE_ETH_BALANCE_DEC,
): Promise<void> {
try {
await this.driver.waitForSelector({
css: this.balance,
text: `${expectedBalance} ETH`,
});
} catch (e) {
const balance = await this.driver.waitForSelector(this.balance);
const currentBalance = parseFloat(await balance.getText());
const errorMessage = `Expected balance ${expectedBalance} ETH, got balance ${currentBalance} ETH`;
console.log(errorMessage, e);
throw e;
}
console.log(
`Expected balance ${expectedBalance} ETH is displayed on homepage`,
);
}

async startSendFlow(): Promise<void> {
await this.driver.clickElement(this.sendButton);
}
Expand Down Expand Up @@ -129,6 +109,39 @@ class HomePage {
);
}

async check_expectedBalanceIsDisplayed(
expectedBalance: string,
): Promise<void> {
try {
await this.driver.waitForSelector({
css: this.balance,
text: `${expectedBalance} ETH`,
});
} catch (e) {
const balance = await this.driver.waitForSelector(this.balance);
const currentBalance = parseFloat(await balance.getText());
const errorMessage = `Expected balance ${expectedBalance} ETH, got balance ${currentBalance} ETH`;
console.log(errorMessage, e);
throw e;
}
console.log(
`Expected balance ${expectedBalance} ETH is displayed on homepage`,
);
}

async check_ganacheBalanceIsDisplayed(
ganacheServer?: Ganache,
address = null,
): Promise<void> {
let expectedBalance: string;
if (ganacheServer) {
expectedBalance = (await ganacheServer.getBalance(address)).toString();
} else {
expectedBalance = '0';
}
await this.check_expectedBalanceIsDisplayed(expectedBalance);
}

/**
* This function checks if a specified transaction amount at the specified index matches the expected one.
*
Expand Down
12 changes: 8 additions & 4 deletions test/e2e/page-objects/pages/login-page.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Driver } from '../../webdriver/driver';
import { WALLET_PASSWORD } from '../../helpers';

class LoginPage {
private driver: Driver;
Expand Down Expand Up @@ -39,11 +40,14 @@ class LoginPage {
console.log('Login page is loaded');
}

async fillPassword(password: string): Promise<void> {
/**
* This method unlocks the wallet and lands user on the homepage.
*
* @param password - The password used to unlock the wallet. Defaults to WALLET_PASSWORD.
*/
async loginToHomepage(password: string = WALLET_PASSWORD): Promise<void> {
console.log(`On login page, Login to homepage `);
await this.driver.fill(this.passwordInput, password);
}

async clickUnlockButton(): Promise<void> {
await this.driver.clickElement(this.unlockButton);
}

Expand Down
4 changes: 2 additions & 2 deletions test/e2e/tests/account/account-custom-name.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('Account Custom Name Persistence', function (this: Suite) {
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await loginWithBalanceValidation(driver, '0');
await loginWithBalanceValidation(driver);

const headerNavbar = new HeaderNavbar(driver);
await headerNavbar.openAccountMenu();
Expand Down Expand Up @@ -45,7 +45,7 @@ describe('Account Custom Name Persistence', function (this: Suite) {

// Lock and unlock wallet
await headerNavbar.lockMetaMask();
await loginWithBalanceValidation(driver, '0');
await loginWithBalanceValidation(driver);

// Verify both account labels persist after unlock
await headerNavbar.check_accountLabel(newAccountLabel);
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/tests/account/account-hide-unhide.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Account list - hide/unhide functionality', function (this: Suite) {
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await loginWithBalanceValidation(driver, '0');
await loginWithBalanceValidation(driver);
new HeaderNavbar(driver).openAccountMenu();

// hide account
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/tests/account/account-pin-unpin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('Account list - pin/unpin functionality', function (this: Suite) {
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await loginWithBalanceValidation(driver, '0');
await loginWithBalanceValidation(driver);
new HeaderNavbar(driver).openAccountMenu();

// pin account
Expand All @@ -39,7 +39,7 @@ describe('Account list - pin/unpin functionality', function (this: Suite) {
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await loginWithBalanceValidation(driver, '0');
await loginWithBalanceValidation(driver);
new HeaderNavbar(driver).openAccountMenu();

// pin account
Expand Down
18 changes: 10 additions & 8 deletions test/e2e/tests/account/forgot-password.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { defaultGanacheOptions, withFixtures } from '../../helpers';
import FixtureBuilder from '../../fixture-builder';
import { Driver } from '../../webdriver/driver';
import { DEFAULT_GANACHE_ETH_BALANCE_DEC } from '../../constants';
import { E2E_SRP } from '../../default-fixture';
import { Ganache } from '../../seeder/ganache';
import HomePage from '../../page-objects/pages/homepage';
import LoginPage from '../../page-objects/pages/login-page';
import ResetPasswordPage from '../../page-objects/pages/reset-password-page';
Expand All @@ -18,8 +18,14 @@ describe('Forgot password', function () {
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await loginWithBalanceValidation(driver);
async ({
driver,
ganacheServer,
}: {
driver: Driver;
ganacheServer?: Ganache;
}) => {
await loginWithBalanceValidation(driver, ganacheServer);

// Lock Wallet
const homePage = new HomePage(driver);
Expand All @@ -36,11 +42,7 @@ describe('Forgot password', function () {
await homePage.headerNavbar.lockMetaMask();

// Check user can log in with new password
await loginWithBalanceValidation(
driver,
DEFAULT_GANACHE_ETH_BALANCE_DEC,
newPassword,
);
await loginWithBalanceValidation(driver, ganacheServer, newPassword);
},
);
});
Expand Down
13 changes: 10 additions & 3 deletions test/e2e/tests/account/lock-account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { defaultGanacheOptions, withFixtures } from '../../helpers';
import FixtureBuilder from '../../fixture-builder';
import HomePage from '../../page-objects/pages/homepage';
import { Driver } from '../../webdriver/driver';
import { Ganache } from '../../seeder/ganache';
import { loginWithBalanceValidation } from '../../page-objects/flows/login.flow';

describe('Lock and unlock', function (this: Suite) {
Expand All @@ -13,11 +14,17 @@ describe('Lock and unlock', function (this: Suite) {
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await loginWithBalanceValidation(driver);
async ({
driver,
ganacheServer,
}: {
driver: Driver;
ganacheServer?: Ganache;
}) => {
await loginWithBalanceValidation(driver, ganacheServer);
const homePage = new HomePage(driver);
await homePage.headerNavbar.lockMetaMask();
await loginWithBalanceValidation(driver);
await loginWithBalanceValidation(driver, ganacheServer);
},
);
});
Expand Down
13 changes: 10 additions & 3 deletions test/e2e/tests/account/migrate-old-vault.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Suite } from 'mocha';
import { defaultGanacheOptions, withFixtures } from '../../helpers';
import FixtureBuilder from '../../fixture-builder';
import { Driver } from '../../webdriver/driver';
import { Ganache } from '../../seeder/ganache';
import HomePage from '../../page-objects/pages/homepage';
import { loginWithBalanceValidation } from '../../page-objects/flows/login.flow';

Expand All @@ -13,11 +14,17 @@ describe('Migrate vault with old encryption', function (this: Suite) {
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await loginWithBalanceValidation(driver);
async ({
driver,
ganacheServer,
}: {
driver: Driver;
ganacheServer?: Ganache;
}) => {
await loginWithBalanceValidation(driver, ganacheServer);
const homePage = new HomePage(driver);
await homePage.headerNavbar.lockMetaMask();
await loginWithBalanceValidation(driver);
await loginWithBalanceValidation(driver, ganacheServer);
},
);
});
Expand Down
2 changes: 1 addition & 1 deletion test/e2e/tests/account/snap-account-settings.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('Add snap account experimental settings @no-mmi', function (this: Suite
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await loginWithBalanceValidation(driver, '0');
await loginWithBalanceValidation(driver);

// Make sure the "Add snap account" button is not visible.
const headerNavbar = new HeaderNavbar(driver);
Expand Down
8 changes: 5 additions & 3 deletions test/e2e/tests/transaction/ens.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { MockttpServer } from 'mockttp';
import { defaultGanacheOptions, withFixtures } from '../../helpers';
import { Driver } from '../../webdriver/driver';
import FixtureBuilder from '../../fixture-builder';
import { loginWithBalanceValidation } from '../../page-objects/flows/login.flow';
import { loginWithoutBalanceValidation } from '../../page-objects/flows/login.flow';
import HomePage from '../../page-objects/pages/homepage';
import SendTokenPage from '../../page-objects/pages/send/send-token-page';
import { mockServerJsonRpc } from '../ppom/mocks/mock-server-json-rpc';
Expand Down Expand Up @@ -108,10 +108,12 @@ describe('ENS', function (this: Suite) {
testSpecificMock: mockInfura,
},
async ({ driver }: { driver: Driver }) => {
await loginWithBalanceValidation(driver, '<0.000001');
await loginWithoutBalanceValidation(driver);

// click send button on homepage to start send flow
await new HomePage(driver).startSendFlow();
const homepage = new HomePage(driver);
await homepage.check_expectedBalanceIsDisplayed('<0.000001');
await homepage.startSendFlow();

// fill ens address as recipient when user lands on send token screen
const sendToPage = new SendTokenPage(driver);
Expand Down
11 changes: 9 additions & 2 deletions test/e2e/tests/transaction/simple-send.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Suite } from 'mocha';
import { Driver } from '../../webdriver/driver';
import { Ganache } from '../../seeder/ganache';
import { withFixtures, defaultGanacheOptions } from '../../helpers';
import FixtureBuilder from '../../fixture-builder';
import { loginWithBalanceValidation } from '../../page-objects/flows/login.flow';
Expand All @@ -14,8 +15,14 @@ describe('Simple send eth', function (this: Suite) {
ganacheOptions: defaultGanacheOptions,
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await loginWithBalanceValidation(driver);
async ({
driver,
ganacheServer,
}: {
driver: Driver;
ganacheServer?: Ganache;
}) => {
await loginWithBalanceValidation(driver, ganacheServer);
await sendTransaction(
driver,
'0x985c30949c92df7a0bd42e0f3e3d539ece98db24',
Expand Down
11 changes: 9 additions & 2 deletions test/e2e/tests/transaction/stuck-approved-transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Suite } from 'mocha';
import { withFixtures, generateGanacheOptions } from '../../helpers';
import FixtureBuilder from '../../fixture-builder';
import { Driver } from '../../webdriver/driver';
import { Ganache } from '../../seeder/ganache';
import { loginWithBalanceValidation } from '../../page-objects/flows/login.flow';
import HomePage from '../../page-objects/pages/homepage';

Expand All @@ -15,8 +16,14 @@ describe('Editing Confirm Transaction', function (this: Suite) {
ganacheOptions: generateGanacheOptions({ hardfork: 'london' }),
title: this.test?.fullTitle(),
},
async ({ driver }: { driver: Driver }) => {
await loginWithBalanceValidation(driver);
async ({
driver,
ganacheServer,
}: {
driver: Driver;
ganacheServer?: Ganache;
}) => {
await loginWithBalanceValidation(driver, ganacheServer);

const homePage = new HomePage(driver);
await homePage.goToActivityList();
Expand Down

0 comments on commit 74f6504

Please sign in to comment.