Skip to content

Commit 271c90f

Browse files
committed
fix: refactored and debugged filter tests (#4068)
1 parent a244a29 commit 271c90f

File tree

2 files changed

+47
-57
lines changed

2 files changed

+47
-57
lines changed

explorer/e2e/anvil/anvil-filters.spec.ts

+33-28
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { expect, test } from "@playwright/test";
2-
import { testFilterPresence } from "../testFunctions";
2+
import {
3+
filter_regex,
4+
getFirstElementTextLocator,
5+
testFilterPresence,
6+
} from "../testFunctions";
37
import { anvilFilters, anvilTabs, anvilTabTestOrder } from "./anvil-tabs";
48

59
test.describe.configure({ mode: "parallel" });
610

7-
const filter_regex = (filter: string): RegExp =>
8-
new RegExp(filter + "\\s+\\([0-9]+\\)\\s*");
9-
1011
test("Check that all filters exist on the Datasets tab and are clickable", async ({
1112
page,
1213
}) => {
@@ -49,7 +50,11 @@ test("Check that the first filter on the Datasets tab creates at least one check
4950
// Select a filter
5051
await page
5152
.getByRole("button")
52-
.getByText(filter_regex(anvilFilters[4]))
53+
.getByText(
54+
filter_regex(
55+
anvilFilters[Math.floor(Math.random() * anvilFilters.length)]
56+
)
57+
)
5358
.click();
5459
// Expect all checkboxes to be unchecked initially and to work properly
5560
await expect(page.getByRole("checkbox").first()).toBeVisible();
@@ -62,42 +67,42 @@ test("Check that the first filter on the Datasets tab creates at least one check
6267
await expect(checkbox).toBeChecked();
6368
}
6469
await page.locator("body").click();
65-
const firstElementTextLocator = page
66-
.getByRole("rowgroup")
67-
.nth(1)
68-
.getByRole("row")
69-
.nth(0)
70-
.getByRole("cell")
71-
.first();
72-
await expect(firstElementTextLocator).toBeVisible();
70+
await expect(getFirstElementTextLocator(page, 0)).toBeVisible();
7371
});
7472

7573
test("Check that filter checkboxes are persistent across pages", async ({
7674
page,
7775
}) => {
78-
await page.goto(anvilTabs.datasets.url);
76+
// Randomly select a filter
77+
const test_filter =
78+
anvilFilters[Math.floor(Math.random() * anvilFilters.length)];
79+
// Start on the first tab in the test order (should be files)
80+
await page.goto(anvilTabs[anvilTabTestOrder[0]].url);
7981
await expect(
8082
page.getByRole("tab").getByText(anvilTabs.datasets.tabName)
8183
).toBeVisible();
82-
await page.getByText(filter_regex(anvilFilters[3])).click(); // maybe should select a random one instead;
84+
// Select the first checkbox on the test filter
85+
await page.getByText(filter_regex(test_filter)).click();
8386
await expect(page.getByRole("checkbox").first()).not.toBeChecked();
8487
await page.getByRole("checkbox").first().click();
8588
await expect(page.getByRole("checkbox").first()).toBeChecked();
8689
await page.locator("body").click();
87-
for (const blah of anvilTabTestOrder) {
88-
console.log(blah);
89-
await page.getByRole("tab").getByText(anvilTabs[blah].tabName).click();
90-
const firstElementTextLocator = page
91-
.getByRole("rowgroup")
92-
.nth(1)
93-
.getByRole("row")
94-
.nth(0)
95-
.getByRole("cell")
96-
.first();
97-
await expect(firstElementTextLocator).toBeVisible();
98-
await expect(page.getByText(filter_regex(anvilFilters[3]))).toBeVisible();
99-
await page.getByText(filter_regex(anvilFilters[3])).click();
90+
// Expect at least some text to still be visible
91+
await expect(getFirstElementTextLocator(page, 0)).toBeVisible();
92+
// For each tab, check that the selected filter is still checked
93+
for (const tab of anvilTabTestOrder.slice(1)) {
94+
await page.getByRole("tab").getByText(anvilTabs[tab].tabName).click();
95+
await expect(page.getByText(filter_regex(test_filter))).toBeVisible();
96+
await page.getByText(filter_regex(test_filter)).click();
10097
await expect(page.getByRole("checkbox").first()).toBeChecked();
10198
await page.locator("body").click();
10299
}
100+
// Return to the start tab and confirm that the filter stays checked and that some content is visible
101+
await page
102+
.getByRole("tab")
103+
.getByText(anvilTabs[anvilTabTestOrder[0]].tabName)
104+
.click();
105+
await expect(getFirstElementTextLocator(page, 0)).toBeVisible();
106+
await page.getByText(filter_regex(test_filter)).click();
107+
await expect(page.getByRole("checkbox").first()).toBeChecked();
103108
});

explorer/e2e/testFunctions.ts

+14-29
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { expect, Locator, Page } from "@playwright/test";
2-
import { anvilFilters, anvilTabs, anvilTabTestOrder } from "./anvil/anvil-tabs";
2+
import { anvilFilters } from "./anvil/anvil-tabs";
33
import { TabDescription } from "./testInterfaces";
44

55
/* eslint-disable sonarjs/no-duplicate-string -- ignoring duplicate strings here */
66
// Run the "Expect each tab to appear as selected when the corresponding url is accessed" test
77

8-
const getFirstElementTextLocator = (
8+
export const getFirstElementTextLocator = (
99
page: Page,
1010
workColumnPosition: number
1111
): Locator => {
@@ -82,13 +82,10 @@ export async function testSortAzul(
8282
? columnPosition + 1
8383
: columnPosition;
8484
// Locators for the first and last cells in a particular column position on the page
85-
const firstElementTextLocator = page
86-
.getByRole("rowgroup")
87-
.nth(1)
88-
.getByRole("row")
89-
.nth(0)
90-
.getByRole("cell")
91-
.nth(workColumnPosition);
85+
const firstElementTextLocator = getFirstElementTextLocator(
86+
page,
87+
workColumnPosition
88+
);
9289
const lastElementTextLocator = page
9390
.getByRole("rowgroup")
9491
.nth(1)
@@ -141,13 +138,10 @@ export async function testSortCatalog(
141138
? columnPosition + 1
142139
: columnPosition;
143140
// Locators for the first and last cells in a particular column position on the page
144-
const firstElementTextLocator = page
145-
.getByRole("rowgroup")
146-
.nth(1)
147-
.getByRole("row")
148-
.nth(0)
149-
.getByRole("cell")
150-
.nth(workColumnPosition);
141+
const firstElementTextLocator = getFirstElementTextLocator(
142+
page,
143+
workColumnPosition
144+
);
151145
// Locator for the sort button
152146
const columnSortLocator = page
153147
.getByRole("columnheader", {
@@ -222,7 +216,7 @@ export async function testPreSelectedColumns(
222216
}
223217
}
224218

225-
const filter_regex = (filter: string): RegExp =>
219+
export const filter_regex = (filter: string): RegExp =>
226220
new RegExp(filter + "\\s+\\([0-9]+\\)\\s*");
227221

228222
export async function testFilterPresence(
@@ -231,18 +225,9 @@ export async function testFilterPresence(
231225
): Promise<void> {
232226
await page.goto(tab.url);
233227
await expect(page.getByRole("tab").getByText(tab.tabName)).toBeVisible();
234-
await page.getByText(filter_regex(anvilFilters[3])).click(); // maybe should select a random one instead;
235-
await expect(page.getByRole("checkbox").first()).not.toBeChecked();
236-
await page.getByRole("checkbox").first().click();
237-
await expect(page.getByRole("checkbox").first()).toBeChecked();
238-
await page.locator("body").click();
239-
for (const blah of anvilTabTestOrder) {
240-
console.log(blah);
241-
await page.getByRole("tab").getByText(anvilTabs[blah].tabName).click();
242-
await expect(getFirstElementTextLocator(page, 0)).toBeVisible();
243-
await expect(page.getByText(filter_regex(anvilFilters[3]))).toBeVisible();
244-
await page.getByText(filter_regex(anvilFilters[3])).click();
245-
await expect(page.getByRole("checkbox").first()).toBeChecked();
228+
for (const filter of anvilFilters) {
229+
await page.getByText(filter_regex(filter)).click(); // maybe should select a random one instead;
230+
await expect(page.getByRole("checkbox").first()).not.toBeChecked();
246231
await page.locator("body").click();
247232
}
248233
}

0 commit comments

Comments
 (0)