Skip to content

Commit

Permalink
test: refactor alert and snackbar utils
Browse files Browse the repository at this point in the history
  • Loading branch information
JuroUhlar committed Apr 3, 2024
1 parent fd32784 commit 91ab2ee
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 38 deletions.
2 changes: 1 addition & 1 deletion e2e/bot-firewall.spec.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
2 changes: 1 addition & 1 deletion e2e/coupon-fraud.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Page, test, expect } from '@playwright/test';

Check failure on line 1 in e2e/coupon-fraud.spec.ts

View workflow job for this annotation

GitHub Actions / Playwright e2e tests (2, 3)

[firefox] › coupon-fraud.spec.ts:20:7 › Coupon fraud › should not allow to claim coupon that does not exist

1) [firefox] › coupon-fraud.spec.ts:20:7 › Coupon fraud › should not allow to claim coupon that does not exist Error: browser.newContext: Unknown permission: clipboard-read

Check failure on line 1 in e2e/coupon-fraud.spec.ts

View workflow job for this annotation

GitHub Actions / Playwright e2e tests (2, 3)

[firefox] › coupon-fraud.spec.ts:20:7 › Coupon fraud › should not allow to claim coupon that does not exist

1) [firefox] › coupon-fraud.spec.ts:20:7 › Coupon fraud › should not allow to claim coupon that does not exist Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: browser.newContext: Unknown permission: clipboard-read

Check failure on line 1 in e2e/coupon-fraud.spec.ts

View workflow job for this annotation

GitHub Actions / Playwright e2e tests (2, 3)

[firefox] › coupon-fraud.spec.ts:20:7 › Coupon fraud › should not allow to claim coupon that does not exist

1) [firefox] › coupon-fraud.spec.ts:20:7 › Coupon fraud › should not allow to claim coupon that does not exist Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: browser.newContext: Unknown permission: clipboard-read

Check failure on line 1 in e2e/coupon-fraud.spec.ts

View workflow job for this annotation

GitHub Actions / Playwright e2e tests (2, 3)

[firefox] › coupon-fraud.spec.ts:27:7 › Coupon fraud › should apply correct coupon only once

2) [firefox] › coupon-fraud.spec.ts:27:7 › Coupon fraud › should apply correct coupon only once ── Error: browser.newContext: Unknown permission: clipboard-read

Check failure on line 1 in e2e/coupon-fraud.spec.ts

View workflow job for this annotation

GitHub Actions / Playwright e2e tests (2, 3)

[firefox] › coupon-fraud.spec.ts:27:7 › Coupon fraud › should apply correct coupon only once

2) [firefox] › coupon-fraud.spec.ts:27:7 › Coupon fraud › should apply correct coupon only once ── Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: browser.newContext: Unknown permission: clipboard-read

Check failure on line 1 in e2e/coupon-fraud.spec.ts

View workflow job for this annotation

GitHub Actions / Playwright e2e tests (2, 3)

[firefox] › coupon-fraud.spec.ts:27:7 › Coupon fraud › should apply correct coupon only once

2) [firefox] › coupon-fraud.spec.ts:27:7 › Coupon fraud › should apply correct coupon only once ── Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: browser.newContext: Unknown permission: clipboard-read

Check failure on line 1 in e2e/coupon-fraud.spec.ts

View workflow job for this annotation

GitHub Actions / Playwright e2e tests (2, 3)

[firefox] › coupon-fraud.spec.ts:37:7 › Coupon fraud › should prevent spamming multiple coupons

3) [firefox] › coupon-fraud.spec.ts:37:7 › Coupon fraud › should prevent spamming multiple coupons Error: browser.newContext: Unknown permission: clipboard-read

Check failure on line 1 in e2e/coupon-fraud.spec.ts

View workflow job for this annotation

GitHub Actions / Playwright e2e tests (2, 3)

[firefox] › coupon-fraud.spec.ts:37:7 › Coupon fraud › should prevent spamming multiple coupons

3) [firefox] › coupon-fraud.spec.ts:37:7 › Coupon fraud › should prevent spamming multiple coupons Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: browser.newContext: Unknown permission: clipboard-read

Check failure on line 1 in e2e/coupon-fraud.spec.ts

View workflow job for this annotation

GitHub Actions / Playwright e2e tests (2, 3)

[firefox] › coupon-fraud.spec.ts:37:7 › Coupon fraud › should prevent spamming multiple coupons

3) [firefox] › coupon-fraud.spec.ts:37:7 › Coupon fraud › should prevent spamming multiple coupons Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: browser.newContext: Unknown permission: clipboard-read
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';

Expand Down
2 changes: 1 addition & 1 deletion e2e/credential-stuffing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Page, test } from '@playwright/test';

Check failure on line 1 in e2e/credential-stuffing.spec.ts

View workflow job for this annotation

