1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 03:12:42 +02:00

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

* Using ganache requests to get balances

* Replace getBalance getAccounts with ganache funcs

* Add secondary ganache server to testsuite
This commit is contained in:
seaona 2023-03-20 10:29:18 +01:00 committed by GitHub
parent 33a25cd6e9
commit c940f744a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 64 additions and 32 deletions

View File

@ -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);
return balanceFormatted;
}
async quit() {
if (!this._server) {
throw new Error('Server not running yet');

View File

@ -145,6 +145,8 @@ async function withFixtures(options, testSuite) {
driver: driverProxy ?? driver,
mockServer,
contractRegistry,
ganacheServer,
secondaryGanacheServer,
});
} catch (error) {
failed = true;

View File

@ -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,
);
@ -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());
},
);
});

View File

@ -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
@ -104,9 +104,10 @@ describe('MetaMask Responsive UI', function () {
await driver.press('#confirm-password', driver.Key.ENTER);
// balance renders
const balance = await ganacheServer.getBalance();
await driver.waitForSelector({
css: '[data-testid="eth-overview__primary-currency"]',
text: '1000 ETH',
text: `${balance} ETH`,
});
},
);

View File

@ -228,7 +228,7 @@ 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);
@ -236,10 +236,11 @@ describe('Navigate transactions', function () {
// 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());
},
);
});

View File

@ -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);
@ -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());
},
);
});

View File

@ -13,7 +13,6 @@ describe('Permissions', function () {
},
],
};
const publicAddress = '0x5cfe73b6021e818b776b421b1c4db2474086a7e1';
await withFixtures(
{
dapp: true,
@ -21,7 +20,9 @@ describe('Permissions', 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);

View File

@ -13,7 +13,6 @@ describe('Personal sign', function () {
},
],
};
const publicAddress = '0x5cfe73b6021e818b776b421b1c4db2474086a7e1';
await withFixtures(
{
dapp: true,
@ -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);

View File

@ -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);
@ -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);
},
);
});

View File

@ -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
@ -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

View File

@ -17,7 +17,6 @@ describe('Sign Typed Data V4 Signature Request', function () {
},
],
};
const publicAddress = '0x5cfe73b6021e818b776b421b1c4db2474086a7e1';
await withFixtures(
{
dapp: true,
@ -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);
@ -97,7 +98,6 @@ describe('Sign Typed Data V3 Signature Request', function () {
},
],
};
const publicAddress = '0x5cfe73b6021e818b776b421b1c4db2474086a7e1';
await withFixtures(
{
dapp: true,
@ -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);
@ -178,7 +180,6 @@ describe('Sign Typed Data Signature Request', function () {
},
],
};
const publicAddress = '0x5cfe73b6021e818b776b421b1c4db2474086a7e1';
await withFixtures(
{
dapp: true,
@ -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);