Skip to content

Commit

Permalink
what if the e2e test strings are random and what if we try some crazy…
Browse files Browse the repository at this point in the history
… timing racy condition thing with promise.all
  • Loading branch information
doug-s-nava committed Oct 18, 2024
1 parent 3662e2a commit 2915e8b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
10 changes: 8 additions & 2 deletions frontend/tests/e2e/newsletter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable testing-library/prefer-screen-queries */
import { expect, test } from "@playwright/test";

import { generateRandomString } from "./search/searchSpecUtil";

test.beforeEach(async ({ page }) => {
await page.goto("/subscribe");
});
Expand Down Expand Up @@ -32,7 +34,9 @@ test("successful signup", async ({ page }) => {
);

// Fill out form
await page.getByLabel("First Name (required)").fill("Apple");
await page
.getByLabel("First Name (required)")
.fill(generateRandomString([10]));
await page.getByLabel("Email (required)").fill("[email protected]");

await page.getByRole("button", { name: /subscribe/i }).click();
Expand All @@ -54,7 +58,9 @@ test("error during signup", async ({ page }) => {
);

// Fill out form
await page.getByLabel("First Name (required)").fill("Apple");
await page
.getByLabel("First Name (required)")
.fill(generateRandomString([10]));
await page.getByLabel("Email (required)").fill("[email protected]");

await page.getByRole("button", { name: /subscribe/i }).click();
Expand Down
22 changes: 13 additions & 9 deletions frontend/tests/e2e/search/search-loading.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { expect, Page, test } from "@playwright/test";
import { BrowserContextOptions } from "playwright-core";

import { fillSearchInputAndSubmit } from "./searchSpecUtil";
import {
fillSearchInputAndSubmit,
generateRandomString,
} from "tests/e2e/search/searchSpecUtil";

interface PageProps {
page: Page;
Expand All @@ -16,16 +18,18 @@ test.describe("Search page tests", () => {
});

test("should show and hide loading state", async ({ page }: PageProps) => {
const searchTerm = "advanced";
await fillSearchInputAndSubmit(searchTerm, page);

// if this doesn't work we can try https://playwright.dev/docs/api/class-browsertype#browser-type-launch-option-slow-mo
const searchTerm = generateRandomString([4, 5]);
const loadingIndicator = page.getByTestId("loading-message");
await expect(loadingIndicator).toBeVisible();
await fillSearchInputAndSubmit(searchTerm, page);
await Promise.all([expect(loadingIndicator).toBeVisible()]);
await expect(loadingIndicator).toBeHidden();

const searchTerm2 = "agency";
await fillSearchInputAndSubmit(searchTerm2, page);
await expect(loadingIndicator).toBeVisible();
const searchTerm2 = generateRandomString([8]);
await Promise.all([
fillSearchInputAndSubmit(searchTerm2, page),
expect(loadingIndicator).toBeVisible(),
]);
await expect(loadingIndicator).toBeHidden();
});
});
3 changes: 2 additions & 1 deletion frontend/tests/e2e/search/search-no-results.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BrowserContextOptions } from "playwright-core";
import {
expectURLContainsQueryParam,
fillSearchInputAndSubmit,
generateRandomString,
} from "./searchSpecUtil";

interface PageProps {
Expand All @@ -21,7 +22,7 @@ test.describe("Search page tests", () => {
test("should return 0 results when searching for obscure term", async ({
page,
}: PageProps) => {
const searchTerm = "0resultearch";
const searchTerm = generateRandomString([10]);

await fillSearchInputAndSubmit(searchTerm, page);
await new Promise((resolve) => setTimeout(resolve, 3250));
Expand Down
31 changes: 31 additions & 0 deletions frontend/tests/e2e/search/searchSpecUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,37 @@ export async function fillSearchInputAndSubmit(term: string, page: Page) {
await page.click(".usa-search > button[type='submit']");
}

const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

// adapted from https://stackoverflow.com/a/1349426
export const generateRandomString = (desiredPattern: number[]) => {
const numberOfPossibleCharacters = characters.length;
return desiredPattern.reduce((randomString, numberOfCharacters, index) => {
let counter = 0;
while (counter < numberOfCharacters) {
randomString += characters.charAt(
Math.floor(Math.random() * numberOfPossibleCharacters),
);
counter += 1;
}
if (index < desiredPattern.length - 1) {
randomString += " ";
}
return randomString;
}, "");
};

// let result = "";
// const charactersLength = characters.length;
// let counter = 0;
// while (counter < length) {
// result += characters.charAt(Math.floor(Math.random() * charactersLength));
// counter += 1;
// }
// return result;
// };
// (Math.random() + 1).toString(36).substring(7);

export function expectURLContainsQueryParam(
page: Page,
queryParamName: string,
Expand Down

0 comments on commit 2915e8b

Please sign in to comment.