Skip to content

Commit

Permalink
wallet switcher e2e (#485)
Browse files Browse the repository at this point in the history
Co-authored-by: brunobar79 <[email protected]>
Co-authored-by: gregs <[email protected]>
Co-authored-by: Esteban Miño <[email protected]>
Co-authored-by: Christopher Howard <[email protected]>
Co-authored-by: DanielSinclair <[email protected]>
  • Loading branch information
6 people authored May 10, 2023
1 parent 0fa7c1d commit e7c8fb1
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 46 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ jobs:
- name: Run e2e (Chrome)
uses: nick-fields/retry@v2
with:
timeout_minutes: 16
timeout_minutes: 18
max_attempts: 3
command: |
export BROWSER=chrome
Expand Down Expand Up @@ -173,7 +173,7 @@ jobs:
# - name: Run e2e (Brave)
# uses: nick-fields/retry@v2
# with:
# timeout_minutes: 16
# timeout_minutes: 18
# max_attempts: 3
# command: |
# export BROWSER=brave
Expand Down
35 changes: 35 additions & 0 deletions e2e/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,28 @@ export async function getExtensionIdByName(driver, extensionName) {
`);
}

export function shortenAddress(address) {
// if address is 42 in length and starts with 0x, then shorten it
// otherwise return the base value. this is so it doesn't break incase an ens, etc is input
return address.substring(0, 2) === '0x' && address.length === 42
? `${address.substring(0, 6)}...${address.substring(38, 42)}`
: address;
}

export async function switchWallet(address, rootURL, driver) {
// find shortened address, go to popup, find header, click, find wallet you want to switch to and click
const shortenedAddress = shortenAddress(address);

await goToPopup(driver, rootURL, '#/home');
await findElementByIdAndClick({
id: 'header-account-name-shuffle',
driver,
});

await findElementByTextAndClick(driver, shortenedAddress);
await delayTime('short');
}

export async function getOnchainBalance(addy, contract) {
const provider = ethers.getDefaultProvider('http://127.0.0.1:8545');
const testContract = new ethers.Contract(contract, erc20ABI, provider);
Expand Down Expand Up @@ -94,6 +116,13 @@ export async function findElementByText(driver, text) {
return driver.findElement(By.xpath("//*[contains(text(),'" + text + "')]"));
}

export async function findElementByTextAndClick(driver, text) {
const element = await driver.findElement(
By.xpath("//*[contains(text(),'" + text + "')]"),
);
await waitAndClick(element, driver);
}

export async function waitAndClick(element, driver) {
await delay(200);
await driver.wait(until.elementIsVisible(element), waitUntilTime);
Expand Down Expand Up @@ -130,6 +159,12 @@ export async function findElementByTestIdAndClick({ id, driver }) {
await waitAndClick(element, driver);
}

export async function findElementByIdAndClick({ id, driver }) {
await delay(200);
const element = await findElementById({ id, driver });
await waitAndClick(element, driver);
}

export async function typeOnTextInput({ id, text, driver }) {
const element = await findElementByTestId({ id, driver });
await element.sendKeys(text);
Expand Down
34 changes: 7 additions & 27 deletions e2e/parallel/importWalletFlow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ import {
findElementByTestIdAndClick,
findElementByText,
getExtensionIdByName,
getTextFromText,
goToPopup,
goToWelcome,
initDriverWithOptions,
querySelector,
shortenAddress,
typeOnTextInput,
waitAndClick,
} from '../helpers';

let rootURL = 'chrome-extension://';
let driver: WebDriver;

const browser = process.env.BROWSER || 'chrome';
const os = process.env.OS || 'mac';
const wallet = '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266';

describe('Import wallet flow', () => {
beforeAll(async () => {
Expand Down Expand Up @@ -71,31 +72,10 @@ describe('Import wallet flow', () => {
await findElementByTestIdAndClick({ id: 'set-password-button', driver });
await delayTime('long');
await findElementByText(driver, 'Your wallets ready');
});

it('should be able to test the sandbox for the popup', async () => {
await goToPopup(driver, rootURL, '#/home');
await findElementByTestIdAndClick({ id: 'home-page-header-right', driver });
await findElementByTestIdAndClick({ id: 'settings-link', driver });
const btn = await querySelector(
driver,
'[data-testid="test-sandbox-popup"]',
);
await waitAndClick(btn, driver);
const text = await driver.switchTo().alert().getText();
expect(text).toBe('Popup sandboxed!');
await driver.switchTo().alert().accept();
});

it('should be able to test the sandbox for the background', async () => {
const btn = await querySelector(
driver,
'[data-testid="test-sandbox-background"]',
);
await waitAndClick(btn, driver);
await delayTime('long');
const text = await driver.switchTo().alert().getText();
expect(text).toBe('Background sandboxed!');
await driver.switchTo().alert().accept();
goToPopup(driver, rootURL);
await delayTime('short');
const account = await getTextFromText({ id: 'account-name', driver });
expect(account).toBe(await shortenAddress(wallet));
});
});
78 changes: 78 additions & 0 deletions e2e/parallel/importWalletFlowPkey.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import 'chromedriver';
import 'geckodriver';
import { WebDriver } from 'selenium-webdriver';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';

import {
delayTime,
findElementByTestIdAndClick,
findElementByText,
getExtensionIdByName,
getTextFromText,
goToPopup,
goToWelcome,
initDriverWithOptions,
shortenAddress,
typeOnTextInput,
} from '../helpers';

let rootURL = 'chrome-extension://';
let driver: WebDriver;

const browser = process.env.BROWSER || 'chrome';
const os = process.env.OS || 'mac';
const wallet = '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266';

describe('Import wallet flow', () => {
beforeAll(async () => {
driver = await initDriverWithOptions({
browser,
os,
});
const extensionId = await getExtensionIdByName(driver, 'Rainbow');
if (!extensionId) throw new Error('Extension not found');
rootURL += extensionId;
});

afterAll(async () => driver.quit());

// Import a wallet
it('should be able import a wallet via pkey', async () => {
// Start from welcome screen
await goToWelcome(driver, rootURL);
await findElementByTestIdAndClick({
id: 'import-wallet-button',
driver,
});
await findElementByTestIdAndClick({
id: 'import-wallet-option',
driver,
});

await typeOnTextInput({
id: 'secret-textarea',
driver,
text: '0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80',
});

await findElementByTestIdAndClick({
id: 'import-wallets-button',
driver,
});

await typeOnTextInput({ id: 'password-input', driver, text: 'test1234' });
await typeOnTextInput({
id: 'confirm-password-input',
driver,
text: 'test1234',
});
await findElementByTestIdAndClick({ id: 'set-password-button', driver });
await delayTime('long');
await findElementByText(driver, 'Your wallets ready');

goToPopup(driver, rootURL);
await delayTime('short');
const account = await getTextFromText({ id: 'account-name', driver });
expect(account).toBe(await shortenAddress(wallet));
});
});
41 changes: 41 additions & 0 deletions e2e/parallel/newWalletFlow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
initDriverWithOptions,
querySelector,
typeOnTextInput,
waitAndClick,
} from '../helpers';

let rootURL = 'chrome-extension://';
Expand Down Expand Up @@ -96,4 +97,44 @@ describe('New wallet flow', () => {
const actual = await label.getText();
expect(actual.substr(0, 2) === '0x' && actual.length === 13).toEqual(true);
});

it('should be able to lock and unlock the extension', async () => {
await goToPopup(driver, rootURL, '#/home');
// Lock
await findElementByTestIdAndClick({
id: 'home-page-header-right',
driver,
});
await findElementByTestIdAndClick({ id: 'lock', driver });

// Unlock
await typeOnTextInput({ id: 'password-input', driver, text: 'test1234' });
await findElementByTestIdAndClick({ id: 'unlock-button', driver });
});

it('should be able to test the sandbox for the popup', async () => {
await goToPopup(driver, rootURL, '#/home');
await findElementByTestIdAndClick({ id: 'home-page-header-right', driver });
await findElementByTestIdAndClick({ id: 'settings-link', driver });
const btn = await querySelector(
driver,
'[data-testid="test-sandbox-popup"]',
);
await waitAndClick(btn, driver);
const text = await driver.switchTo().alert().getText();
expect(text).toBe('Popup sandboxed!');
await driver.switchTo().alert().accept();
});

it('should be able to test the sandbox for the background', async () => {
const btn = await querySelector(
driver,
'[data-testid="test-sandbox-background"]',
);
await waitAndClick(btn, driver);
await delayTime('long');
const text = await driver.switchTo().alert().getText();
expect(text).toBe('Background sandboxed!');
await driver.switchTo().alert().accept();
});
});
13 changes: 13 additions & 0 deletions e2e/parallel/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { mergeConfig } from 'vite';
import { defineConfig } from 'vitest/config';

import viteConfig from '../vitest.config';

export default mergeConfig(
viteConfig,
defineConfig({
test: {
threads: false,
},
}),
);
Loading

0 comments on commit e7c8fb1

Please sign in to comment.