Skip to content

Commit

Permalink
Merge pull request #684 from OasisDEX/earn-protocol-position-page
Browse files Browse the repository at this point in the history
Earn protocol - Tests for position pages
  • Loading branch information
juan-langa authored Dec 12, 2024
2 parents abaf529 + d823e48 commit 008ffc0
Show file tree
Hide file tree
Showing 10 changed files with 180 additions and 21 deletions.
4 changes: 4 additions & 0 deletions srcEarnProtocol/pages/positionPage/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { Page } from '@playwright/test';
import { VaultExposure } from './vaultExposure';
import { VaultSidebar } from '../vaultSidebar';

export class PositionPage {
readonly page: Page;

readonly exposure: VaultExposure;

readonly sideBar: VaultSidebar;

constructor(page: Page) {
this.page = page;
this.exposure = new VaultExposure(page);
this.sideBar = new VaultSidebar(page, this.page.locator('[class*="_sidebarWrapper_"]'));
}
}
43 changes: 43 additions & 0 deletions srcEarnProtocol/pages/positionPage/vaultExposure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { expect, step } from '#noWalletFixtures';
import { Locator, Page } from '@playwright/test';

export class VaultExposure {
readonly page: Page;

readonly vaultExposureLocator: Locator;

constructor(page: Page) {
this.page = page;
this.vaultExposureLocator = page.locator('button:has-text("Vault exposure")').locator('..');
}

@step
async viewMore() {
await this.vaultExposureLocator.getByText('View more').click();
expect(this.vaultExposureLocator.getByText('View less')).toBeVisible();
}

@step
async viewLess() {
await this.vaultExposureLocator.getByText('View less').click();
expect(this.vaultExposureLocator.getByText('View more')).toBeVisible();
}

@step
async getStrategiesTotalAllocation() {
const strategyAllocationLocator = this.vaultExposureLocator.locator('tr > td:nth-child(2)');
// Wait for table to load
await expect(strategyAllocationLocator.first()).toBeVisible();

const viewMoreIsVisible = await this.vaultExposureLocator.getByText('View more').isVisible();
if (viewMoreIsVisible) await this.viewMore();

const allocations = (await strategyAllocationLocator.allInnerTexts()).map((text) =>
parseFloat(text.replace('%', ''))
);

const totalAllocation = allocations.reduce((a, b) => a + b, 0);

return totalAllocation;
}
}
27 changes: 26 additions & 1 deletion srcEarnProtocol/pages/vaultSidebar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ export class VaultSidebar {
this.sideBarLocator = sideBarLocator;
}

@step
async openBalanceTokens() {
await expect(async () => {
await this.sideBarLocator.locator('[class*="_dropdownSelected_"]').click();
await expect(
this.sideBarLocator.locator('[class*="_dropdownOptions_"]'),
'Tokens drop-downshould be visible'
).toBeVisible();
}).toPass();
}

@step
async selectBalanceToken(token: EarnTokens) {
await this.sideBarLocator.locator(`[class*="_dropdownOption_"]:has-text("${token}")`).click();
}