GitHub Actions / Playwright e2e tests (2, 3)

[firefox] › credential-stuffing.spec.ts:17:7 › Credential stuffing › should prevent login even with correct credentials

4) [firefox] › credential-stuffing.spec.ts:17:7 › Credential stuffing › should prevent login even with correct credentials Error: browser.newContext: Unknown permission: clipboard-read
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';

Expand Down
29 changes: 29 additions & 0 deletions e2e/e2eTestUtils.ts
Original file line number Diff line number Diff line change
@@ -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);
}
2 changes: 1 addition & 1 deletion e2e/loan-risk.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
2 changes: 1 addition & 1 deletion e2e/payment-fraud.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
2 changes: 1 addition & 1 deletion e2e/paywall.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down
2 changes: 1 addition & 1 deletion e2e/personalization.spec.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
9 changes: 0 additions & 9 deletions e2e/resetHelper.ts

This file was deleted.

30 changes: 9 additions & 21 deletions e2e/sms-fraud/bot-unprotected.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()'));

Check failure on line 43 in e2e/sms-fraud/bot-unprotected.spec.ts

View workflow job for this annotation

GitHub Actions / Playwright e2e tests (3, 3)

[webkit] › sms-fraud/bot-unprotected.spec.ts:35:7 › Sending verification SMS messages › allows user to create an account with the correct code

1) [webkit] › sms-fraud/bot-unprotected.spec.ts:35:7 › Sending verification SMS messages › allows user to create an account with the correct code Error: page.evaluate: NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission. 41 | await page.getByTestId(TEST_IDS.smsFraud.copyCodeButton).click(); 42 | > 43 | await page.getByTestId(TEST_IDS.smsFraud.codeInput).fill(await page.evaluate('navigator.clipboard.readText()')); | ^ 44 | await page.getByTestId(TEST_IDS.smsFraud.sendCode).click(); 45 | 46 | await assertAlert({ page, severity: 'success', text: SMS_FRAUD_COPY.accountCreated, index: 1 }); at /home/runner/work/fingerprintjs-pro-use-cases/fingerprintjs-pro-use-cases/e2e/sms-fraud/bot-unprotected.spec.ts:43:73

Check failure on line 43 in e2e/sms-fraud/bot-unprotected.spec.ts

View workflow job for this annotation

GitHub Actions / Playwright e2e tests (3, 3)

[webkit] › sms-fraud/bot-unprotected.spec.ts:35:7 › Sending verification SMS messages › allows user to create an account with the correct code

1) [webkit] › sms-fraud/bot-unprotected.spec.ts:35:7 › Sending verification SMS messages › allows user to create an account with the correct code Retry #1 ─────────────────────────────────────────────────────────────────────────────────────── Error: page.evaluate: NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission. 41 | await page.getByTestId(TEST_IDS.smsFraud.copyCodeButton).click(); 42 | > 43 | await page.getByTestId(TEST_IDS.smsFraud.codeInput).fill(await page.evaluate('navigator.clipboard.readText()')); | ^ 44 | await page.getByTestId(TEST_IDS.smsFraud.sendCode).click(); 45 | 46 | await assertAlert({ page, severity: 'success', text: SMS_FRAUD_COPY.accountCreated, index: 1 }); at /home/runner/work/fingerprintjs-pro-use-cases/fingerprintjs-pro-use-cases/e2e/sms-fraud/bot-unprotected.spec.ts:43:73

Check failure on line 43 in e2e/sms-fraud/bot-unprotected.spec.ts

View workflow job for this annotation

GitHub Actions / Playwright e2e tests (3, 3)

[webkit] › sms-fraud/bot-unprotected.spec.ts:35:7 › Sending verification SMS messages › allows user to create an account with the correct code

1) [webkit] › sms-fraud/bot-unprotected.spec.ts:35:7 › Sending verification SMS messages › allows user to create an account with the correct code Retry #2 ─────────────────────────────────────────────────────────────────────────────────────── Error: page.evaluate: NotAllowedError: The request is not allowed by the user agent or the platform in the current context, possibly because the user denied permission. 41 | await page.getByTestId(TEST_IDS.smsFraud.copyCodeButton).click(); 42 | > 43 | await page.getByTestId(TEST_IDS.smsFraud.codeInput).fill(await page.evaluate('navigator.clipboard.readText()')); | ^ 44 | await page.getByTestId(TEST_IDS.smsFraud.sendCode).click(); 45 | 46 | await assertAlert({ page, severity: 'success', text: SMS_FRAUD_COPY.accountCreated, index: 1 }); at /home/runner/work/fingerprintjs-pro-use-cases/fingerprintjs-pro-use-cases/e2e/sms-fraud/bot-unprotected.spec.ts:43:73
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 });
});
});
2 changes: 1 addition & 1 deletion src/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 91ab2ee

Please sign in to comment.