From d6110ccc696f3959382bc05fb2397946618b5c07 Mon Sep 17 00:00:00 2001 From: Dawid Urbas Date: Fri, 28 Jun 2024 13:46:55 +0200 Subject: [PATCH] Check test --- tests/helpers.ts | 14 +++++- tests/index.test.ts | 114 +++++++++++++------------------------------- 2 files changed, 44 insertions(+), 84 deletions(-) diff --git a/tests/helpers.ts b/tests/helpers.ts index 6f82163..c220099 100644 --- a/tests/helpers.ts +++ b/tests/helpers.ts @@ -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 => { return launch({ @@ -24,7 +24,7 @@ const waitForNewPage = (browser: Browser): Promise => export const clickButtonAndWaitForNewPage = async ( browser: Browser, - button: ElementHandle, + button: Locator, downloadPath: string ): Promise => { await button?.click(); @@ -54,3 +54,13 @@ export const fileExists = (filePath: string): Promise => { 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, ''); diff --git a/tests/index.test.ts b/tests/index.test.ts index 4e05e29..ef5fa92 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -8,6 +8,8 @@ import { afterAll, beforeAll, describe, expect, it } from 'vitest'; import { clickButtonAndWaitForNewPage, fileExists, + findButtonByText, + findTextBySelector, launchBrowserWithExtension, openExtensionPage } from './helpers'; @@ -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(); @@ -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); });