Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[e2e] Using ganache requests to getBalance and getAccounts #18215

Merged
merged 4 commits into from
Mar 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions test/e2e/ganache.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ class Ganache {
return this._server.provider;
}

async getAccounts() {
return await this.getProvider().request({
method: 'eth_accounts',
params: [],
});
}

async getBalance() {
const accounts = await this.getAccounts();
const balanceHex = await this.getProvider().request({
method: 'eth_getBalance',
params: [accounts[0], 'latest'],
});
const balanceInt = parseInt(balanceHex, 16) / 10 ** 18;

const balanceFormatted =
balanceInt % 1 === 0 ? balanceInt : balanceInt.toFixed(4);
Copy link
Contributor Author

@seaona seaona Mar 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the balance is a float number, we round it up to 4 decimals to match UI.
If it's a whole number, do nothing


return balanceFormatted;
}

async quit() {
if (!this._server) {
throw new Error('Server not running yet');
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ async function withFixtures(options, testSuite) {
driver: driverProxy ?? driver,
mockServer,
contractRegistry,
ganacheServer,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we pass ganacheServer to the testsuite to be able to access its methods

secondaryGanacheServer,
});
} catch (error) {
failed = true;
Expand Down
10 changes: 5 additions & 5 deletions test/e2e/tests/contract-interactions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Deploy contract and call contract methods', function () {
smartContract,
title: this.test.title,
},
async ({ driver, contractRegistry }) => {
async ({ driver, contractRegistry, ganacheServer }) => {
const contractAddress = await contractRegistry.getContractAddress(
smartContract,
);
Expand Down Expand Up @@ -99,15 +99,15 @@ describe('Deploy contract and call contract methods', function () {

// renders the correct ETH balance
await driver.switchToWindow(extension);
const balance = await driver.waitForSelector(
const balance = await ganacheServer.getBalance();
const balanceElement = await driver.waitForSelector(
{
css: '[data-testid="eth-overview__primary-currency"]',
text: '21.',
text: balance,
},
{ timeout: 10000 },
);
const tokenAmount = await balance.getText();
assert.ok(/^21.*\s*ETH.*$/u.test(tokenAmount));
assert.equal(`${balance}\nETH`, await balanceElement.getText());
},
);
});
Expand Down
5 changes: 3 additions & 2 deletions test/e2e/tests/metamask-responsive-ui.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ describe('MetaMask Responsive UI', function () {
title: this.test.title,
failOnConsoleError: false,
},
async ({ driver }) => {
async ({ driver, ganacheServer }) => {
await driver.navigate();

// Import Secret Recovery Phrase
Expand All @@ -104,9 +104,10 @@ describe('MetaMask Responsive UI', function () {
await driver.press('#confirm-password', driver.Key.ENTER);

// balance renders
const balance = await ganacheServer.getBalance();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here we access the current balance of the acocunt, instead of hardcoding the balance

await driver.waitForSelector({
css: '[data-testid="eth-overview__primary-currency"]',
text: '1000 ETH',
text: `${balance} ETH`,
});
},
);
Expand Down
7 changes: 4 additions & 3 deletions test/e2e/tests/navigate-transactions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,18 +228,19 @@ describe('Navigate transactions', function () {
ganacheOptions,
title: this.test.title,
},
async ({ driver }) => {
async ({ driver, ganacheServer }) => {
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);

// reject transactions
await driver.clickElement({ text: 'Reject 4', tag: 'a' });
await driver.clickElement({ text: 'Reject all', tag: 'button' });
const balance = await driver.findElement(
const balance = await ganacheServer.getBalance();
const balanceElement = await driver.findElement(
'[data-testid="eth-overview__primary-currency"]',
);
assert.ok(/^25\sETH$/u.test(await balance.getText()));
assert.equal(`${balance}\nETH`, await balanceElement.getText());
},
);
});
Expand Down
7 changes: 4 additions & 3 deletions test/e2e/tests/onboarding.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ describe('MetaMask onboarding', function () {
title: this.test.title,
},

async ({ driver }) => {
async ({ driver, secondaryGanacheServer }) => {
await driver.navigate();
await importSRPOnboardingFlow(driver, testSeedPhrase, testPassword);

Expand Down Expand Up @@ -300,10 +300,11 @@ describe('MetaMask onboarding', function () {
);
assert.equal(await networkDisplay.getText(), networkName);

const balance1 = await driver.findElement(
const balance = await secondaryGanacheServer.getBalance();
const balanceElement = await driver.findElement(
'[data-testid="eth-overview__primary-currency"]',
);
assert.ok(/^10\sETH$/u.test(await balance1.getText()));
assert.equal(`${balance}\nETH`, await balanceElement.getText());
},
);
});
Expand Down
5 changes: 3 additions & 2 deletions test/e2e/tests/permissions.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ describe('Permissions', function () {
},
],
};
const publicAddress = '0x5cfe73b6021e818b776b421b1c4db2474086a7e1';
await withFixtures(
{
dapp: true,
fixtures: new FixtureBuilder().build(),
ganacheOptions,
title: this.test.title,
},
async ({ driver }) => {
async ({ driver, ganacheServer }) => {
const addresses = await ganacheServer.getAccounts();
const publicAddress = addresses[0];
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
Expand Down
5 changes: 3 additions & 2 deletions test/e2e/tests/personal-sign.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ describe('Personal sign', function () {
},
],
};
const publicAddress = '0x5cfe73b6021e818b776b421b1c4db2474086a7e1';
await withFixtures(
{
dapp: true,
Expand All @@ -23,7 +22,9 @@ describe('Personal sign', function () {
ganacheOptions,
title: this.test.title,
},
async ({ driver }) => {
async ({ driver, ganacheServer }) => {
const addresses = await ganacheServer.getAccounts();
const publicAddress = addresses[0];
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
Expand Down
9 changes: 4 additions & 5 deletions test/e2e/tests/provider-api.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ describe('MetaMask', function () {
ganacheOptions,
title: this.test.title,
},
async ({ driver }) => {
async ({ driver, ganacheServer }) => {
const addresses = await ganacheServer.getAccounts();
const publicAddress = addresses[0];
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
Expand Down Expand Up @@ -60,10 +62,7 @@ describe('MetaMask', function () {

assert.equal(await switchedNetworkDiv.getText(), '0x1');
assert.equal(await switchedChainIdDiv.getText(), '0x1');
assert.equal(
await accountsDiv.getText(),
'0x5cfe73b6021e818b776b421b1c4db2474086a7e1',
);
assert.equal(await accountsDiv.getText(), publicAddress);
},
);
});
Expand Down
10 changes: 6 additions & 4 deletions test/e2e/tests/send-hex-address.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,14 @@ describe('Send ERC20 to a 40 character hexadecimal address', function () {
title: this.test.title,
failOnConsoleError: false,
},
async ({ driver }) => {
async ({ driver, ganacheServer }) => {
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
const balanceAfterDeployment = await ganacheServer.getBalance();
await driver.waitForSelector({
css: '[data-testid="eth-overview__primary-currency"]',
text: '24.9977 ETH',
text: `${balanceAfterDeployment} ETH`,
});

// Send TST
Expand Down Expand Up @@ -194,13 +195,14 @@ describe('Send ERC20 to a 40 character hexadecimal address', function () {
title: this.test.title,
failOnConsoleError: false,
},
async ({ driver }) => {
async ({ driver, ganacheServer }) => {
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
const balanceAfterDeployment = await ganacheServer.getBalance();
await driver.waitForSelector({
css: '[data-testid="eth-overview__primary-currency"]',
text: '24.9977 ETH',
text: `${balanceAfterDeployment} ETH`,
});

// Send TST
Expand Down
15 changes: 9 additions & 6 deletions test/e2e/tests/signature-request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ describe('Sign Typed Data V4 Signature Request', function () {
},
],
};
const publicAddress = '0x5cfe73b6021e818b776b421b1c4db2474086a7e1';
await withFixtures(
{
dapp: true,
Expand All @@ -27,7 +26,9 @@ describe('Sign Typed Data V4 Signature Request', function () {
ganacheOptions,
title: this.test.title,
},
async ({ driver }) => {
async ({ driver, ganacheServer }) => {
const addresses = await ganacheServer.getAccounts();
const publicAddress = addresses[0];
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
Expand Down Expand Up @@ -97,7 +98,6 @@ describe('Sign Typed Data V3 Signature Request', function () {
},
],
};
const publicAddress = '0x5cfe73b6021e818b776b421b1c4db2474086a7e1';
await withFixtures(
{
dapp: true,
Expand All @@ -107,7 +107,9 @@ describe('Sign Typed Data V3 Signature Request', function () {
ganacheOptions,
title: this.test.title,
},
async ({ driver }) => {
async ({ driver, ganacheServer }) => {
const addresses = await ganacheServer.getAccounts();
const publicAddress = addresses[0];
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
Expand Down Expand Up @@ -178,7 +180,6 @@ describe('Sign Typed Data Signature Request', function () {
},
],
};
const publicAddress = '0x5cfe73b6021e818b776b421b1c4db2474086a7e1';
await withFixtures(
{
dapp: true,
Expand All @@ -188,7 +189,9 @@ describe('Sign Typed Data Signature Request', function () {
ganacheOptions,
title: this.test.title,
},
async ({ driver }) => {
async ({ driver, ganacheServer }) => {
const addresses = await ganacheServer.getAccounts();
const publicAddress = addresses[0];
await driver.navigate();
await driver.fill('#password', 'correct horse battery staple');
await driver.press('#password', driver.Key.ENTER);
Expand Down