From 91ab2ee856d5cf3ea328db7627ec0ac67deeb351 Mon Sep 17 00:00:00 2001 From: Juraj Uhlar Date: Wed, 3 Apr 2024 23:14:00 +0200 Subject: [PATCH] test: refactor alert and snackbar utils --- e2e/bot-firewall.spec.ts | 2 +- e2e/coupon-fraud.spec.ts | 2 +- e2e/credential-stuffing.spec.ts | 2 +- e2e/e2eTestUtils.ts | 29 ++++++++++++++++++++++++++ e2e/loan-risk.spec.ts | 2 +- e2e/payment-fraud.spec.ts | 2 +- e2e/paywall.spec.ts | 2 +- e2e/personalization.spec.ts | 2 +- e2e/resetHelper.ts | 9 -------- e2e/sms-fraud/bot-unprotected.spec.ts | 30 ++++++++------------------- src/server/server.ts | 2 +- 11 files changed, 46 insertions(+), 38 deletions(-) create mode 100644 e2e/e2eTestUtils.ts delete mode 100644 e2e/resetHelper.ts diff --git a/e2e/bot-firewall.spec.ts b/e2e/bot-firewall.spec.ts index 6798a1d1..4471d476 100644 --- a/e2e/bot-firewall.spec.ts +++ b/e2e/bot-firewall.spec.ts @@ -1,5 +1,5 @@ import { Locator, Page, expect, test } from '@playwright/test'; -import { resetScenarios } from './resetHelper'; +import { resetScenarios } from './e2eTestUtils'; import { TEST_IDS } from '../src/client/testIDs'; import { BOT_FIREWALL_COPY } from '../src/client/bot-firewall/botFirewallCopy'; import { PRODUCTION_E2E_TEST_BASE_URL } from '../playwright.config'; diff --git a/e2e/coupon-fraud.spec.ts b/e2e/coupon-fraud.spec.ts index 923ba4a3..ef4879e9 100644 --- a/e2e/coupon-fraud.spec.ts +++ b/e2e/coupon-fraud.spec.ts @@ -1,5 +1,5 @@ import { Page, test, expect } from '@playwright/test'; -import { resetScenarios } from './resetHelper'; +import { resetScenarios } from './e2eTestUtils'; import { TEST_IDS } from '../src/client/testIDs'; import { COUPON_FRAUD_COPY } from '../src/pages/api/coupon-fraud/claim'; diff --git a/e2e/credential-stuffing.spec.ts b/e2e/credential-stuffing.spec.ts index 32c704bd..711f5fe5 100644 --- a/e2e/credential-stuffing.spec.ts +++ b/e2e/credential-stuffing.spec.ts @@ -1,5 +1,5 @@ import { Page, test } from '@playwright/test'; -import { resetScenarios } from './resetHelper'; +import { resetScenarios } from './e2eTestUtils'; import { TEST_IDS } from '../src/client/testIDs'; import { CREDENTIAL_STUFFING_COPY } from '../src/pages/api/credential-stuffing/authenticate'; diff --git a/e2e/e2eTestUtils.ts b/e2e/e2eTestUtils.ts new file mode 100644 index 00000000..d7bb3465 --- /dev/null +++ b/e2e/e2eTestUtils.ts @@ -0,0 +1,29 @@ +import { Page, expect } from '@playwright/test'; +import { TEST_ATTRIBUTES, TEST_IDS } from '../src/client/testIDs'; +import { Severity } from '../src/server/server'; + +// Assumes you already are on a use case page with the Reset button present +export async function resetScenarios(page: Page) { + await page.getByTestId(TEST_IDS.reset.resetButton).click(); + await page.getByTestId(TEST_IDS.reset.resetSuccess).waitFor({ timeout: 10000 }); + await page.getByTestId(TEST_IDS.common.closeSnackbar).first().click(); +} + +type AssertAlertOrSnackbarArgs = { + page: Page; + severity: Severity; + text: string; + index?: number; +}; + +export async function assertAlert({ page, severity, text, index = 0 }: AssertAlertOrSnackbarArgs) { + const alert = await page.getByTestId(TEST_IDS.common.alert).nth(index); + await expect(alert).toHaveAttribute(TEST_ATTRIBUTES.severity, severity); + await expect(alert).toContainText(text); +} + +export async function assertSnackbar({ page, severity, text, index = 0 }: AssertAlertOrSnackbarArgs) { + const snackbar = await page.getByTestId(TEST_IDS.common.snackBar).nth(index); + await expect(snackbar).toHaveAttribute(TEST_ATTRIBUTES.severity, severity); + await expect(snackbar).toContainText(text); +} diff --git a/e2e/loan-risk.spec.ts b/e2e/loan-risk.spec.ts index a0b65a71..40f6a6d0 100644 --- a/e2e/loan-risk.spec.ts +++ b/e2e/loan-risk.spec.ts @@ -1,5 +1,5 @@ import { Page, expect, test } from '@playwright/test'; -import { resetScenarios } from './resetHelper'; +import { resetScenarios } from './e2eTestUtils'; import { TEST_IDS } from '../src/client/testIDs'; import { LOAN_RISK_COPY } from '../src/pages/api/loan-risk/request-loan'; diff --git a/e2e/payment-fraud.spec.ts b/e2e/payment-fraud.spec.ts index 38878e44..34f65c68 100644 --- a/e2e/payment-fraud.spec.ts +++ b/e2e/payment-fraud.spec.ts @@ -1,5 +1,5 @@ import { Page, test } from '@playwright/test'; -import { resetScenarios } from './resetHelper'; +import { resetScenarios } from './e2eTestUtils'; import { PAYMENT_FRAUD_COPY } from '../src/pages/api/payment-fraud/place-order'; import { TEST_IDS } from '../src/client/testIDs'; diff --git a/e2e/paywall.spec.ts b/e2e/paywall.spec.ts index 4f2b230c..8d14aefc 100644 --- a/e2e/paywall.spec.ts +++ b/e2e/paywall.spec.ts @@ -1,5 +1,5 @@ import { test, expect } from '@playwright/test'; -import { resetScenarios } from './resetHelper'; +import { resetScenarios } from './e2eTestUtils'; import { TEST_IDS } from '../src/client/testIDs'; import { PAYWALL_COPY } from '../src/server/paywall/paywallCopy'; diff --git a/e2e/personalization.spec.ts b/e2e/personalization.spec.ts index ae2d8228..4c9698cc 100644 --- a/e2e/personalization.spec.ts +++ b/e2e/personalization.spec.ts @@ -1,5 +1,5 @@ import { expect, test } from '@playwright/test'; -import { resetScenarios } from './resetHelper'; +import { resetScenarios } from './e2eTestUtils'; import { TEST_IDS } from '../src/client/testIDs'; const CART_ID = TEST_IDS.common.cart; diff --git a/e2e/resetHelper.ts b/e2e/resetHelper.ts deleted file mode 100644 index 99cc7f35..00000000 --- a/e2e/resetHelper.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { Page } from '@playwright/test'; -import { TEST_IDS } from '../src/client/testIDs'; - -// Assumes you already are on a use case page with the Reset button present -export async function resetScenarios(page: Page) { - await page.getByTestId(TEST_IDS.reset.resetButton).click(); - await page.getByTestId(TEST_IDS.reset.resetSuccess).waitFor({ timeout: 10000 }); - await page.getByTestId(TEST_IDS.common.closeSnackbar).first().click(); -} diff --git a/e2e/sms-fraud/bot-unprotected.spec.ts b/e2e/sms-fraud/bot-unprotected.spec.ts index 93ed0ec1..b9a5122b 100644 --- a/e2e/sms-fraud/bot-unprotected.spec.ts +++ b/e2e/sms-fraud/bot-unprotected.spec.ts @@ -2,7 +2,7 @@ import { expect, test } from '@playwright/test'; import { TEST_ATTRIBUTES, TEST_IDS } from '../../src/client/testIDs'; import { SMS_FRAUD_COPY } from '../../src/server/sms-fraud/smsFraudCopy'; import { TEST_PHONE_NUMBER } from '../../src/pages/api/sms-fraud/send-verification-sms'; -import { resetScenarios } from '../resetHelper'; +import { assertAlert, assertSnackbar, resetScenarios } from '../e2eTestUtils'; const TEST_ID = TEST_IDS.smsFraud; @@ -19,42 +19,30 @@ test.describe('Sending verification SMS messages', () => { const sendButton = await page.getByTestId(TEST_ID.sendMessage); await sendButton.click(); - const alert = await page.getByTestId(TEST_IDS.common.alert); - await expect(alert).toHaveAttribute(TEST_ATTRIBUTES.severity, 'success'); - await expect(alert).toContainText(SMS_FRAUD_COPY.messageSent(TEST_PHONE_NUMBER, 2)); + await assertAlert({ page, severity: 'success', text: SMS_FRAUD_COPY.messageSent(TEST_PHONE_NUMBER, 2) }); await sendButton.click(); - await expect(alert).toHaveAttribute(TEST_ATTRIBUTES.severity, 'error'); - await expect(alert).toContainText(SMS_FRAUD_COPY.needToWait(1), {}); + await assertAlert({ page, severity: 'error', text: SMS_FRAUD_COPY.needToWait(1) }); await page.waitForTimeout(30000); await sendButton.click(); - await expect(alert).toHaveAttribute(TEST_ATTRIBUTES.severity, 'success'); - await expect(alert).toContainText(SMS_FRAUD_COPY.messageSent(TEST_PHONE_NUMBER, 1), {}); + await assertAlert({ page, severity: 'success', text: SMS_FRAUD_COPY.messageSent(TEST_PHONE_NUMBER, 1) }); await sendButton.click(); - await expect(alert).toHaveAttribute(TEST_ATTRIBUTES.severity, 'error'); - await expect(alert).toContainText(SMS_FRAUD_COPY.needToWait(2), {}); + await assertAlert({ page, severity: 'error', text: SMS_FRAUD_COPY.needToWait(2) }); }); test('allows user to create an account with the correct code', async ({ page }) => { const sendButton = await page.getByTestId(TEST_ID.sendMessage); - await sendButton.click(); + await assertAlert({ page, severity: 'success', text: SMS_FRAUD_COPY.messageSent(TEST_PHONE_NUMBER, 2) }); - const alert = await page.getByTestId(TEST_IDS.common.alert); - await expect(alert).toHaveAttribute(TEST_ATTRIBUTES.severity, 'success'); - await expect(alert).toContainText(SMS_FRAUD_COPY.messageSent(TEST_PHONE_NUMBER, 2)); - - const snackBar = await page.getByTestId(TEST_IDS.common.snackBar); - await expect(snackBar).toHaveAttribute(TEST_ATTRIBUTES.severity, 'info'); - await expect(snackBar).toContainText('Your verification code is'); + await assertSnackbar({ page, severity: 'info', text: 'Your verification code is' }); await page.getByTestId(TEST_IDS.smsFraud.copyCodeButton).click(); await page.getByTestId(TEST_IDS.smsFraud.codeInput).fill(await page.evaluate('navigator.clipboard.readText()')); await page.getByTestId(TEST_IDS.smsFraud.sendCode).click(); - const alert2 = await page.getByTestId(TEST_IDS.common.alert).nth(1); - await expect(alert2).toHaveAttribute(TEST_ATTRIBUTES.severity, 'success'); - await expect(alert2).toContainText(SMS_FRAUD_COPY.accountCreated); + + await assertAlert({ page, severity: 'success', text: SMS_FRAUD_COPY.accountCreated, index: 1 }); }); }); diff --git a/src/server/server.ts b/src/server/server.ts index 527c6375..89837590 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -25,7 +25,7 @@ export const ourOrigins = [ 'https://staging.fingerprinthub.com', ]; -export type Severity = 'success' | 'warning' | 'error'; +export type Severity = 'success' | 'warning' | 'error' | 'info'; export const messageSeverity = Object.freeze({ Success: 'success',