Skip to content

Commit

Permalink
what if the e2e test strings are random and what if we use role inste…
Browse files Browse the repository at this point in the history
…ad of test id
  • Loading branch information
doug-s-nava committed Oct 18, 2024
1 parent 3662e2a commit 2695b69
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 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
12 changes: 7 additions & 5 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");
const loadingIndicator = page.getByRole("progressbar");
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 2695b69

Please sign in to comment.