Skip to content

Commit 3916fb1

Browse files
committed
what if the e2e test strings are random
1 parent 3662e2a commit 3916fb1

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

frontend/tests/e2e/search/search-loading.spec.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { expect, Page, test } from "@playwright/test";
22
import { BrowserContextOptions } from "playwright-core";
3-
4-
import { fillSearchInputAndSubmit } from "./searchSpecUtil";
3+
import {
4+
fillSearchInputAndSubmit,
5+
generateRandomString,
6+
} from "tests/e2e/search/searchSpecUtil";
57

68
interface PageProps {
79
page: Page;
@@ -16,14 +18,14 @@ test.describe("Search page tests", () => {
1618
});
1719

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

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

26-
const searchTerm2 = "agency";
28+
const searchTerm2 = generateRandomString([8]);
2729
await fillSearchInputAndSubmit(searchTerm2, page);
2830
await expect(loadingIndicator).toBeVisible();
2931
await expect(loadingIndicator).toBeHidden();

frontend/tests/e2e/search/search-no-results.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { BrowserContextOptions } from "playwright-core";
44
import {
55
expectURLContainsQueryParam,
66
fillSearchInputAndSubmit,
7+
generateRandomString,
78
} from "./searchSpecUtil";
89

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

2627
await fillSearchInputAndSubmit(searchTerm, page);
2728
await new Promise((resolve) => setTimeout(resolve, 3250));

frontend/tests/e2e/search/searchSpecUtil.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,37 @@ export async function fillSearchInputAndSubmit(term: string, page: Page) {
1414
await page.click(".usa-search > button[type='submit']");
1515
}
1616

17+
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
18+
19+
// adapted from https://stackoverflow.com/a/1349426
20+
export const generateRandomString = (desiredPattern: number[]) => {
21+
const numberOfPossibleCharacters = characters.length;
22+
return desiredPattern.reduce((randomString, numberOfCharacters, index) => {
23+
let counter = 0;
24+
while (counter < numberOfCharacters) {
25+
randomString += characters.charAt(
26+
Math.floor(Math.random() * numberOfPossibleCharacters),
27+
);
28+
counter += 1;
29+
}
30+
if (index < desiredPattern.length - 1) {
31+
randomString += " ";
32+
}
33+
return randomString;
34+
}, "");
35+
};
36+
37+
// let result = "";
38+
// const charactersLength = characters.length;
39+
// let counter = 0;
40+
// while (counter < length) {
41+
// result += characters.charAt(Math.floor(Math.random() * charactersLength));
42+
// counter += 1;
43+
// }
44+
// return result;
45+
// };
46+
// (Math.random() + 1).toString(36).substring(7);
47+
1748
export function expectURLContainsQueryParam(
1849
page: Page,
1950
queryParamName: string,

0 commit comments

Comments
 (0)