@step
async shouldHaveBalance({
balance,
Expand All @@ -21,11 +37,20 @@ export class VaultSidebar {
}: {
balance: string;
token: EarnTokens;
timeout: number;
timeout?: number;
}) {
const regExp = new RegExp(`${balance}.*${token}`);
await expect(this.sideBarLocator.getByText('Balance').locator('..')).toContainText(regExp, {
timeout: timeout ?? expectDefaultTimeout,
});
}

@step
async changeNetwork(options?: { delay: number }) {
await expect(this.sideBarLocator.getByRole('button', { name: 'Change network' })).toBeVisible();
if (options?.delay) {
await this.page.waitForTimeout(options.delay);
}
await this.sideBarLocator.getByRole('button', { name: 'Change network' }).click();
}
}
13 changes: 13 additions & 0 deletions srcEarnProtocol/utils/logIn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,29 @@ export const logInWithWalletAddress = async ({
metamask,
app,
wallet,
network, // This property should be removed in the future
}: {
metamask: MetaMask;
app: App;
wallet: walletTypes;
network?: 'Arbitrum';
}) => {
await app.header.logIn();
await app.modals.logIn.withWallet();
await app.modals.logIn.selectWallet(wallet);
await metamask.connectToDapp();

// TO BE REMOVED in the future
if (network) {
// Earn protocol app switches to Base by default
await metamask.approveNewNetwork();
await metamask.approveSwitchNetwork();

// SKIPPED - Weird issue with Arbitrumwhen switching network in main page (/)
// // Switch back to Arbitrum
// await metamask.switchNetwork('arbitrum');
}

// App doesn't reload when loging in at the moment
await app.header.shouldHaveWalletAddress('0x1064...4743F');
await app.page.reload();
Expand Down
2 changes: 1 addition & 1 deletion tests/withWallet/ajna/arbitrum/ajnaArbitrumEarn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test.describe('Ajna Arbitrum Earn - Wallet connected', async () => {
await tenderly.deleteFork(forkId);
});

test('It should open an Ajna Arbitrum Earn position @regression', async ({ metamask, page }) => {
test('It should open an Ajna Arbitrum Earn position @regression', async ({ metamask }) => {
test.setTimeout(extremelyLongTestTimeout);

await app.position.openPage('/arbitrum/ajna/earn/USDC-WBTC#setup');
Expand Down
14 changes: 14 additions & 0 deletions testsEarnProtocol/noWallet/positionPageArbitrum.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect, test } from '#earnProtocolFixtures';

test.describe(`Earn page`, async () => {
test.beforeEach(async ({ app }) => {
await app.page.goto('/earn/arbitrum/position/earn-mcyieldface-usdc');
});

test('It should show strategies exposure and be 100% in total', async ({ app }) => {
const totalAllocation = await app.positionPage.exposure.getStrategiesTotalAllocation();

expect(totalAllocation).toBeCloseTo(100);
expect(totalAllocation).toBeLessThanOrEqual(100);
});
});
14 changes: 14 additions & 0 deletions testsEarnProtocol/noWallet/positionPageBase.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { expect, test } from '#earnProtocolFixtures';

test.describe(`Earn page`, async () => {
test.beforeEach(async ({ app }) => {
await app.page.goto('/earn/base/position/usdc-ya-later');
});

test('It should show strategies exposure and be 100% in total', async ({ app }) => {
const totalAllocation = await app.positionPage.exposure.getStrategiesTotalAllocation();

expect(totalAllocation).toBeCloseTo(100);
expect(totalAllocation).toBeLessThanOrEqual(100);
});
});
51 changes: 51 additions & 0 deletions testsEarnProtocol/withRealWallet/arbitrum/positionPage.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { testWithSynpress } from '@synthetixio/synpress';
import { test as withWalletArbitrumFixtures } from '../../../srcEarnProtocol/fixtures/withTestWalletArbitrum';
import { logInWithWalletAddress } from 'srcEarnProtocol/utils/logIn';
import { expectDefaultTimeout, longTestTimeout } from 'utils/config';

const test = testWithSynpress(withWalletArbitrumFixtures);

const { expect } = test;

test.describe('With reaal wallet - Arbitrum', async () => {
test.beforeEach(async ({ app, metamask }) => {
await logInWithWalletAddress({
metamask,
app,
wallet: 'MetaMask',
network: 'Arbitrum',
});
});

test('It should show USDC balance in Arbitrum USDC position', async ({ app, metamask }) => {
await app.page.goto('/earn/arbitrum/position/earn-mcyieldface-usdc');

/*
Changing network in position page to avoid weird issue with Arbitrum
when switching network in main page (/) and the visiting position page
*/
await app.positionPage.sideBar.changeNetwork({ delay: 500 });
await metamask.approveSwitchNetwork();
await app.page.reload();

await app.positionPage.sideBar.shouldHaveBalance({
balance: '1.00',
token: 'USDC',
timeout: expectDefaultTimeout * 2,
});
});

// TODO - Add one or more tokens once it'sworking
// test.skip('It should show ??? balance in Arbitrum USDC position', async ({ app }) => {
// await app.page.goto('/earn/arbitrum/position/earn-mcyieldface-usdc');

// await app.positionPage.sideBar.openBalanceTokens();
// await app.positionPage.sideBar.selectBalanceToken('USDBC');

// await app.positionPage.sideBar.shouldHaveBalance({
// balance: '1.05',
// token: 'USDBC',
// timeout: expectDefaultTimeout * 2,
// });
// });
});

This file was deleted.

15 changes: 14 additions & 1 deletion testsEarnProtocol/withRealWallet/base/positionPage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { testWithSynpress } from '@synthetixio/synpress';
import { test as withRealWalletBaseFixtures } from '../../../../srcEarnProtocol/fixtures/withRealWalletBase';
import { test as withRealWalletBaseFixtures } from '../../../srcEarnProtocol/fixtures/withRealWalletBase';
import { logInWithWalletAddress } from 'srcEarnProtocol/utils/logIn';
import { expectDefaultTimeout } from 'utils/config';

Expand All @@ -25,4 +25,17 @@ test.describe('With reaal wallet - Base', async () => {
timeout: expectDefaultTimeout * 2,
});
});

test('It should show USDBC balance in Base USDC position', async ({ app }) => {
await app.page.goto('/earn/base/position/usdc-ya-later');

await app.positionPage.sideBar.openBalanceTokens();
await app.positionPage.sideBar.selectBalanceToken('USDBC');

await app.positionPage.sideBar.shouldHaveBalance({
balance: '1.05',
token: 'USDBC',
timeout: expectDefaultTimeout * 2,
});
});
});

0 comments on commit 008ffc0

Please sign in to comment.