Skip to content

Commit

Permalink
what if the e2e test strings are random
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-s-nava committed Oct 18, 2024
1 parent 3662e2a commit 3916fb1
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
10 changes: 6 additions & 4 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,14 +18,14 @@ test.describe("Search page tests", () => {
});

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

const loadingIndicator = page.getByTestId("loading-message");
await expect(loadingIndicator).toBeVisible();
await expect(loadingIndicator).toBeHidden();

const searchTerm2 = "agency";
const searchTerm2 = generateRandomString([8]);
await fillSearchInputAndSubmit(searchTerm2, page);
await expect(loadingIndicator).toBeVisible();
await expect(loadingIndicator).toBeHidden();
Expand Down
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 3916fb1

Please sign in to comment.