Skip to content

Commit

Permalink
Check test
Browse files Browse the repository at this point in the history
  • Loading branch information
mrruby committed Jun 28, 2024
1 parent e5d3500 commit d6110cc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 84 deletions.
14 changes: 12 additions & 2 deletions tests/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { access, constants } from 'fs';
import { Browser, ElementHandle, launch, Page } from 'puppeteer';
import { Browser, launch, Locator, Page } from 'puppeteer';

export const launchBrowserWithExtension = async (extensionPath: string): Promise<Browser> => {
return launch({
Expand All @@ -24,7 +24,7 @@ const waitForNewPage = (browser: Browser): Promise<Page> =>

export const clickButtonAndWaitForNewPage = async (
browser: Browser,
button: ElementHandle<Element>,
button: Locator<Element>,
downloadPath: string
): Promise<Page> => {
await button?.click();
Expand Down Expand Up @@ -54,3 +54,13 @@ export const fileExists = (filePath: string): Promise<boolean> => {
checkFile();
});
};

const findElementByText = (context: Page, selector: string) => async (text: string) => {
const locator = context.locator(`${selector}::-p-text(${text})`);
await locator.wait();
return locator;
};

export const findButtonByText = (context: Page) =>
findElementByText(context, 'button:not(:disabled) > span');
export const findTextBySelector = (context: Page) => findElementByText(context, '');
114 changes: 32 additions & 82 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import {
clickButtonAndWaitForNewPage,
fileExists,
findButtonByText,
findTextBySelector,
launchBrowserWithExtension,
openExtensionPage
} from './helpers';
Expand Down Expand Up @@ -37,120 +39,68 @@ afterAll(async () => {

describe('Extension E2E Tests', () => {
it('Setup flow is working properly', async () => {
const setupButton = await page.waitForSelector('button::-p-text("Setup")');
const setupButton = await findButtonByText(page)('Setup');

expect(await page.content()).toContain('Setup Required');
const setupPageContent = await findTextBySelector(page)('Setup Required');

if (!setupButton) {
throw new Error('Button not found');
}
expect(setupPageContent).toBeTruthy();

const setupPage = await clickButtonAndWaitForNewPage(browser, setupButton, downloadPath);

const setupPageContent = await setupPage.content();
const findButtonOnSetupPage = findButtonByText(setupPage);
const findTextOnSetupPage = findTextBySelector(setupPage);

expect(setupPageContent).toContain(
const setupPageText = await findTextOnSetupPage(
'Holo Key Manager is a safe place to set up and manage keys for Holochain apps'
);

const firstTimeSetupButton = await setupPage.waitForSelector(
'button::-p-text("First time set up")'
);
expect(setupPageText).toBeTruthy();

if (!firstTimeSetupButton) {
throw new Error('Button not found');
}
const firstTimeSetupButton = await findButtonOnSetupPage('First time set up');

await Promise.all([setupPage.waitForNavigation(), firstTimeSetupButton.click()]);
const setKeyManagerPasswordText = await findTextOnSetupPage('Set Key Manager Password');

const firstTimeSetupPageContent = await setupPage.content();

expect(firstTimeSetupPageContent).toContain('Set Key Manager Password');

const newPasswordInput = await setupPage.waitForSelector('input[id*="enter-password"]');
const confirmPasswordInput = await setupPage.waitForSelector('input[id="confirm-password"]');

if (!newPasswordInput || !confirmPasswordInput) {
throw new Error('Password inputs not found');
}

await newPasswordInput.type('password');
await confirmPasswordInput.type('password');
expect(setKeyManagerPasswordText).toBeTruthy();

const setPasswordButton = await setupPage.waitForSelector('button::-p-text("Set password")');
await setupPage.type('input[id*="enter-password"]', 'password');
await setupPage.type('input[id="confirm-password"]', 'password');

if (!setPasswordButton) {
throw new Error('Button not found');
}
const setPasswordButton = await findButtonOnSetupPage('Set password');

await Promise.all([setupPage.waitForNavigation(), setPasswordButton.click()]);

const enterPassphrasePageContent = await setupPage.content();
const enterPassphraseText = await findTextOnSetupPage('Enter Passphrase');
expect(enterPassphraseText).toBeTruthy();

expect(enterPassphrasePageContent).toContain('Enter Passphrase');
await setupPage.type('textarea[placeholder="Enter Passphrase"]', 'passphrase passphrase');

const enterPassphraseInput = await setupPage.waitForSelector(
'textarea[placeholder="Enter Passphrase"]'
);
if (!enterPassphraseInput) {
throw new Error('Password inputs not found');
}

await enterPassphraseInput.type('passphrase passphrase');

const setPassphraseButton = await setupPage.waitForSelector(
'button::-p-text("Set passphrase")'
);

if (!setPassphraseButton) {
throw new Error('Button not found');
}
const setPassphraseButton = await findButtonOnSetupPage('Set passphrase');

await setPassphraseButton.click();

const confirmPassphrasePageContent = await setupPage.content();

expect(confirmPassphrasePageContent).toContain('Confirm Passphrase');

const confirmPassphraseInput = await setupPage.waitForSelector(
'textarea[placeholder="Confirm Passphrase"]'
);

if (!confirmPassphraseInput) {
throw new Error('Password inputs not found');
}

await confirmPassphraseInput.type('passphrase passphrase');
const confirmPassphraseText = await findTextOnSetupPage('Confirm Passphrase');
expect(confirmPassphraseText).toBeTruthy();

const confirmPassphraseButton = await setupPage.waitForSelector('button::-p-text("Next")');
await setupPage.type('textarea[placeholder="Confirm Passphrase"]', 'passphrase passphrase');

if (!confirmPassphraseButton) {
throw new Error('Button not found');
}
const confirmPassphraseButton = await findButtonOnSetupPage('Next');

await Promise.all([setupPage.waitForNavigation(), confirmPassphraseButton.click()]);

const generateSeedPageContent = await setupPage.content();

expect(generateSeedPageContent).toContain('Generate seed and key files');

const generateButton = await setupPage.waitForSelector('button::-p-text("Generate")');
const generateSeedText = await findTextOnSetupPage('Generate seed and key files');

if (!generateButton) {
throw new Error('Button not found');
}
expect(generateSeedText).toBeTruthy();

await Promise.all([setupPage.waitForNavigation(), generateButton.click()]);
const generateSeedButton = await findButtonOnSetupPage('Generate');

const saveSeedPageContent = await setupPage.content();
await Promise.all([setupPage.waitForNavigation(), generateSeedButton.click()]);

expect(saveSeedPageContent).toContain('Save seed and key files');
const saveSeedText = await findTextOnSetupPage('Save seed and key files');

const exportButton = await setupPage.waitForSelector('button::-p-text("Export")');
expect(saveSeedText).toBeTruthy();

if (!exportButton) {
throw new Error('Button not found');
}
const exportButton = await findButtonOnSetupPage('Export');

exportButton.click();

Expand All @@ -170,8 +120,8 @@ describe('Extension E2E Tests', () => {
expect(actualFiles).toContain(file);
});

const setupCompletePageContent = await setupPage.content();
const setupCompleteText = await findTextOnSetupPage('Setup Complete');

expect(setupCompletePageContent).toContain('Setup Complete');
expect(setupCompleteText).toBeTruthy();
}, 10000);
});

0 comments on commit d6110cc

Please sign in to comment.