-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtestFunctions.ts
1468 lines (1415 loc) · 53.1 KB
/
testFunctions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { BrowserContext, expect, Locator, Page } from "@playwright/test";
import {
BackpageHeader,
ColumnDescription,
TabDescription,
} from "./testInterfaces";
/* eslint-disable sonarjs/no-duplicate-string -- ignoring duplicate strings here */
// Timeout constants
const TIMEOUT_EXPORT_REQUEST = 60000;
const TIMEOUT_DOWNLOAD = 10000;
/**
* Get an array of all visible column header names
* @param page - a Playwright page object
* @returns an array of the text of all visible column headers
*/
const getAllVisibleColumnNames = async (page: Page): Promise<string[]> => {
return (await page.getByRole("columnheader").allInnerTexts()).map((entry) =>
entry.trim()
);
};
/**
* Get a locator to the cell in the mth row's nth column
* @param page - a Playwright page object
* @param rowIndex - the zero-indexed row to return
* @param columnIndex - the zero-indexed column to return
* @returns a Playwright locator object to the selected cell
**/
export const getMthRowNthColumnCellLocator = (
page: Page,
rowIndex: number,
columnIndex: number
): Locator => {
return page
.getByRole("rowgroup")
.nth(1)
.getByRole("row")
.nth(rowIndex)
.getByRole("cell")
.nth(columnIndex);
};
/**
* Get a locator to the cell in the first row's nth column
* @param page - a Playwright page object
* @param columnIndex - the zero-indexed column to return
* @returns a Playwright locator object to the selected cell
**/
export const getFirstRowNthColumnCellLocator = (
page: Page,
columnIndex: number
): Locator => {
return getMthRowNthColumnCellLocator(page, 0, columnIndex);
};
/**
* Get a locator to the cell in the first row's nth column
* @param page - a Playwright page object
* @param columnIndex - the zero-indexed column to return
* @returns a Playwright locator object to the selected cell
**/
export const getLastRowNthColumnTextLocator = (
page: Page,
columnIndex: number
): Locator => {
return page
.getByRole("rowgroup")
.nth(1)
.getByRole("row")
.last()
.getByRole("cell")
.nth(columnIndex);
};
/**
* Tests that the tab url goes to a valid page and that the correct tab (and only
* the correct tab) appears selected
* @param page - a Playwright page object
* @param tab - the Tab object to check
* @param otherTabs - an array of the other Tab objects for this configuration
*/
export async function testUrl(
page: Page,
tab: TabDescription,
otherTabs: TabDescription[]
): Promise<void> {
// Go to the selected tab
await page.goto(tab.url);
// Check that the selected tab appears selected and the other tabs appear deselected
await expect(
page.getByRole("tab").getByText(tab.tabName, { exact: true })
).toHaveAttribute("aria-selected", "true");
for (const otherTab of otherTabs) {
if (otherTab.tabName !== tab.tabName) {
await expect(
page.getByRole("tab").getByText(otherTab.tabName)
).toHaveAttribute("aria-selected", "false");
}
}
}
/**
* Checks that all preselected columns listed in the tab object are visible in the correct order
* @param page - a Playwright page object
* @param startTab - the tab object to start testing on
* @param endTab - the tab to select during the test
*/
export async function testTab(
page: Page,
startTab: TabDescription,
endTab: TabDescription
): Promise<void> {
// Run the "Expect each tab to become selected, to go to the correct url, and to show all of its columns when selected" test
await page.goto(startTab.url);
await expect(getFirstRowNthColumnCellLocator(page, 1)).toBeVisible();
await page
.getByRole("tab")
.getByText(endTab.tabName, { exact: true })
.click();
await expect(page).toHaveURL(endTab.url);
await expect(page.getByRole("tab").getByText(endTab.tabName)).toHaveAttribute(
"aria-selected",
"true"
);
const columnArray = Array.from(Object.values(endTab.preselectedColumns));
for (const column of columnArray) {
await expect(
page.getByRole("columnheader").getByText(column.name, { exact: true })
).toBeVisible();
}
if (endTab.emptyFirstColumn) {
await expect(page.getByRole("columnheader")).toHaveCount(
columnArray.length + 1
);
} else {
await expect(page.getByRole("columnheader")).toHaveCount(
columnArray.length
);
}
}
// The test id for the column sorted icon
const COLUMN_SORT_ICON_TEST_ID = "SouthRoundedIcon";
/**
* Checks that sorting the tab causes the sort icon to appear
* This test does not check whether the sort order is correct.
* @param page - a Playwright page object
* @param tab - the tab to check
* @returns - true if the test passes and false if the test fails
*/
// eslint-disable-next-line sonarjs/cognitive-complexity -- Complex code just for diagnostic will be removed later
export async function testSortAzul(
page: Page,
tab: TabDescription
): Promise<boolean> {
// Get the current tab, and go to it's URL
await page.goto(tab.url);
// Get the name of all visible columns
const columnNameArray = (await getAllVisibleColumnNames(page)).slice(
tab.emptyFirstColumn ? 1 : 0
);
// Get the predefined column metadata (in an arbitrary order) as an array
const columnObjectArray = Array.from(Object.values(tab.preselectedColumns));
// Defined a value to store the locator for the previously checked locator
let lastElementSortIconLocator: Locator | undefined = undefined;
// Iterate through each visible column on screen
for (
let columnPosition = 0;
columnPosition < columnNameArray.length;
columnPosition++
) {
// Get the column position, taking into account that some tabs start with a non-text first column
const columnObject = columnObjectArray.find(
(x) => x.name === columnNameArray[columnPosition]
);
// If a column is visible but is not in the array of predefined columns, fail the test
if (columnObject === undefined) {
console.log(
`SORT AZUL: Preselected column object ${columnNameArray[columnPosition]} not found in tab configuration`
);
return false;
}
if (columnObject?.sortable) {
// Locator that can be clicked to sort the column
const columnSortLocator = page.getByRole("button", {
exact: true,
name: columnObject.name,
});
// Locator for the column sort icon
const sortIconLocator = columnSortLocator.getByTestId(
COLUMN_SORT_ICON_TEST_ID
);
// If on the first cell, expect the sort icon to be visible. Otherwise, expect it not to be
if (columnPosition === 0) {
await expect(sortIconLocator).not.toHaveCSS("opacity", "0");
} else {
await expect(sortIconLocator).toHaveCSS("opacity", "0");
}
// Click to sort
// dispatchEvent necessary because the table loading component sometimes interrupts a click event
await columnSortLocator.dispatchEvent("click");
// Expect the first element of the table to still be visible (may not have text)
await expect(
getFirstRowNthColumnCellLocator(page, columnPosition)
).toBeVisible();
// Expect the previously selected sort icon to be invisible
if (lastElementSortIconLocator !== undefined) {
await expect(lastElementSortIconLocator).toHaveCSS("opacity", "0");
}
// Expect the newly selected sort icon to be visible
await expect(sortIconLocator).not.toHaveCSS("opacity", "0");
// Save the selected sort icon locator
lastElementSortIconLocator = sortIconLocator;
}
}
return true;
}
const SEARCH_BUTTON_NAME = "Search";
/**
* Checks that sorting the tab does not cause the first row of the table to break.
* This test does not check whether the sort order is correct.
* This test assumes that this is a catalog explorer without pagination,
* so it only checks the first element of the table.
* @param page - a Playwright page object
* @param tab - the tab to check
* @returns - true if the test passes, false if the test fails
*/
export async function testSortCatalog(
page: Page,
tab: TabDescription
): Promise<boolean> {
// Get the current tab, and go to it's URL
await page.goto(tab.url);
await expect(getFirstRowNthColumnCellLocator(page, 0)).toBeVisible();
const columnNameArray = (
await page.getByRole("columnheader").allInnerTexts()
).map((entry) => entry.trim());
const columnObjectArray = Array.from(Object.values(tab.preselectedColumns));
for (
let columnPosition = 0;
columnPosition < columnNameArray.length;
columnPosition++
) {
const columnName = columnNameArray[columnPosition];
// Get the column position, taking into account that some tabs start with a non-text first column
const columnObject = columnObjectArray.find((x) => x.name === columnName);
if (columnObject === undefined) {
console.log(
`SORT CATALOG: Preselected column object ${columnName} not found in tab configuration`
);
return false;
}
if (columnObject.sortable) {
// Locators for the first cell in a particular column position on the page
const firstElementTextLocator = getFirstRowNthColumnCellLocator(
page,
columnPosition
);
// Locator for the sort button
const columnSortLocator = page
.getByRole("columnheader", {
exact: true,
name: columnName,
})
.getByRole("button");
await expect(firstElementTextLocator).toBeVisible();
// Click to sort
await columnSortLocator.click();
// Expect the first cell to still be visible
await expect(firstElementTextLocator).toBeVisible();
const nonOverlappingElement = page.getByRole("button", {
name: SEARCH_BUTTON_NAME,
});
const firstElementText = await hoverAndGetText(
page,
columnObject,
0,
columnPosition,
nonOverlappingElement
);
// Click again
await columnSortLocator.click();
// Expect the first cell to have changed after clicking sort
await expect(firstElementTextLocator).toBeVisible();
await expect(firstElementTextLocator).not.toHaveText(firstElementText);
}
}
return true;
}
/**
* Check that all of the selectable columns specified in the tab object
* are initially not selected in the "Edit Columns" menu, and that selecting
* them causes them to appear in the correct order
* @param page - a Playwright page object
* @param tab - the tab object to check
*/
export async function testSelectableColumns(
page: Page,
tab: TabDescription
): Promise<void> {
// Navigate to the tab
await page.goto(tab.url);
// Select the "Edit Columns" menu
// dispatchEvent necessary because the table loading component sometimes interrupts a click event
await page
.getByRole("button")
.getByText("Edit Columns")
.dispatchEvent("click");
await expect(page.getByRole("menu")).toBeVisible();
// Enable each selectable tab
const tabObjectArray = Array.from(Object.values(tab.selectableColumns));
for (const column of tabObjectArray) {
// Locate the checkbox for each column
const checkboxLocator = page
.getByRole("menu")
.locator("*")
.filter({
has: page
.locator("*")
.filter({ has: page.getByText(column.name, { exact: true }) }),
})
.getByRole("checkbox");
// Expect each column to be enabled and unchecked for selectable tabs
await expect(checkboxLocator).toBeEnabled();
await expect(checkboxLocator).not.toBeChecked();
// Expect clicking the checkbox to function
await checkboxLocator.click();
await expect(checkboxLocator).toBeChecked();
}
await page.getByRole("document").click();
await expect(page.getByRole("menu")).not.toBeVisible();
// Expect all selectable tabs to be enabled
await expect(page.getByRole("columnheader")).toContainText(
tabObjectArray.map((x) => x.name)
);
}
/**
* Checks that the preselected columns specified in the tab object appear
* in the "Edit Columns" menu and that their checkbox is checked and enabled.
* @param page - the Playwright page object
* @param tab - the tab object to test
*/
export async function testPreSelectedColumns(
page: Page,
tab: TabDescription
): Promise<void> {
await page.goto(tab.url);
await page.getByRole("button").getByText("Edit Columns").click();
await expect(page.getByRole("menu")).toBeVisible();
for (const column of Object.values(tab.preselectedColumns)) {
const checkboxLocator = page
.getByRole("menu")
.locator("*")
.filter({
has: page
.locator("*")
.filter({ has: page.getByText(column.name, { exact: true }) }),
})
.getByRole("checkbox");
await expect(checkboxLocator).toBeEnabled();
await expect(checkboxLocator).toBeChecked();
}
}
/**
* Returns a regex that matches the sidebar filter buttons
* This is useful for selecting a filter from the sidebar
* @param filterName - the name of the filter to match
* @returns a regular expression matching "[filterName] ([n])"
*/
export const filterRegex = (filterName: string): RegExp =>
new RegExp(escapeRegExp(filterName) + "\\s+\\([0-9]+\\)\\s*");
/**
* Checks that each filter specified in filterNames is visible and can be
* selected on the specified tab
* @param page - a Playwright page object
* @param tab - the tab to check
* @param filterNames - the names of the filters who whose existence should be tested for
*/
export async function testFilterPresence(
page: Page,
tab: TabDescription,
filterNames: string[]
): Promise<void> {
// Goto the selected tab
await page.goto(tab.url);
await expect(page.getByRole("tab").getByText(tab.tabName)).toBeVisible();
for (const filterName of filterNames) {
// Check that each filter is visible and clickable
await expect(page.getByText(filterRegex(filterName))).toBeVisible();
// dispatchEvent necessary because the filter menu component sometimes interrupts a click event
await page.getByText(filterRegex(filterName)).dispatchEvent("click");
await expect(page.getByRole("checkbox").first()).toBeVisible();
await expect(page.getByRole("checkbox").first()).not.toBeChecked();
// Check that clicking out of the filter menu causes it to disappear
await page.locator("body").click();
await expect(page.getByRole("checkbox")).toHaveCount(0);
}
}
/**
* Get a locator for the specified filter option. Requires a filter menu to be open
* @param page - a Playwright page object
* @param filterOptionName - the name of the filter option
* @returns a Playwright locator to the filter button
*/
export const getNamedFilterOptionLocator = (
page: Page,
filterOptionName: string
): Locator => {
// The Regex matches a filter name with a number after it, with potential whitespace before and after the number.
// This matches how the innerText in the filter options menu appears to Playwright.
return page.getByRole("button").filter({
has: page.getByRole("checkbox"),
hasText: RegExp(`^${escapeRegExp(filterOptionName)}\\s*\\d+\\s*`),
});
};
/**
* Get a locator for the nth filter option on the page.
* @param page - a Playwright page object
* @param n - the index of the filter option to get
* @returns - a Playwright locator object for the first filter option on the page
*/
const getNthFilterOptionLocator = (page: Page, n: number): Locator => {
return page
.getByRole("button")
.filter({ has: page.getByRole("checkbox") })
.nth(n);
};
/**
* Get a locator for the first filter option on the page.
* @param page - a Playwright page object
* @returns - a Playwright locator object for the first filter option on the page
*/
export const getFirstFilterOptionLocator = (page: Page): Locator => {
return getNthFilterOptionLocator(page, 0);
};
export const getFilterOptionName = async (
firstFilterOptionLocator: Locator
): Promise<string> => {
// Filter options display as "[text]\n[number]" , sometimes with extra whitespace, so we split on newlines and take the first non-empty string
return (
(await firstFilterOptionLocator.innerText())
.split("\n")
.map((x) => x.trim())
.find((x) => x.length > 0) ?? ""
);
};
const MAX_FILTER_OPTIONS_TO_CHECK = 10;
interface FilterOptionNameAndLocator {
index: number;
locator: Locator;
name: string;
}
/**
* Gets the name of the filter option associated with a locator
* @param page - a Playwright Page object, on which a filter must be currently selected
* @returns the innerText of the first nonempty filter option as a promise
*/
const getFirstNonEmptyFilterOptionInfo = async (
page: Page
): Promise<FilterOptionNameAndLocator> => {
let filterToSelect = "";
let filterOptionLocator = undefined;
let i = 0;
while (filterToSelect === "" && i < MAX_FILTER_OPTIONS_TO_CHECK) {
// Filter options display as "[text]\n[number]" , sometimes with extra whitespace, so we want the string before the newline
const filterOptionRegex = /^(.*)\n+([0-9]+)\s*$/;
filterOptionLocator = getNthFilterOptionLocator(page, i);
const filterNameMatch = (await filterOptionLocator.innerText())
.trim()
.match(filterOptionRegex);
if (filterNameMatch !== null) {
filterToSelect = filterNameMatch[1];
}
i += 1;
}
if (filterOptionLocator === undefined) {
throw new Error(
"No locator found within the maximum number of filter options"
);
}
return { index: i - 1, locator: filterOptionLocator, name: filterToSelect };
};
/**
* Checks that selecting a specified filter is persistent across the tabs in tabOrder
* @param page - a Playwright page object
* @param testFilterName - the name of the filter to check
* @param tabOrder - the tabs to check, in order. The filter will be selected on the first tab.
*/
export async function testFilterPersistence(
page: Page,
testFilterName: string,
tabOrder: TabDescription[]
): Promise<void> {
// Start on the first tab in the test order (should be files)
await page.goto(tabOrder[0].url);
// Select the first checkbox on the test filter
await page.getByText(filterRegex(testFilterName)).click();
const filterToSelectInfo = await getFirstNonEmptyFilterOptionInfo(page);
const filterToSelectLocator = filterToSelectInfo.locator;
const filterName = filterToSelectInfo.name;
const filterIndex = filterToSelectInfo.index;
await expect(filterToSelectLocator.getByRole("checkbox")).not.toBeChecked();
await filterToSelectLocator.getByRole("checkbox").click();
await expect(filterToSelectLocator.getByRole("checkbox")).toBeChecked();
await page.locator("body").click();
// Expect at least some text to still be visible
await expect(getFirstRowNthColumnCellLocator(page, 0)).toBeVisible();
// For each tab, check that the selected filter is still checked
for (const tab of tabOrder.slice(1)) {
await page
.getByRole("tab")
.getByText(tab.tabName, { exact: true })
.dispatchEvent("click");
await expect(page.getByText(filterRegex(testFilterName))).toBeVisible();
await page.getByText(filterRegex(testFilterName)).dispatchEvent("click");
await page.waitForLoadState("load");
const previouslySelected = getNamedFilterOptionLocator(page, filterName);
await expect(previouslySelected.getByRole("checkbox")).toBeChecked();
await page.waitForLoadState("load");
await page.locator("body").click();
}
// Return to the start tab and confirm that the filter stays checked and that some content is visible
// (dispatchevent necessary because the filter menu sometimes interrupts the click event)
await page
.getByRole("tab")
.getByText(tabOrder[0].tabName, { exact: true })
.dispatchEvent("click");
await expect(getFirstRowNthColumnCellLocator(page, 0)).toBeVisible();
await page.getByText(filterRegex(testFilterName)).dispatchEvent("click");
const previouslySelected = getNthFilterOptionLocator(page, filterIndex);
await expect(previouslySelected).toContainText(filterName, {
useInnerText: true,
});
await expect(previouslySelected.getByRole("checkbox").first()).toBeChecked();
}
/**
* Test that the counts associated with an array of filter names are reflected
* in the table
* @param page - a Playwright page object
* @param tab - the tab object to test
* @param filterNames - the names of the filters to select, in order
* @param elementsPerPage - the maximum number of elements per page
* @returns false if the test should fail and true if the test should pass
*/
export async function testFilterCounts(
page: Page,
tab: TabDescription,
filterNames: string[],
elementsPerPage: number
): Promise<boolean> {
await page.goto(tab.url);
// For each arbitrarily selected filter
for (const filterName of filterNames) {
// Select the filter
// (dispatchevent necessary because the filter menu sometimes interrupts the click event)
await page.getByText(filterRegex(filterName)).dispatchEvent("click");
// Get the number associated with the first filter button, and select it
await page.waitForLoadState("load");
const firstFilterOption = getFirstFilterOptionLocator(page);
const filterNumbers = (await firstFilterOption.innerText()).split("\n");
const filterNumber =
filterNumbers
.reverse()
.map((x) => Number(x))
.find((x) => !isNaN(x) && x !== 0) ?? -1;
if (filterNumber < 0) {
console.log(
"FILTER COUNTS: The number associated with the filter is negative"
);
return false;
}
// Check the filter
// (dispatchevent necessary because the filter menu sometimes interrupts the click event)
await firstFilterOption.getByRole("checkbox").dispatchEvent("click");
await page.waitForLoadState("load");
// Exit the filter menu
await page.locator("body").click();
await expect(page.getByRole("checkbox")).toHaveCount(0);
// Wait for the table to load
await expect(getFirstRowNthColumnCellLocator(page, 0)).toBeVisible();
// Expect the displayed count of elements to be 0
const firstNumber =
filterNumber <= elementsPerPage ? filterNumber : elementsPerPage;
await expect(
page.getByText("Results 1 - " + firstNumber + " of " + filterNumber)
).toBeVisible();
}
return true;
}
const FILTER_CSS_SELECTOR = "#sidebar-positioner";
/**
* Get a locator for a named filter tag
* @param page - a Playwright page object
* @param filterTagName - the name of the filter tag to search for
* @returns - a locator for the named filter tag
*/
const getFilterTagLocator = (page: Page, filterTagName: string): Locator => {
return page
.locator(FILTER_CSS_SELECTOR)
.getByText(filterTagName, { exact: true });
};
/**
* Check that the filter tabs appear when a filter is selected and that clicking
* them causes the filter to be deselected
* @param page - a Playwright page objet
* @param tab - the tab to check
* @param filterNames - the names of the filters to check
*/
export async function testFilterTags(
page: Page,
tab: TabDescription,
filterNames: string[]
): Promise<void> {
await page.goto(tab.url);
for (const filterName of filterNames) {
// Select a filter
// (dispatchevent necessary because the filter menu sometimes interrupts the click event)
await page.getByText(filterRegex(filterName)).dispatchEvent("click");
await page.waitForLoadState("load");
const firstFilterOptionLocator = getFirstFilterOptionLocator(page);
// Get the name of the selected filter
const firstFilterOptionName =
(await firstFilterOptionLocator.innerText())
.split("\n")
.find((x) => x.length > 0) ?? "";
// Click the selected filter and exit the filter menu
await firstFilterOptionLocator.getByRole("checkbox").click();
await page.waitForLoadState("load");
await page.locator("body").click();
await expect(page.getByRole("checkbox")).toHaveCount(0);
// Click the filter tag
const filterTagLocator = getFilterTagLocator(page, firstFilterOptionName);
await expect(filterTagLocator).toBeVisible();
await filterTagLocator.scrollIntoViewIfNeeded();
await filterTagLocator.dispatchEvent("click");
// Expect the tag to disappear when clicked
await expect(filterTagLocator).toHaveCount(0);
// Expect the filter to be deselected in the filter menu
await page.getByText(filterRegex(filterName)).dispatchEvent("click");
await expect(
firstFilterOptionLocator.getByRole("checkbox")
).not.toBeChecked();
await page.locator("body").click();
}
}
/**
* Check that selecting some filters then selecting the clear all button causes
* those filters to become deselected
* @param page - a Playwright page object
* @param tab - the tab object to test on
* @param filterNames - the names of the filters to check
*/
export async function testClearAll(
page: Page,
tab: TabDescription,
filterNames: string[]
): Promise<void> {
await page.goto(tab.url);
const selectedFilterNamesList = [];
// Select each filter and get the names of the actual filter text
// (dispatchevent necessary because the filter menu sometimes interrupts the click event)
for (const filterName of filterNames) {
await page.getByText(filterRegex(filterName)).dispatchEvent("click");
await getFirstFilterOptionLocator(page).getByRole("checkbox").click();
await expect(
getFirstFilterOptionLocator(page).getByRole("checkbox")
).toBeChecked();
selectedFilterNamesList.push(
await getFilterOptionName(getFirstFilterOptionLocator(page))
);
await page.locator("body").click();
}
// Click the "Clear All" button
await page.getByText("Clear All").dispatchEvent("click");
for (const filterName of selectedFilterNamesList) {
await expect(page.getByText(filterRegex(filterName))).toHaveCount(0);
}
// Ensure that the filters still show as unchecked
for (let i = 0; i < filterNames.length; i++) {
await page.getByText(filterRegex(filterNames[i])).dispatchEvent("click");
await expect(
getNamedFilterOptionLocator(page, selectedFilterNamesList[i]).getByRole(
"checkbox"
)
).not.toBeChecked();
await page.locator("body").click();
}
}
/**
* Escape a string so it can safely be used in a regexp
* @param string - the string to escape
* @returns - A string that has all Regexp special characters escaped
*/
function escapeRegExp(string: string): string {
// Searches for regex special characters and adds backslashes in front of them to escape
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
}
/**
* Run a test that gets the first filter option of each of the filters specified in
* filterNames, then attempts to select each through the filter search bar.
* @param page - a Playwright page object
* @param tab - the Tab object to run the test on
* @param filterNames - an array of potential filter names on the selected tab
*/
export async function testSelectFiltersThroughSearchBar(
page: Page,
tab: TabDescription,
filterNames: string[]
): Promise<void> {
await page.goto(tab.url);
for (const filterName of filterNames) {
// Get the first filter option
await expect(page.getByText(filterRegex(filterName))).toBeVisible();
// (dispatchevent necessary because the filter menu sometimes interrupts the click event)
await page.getByText(filterRegex(filterName)).dispatchEvent("click");
const firstFilterOptionLocator = getFirstFilterOptionLocator(page);
const filterOptionName = await getFilterOptionName(
firstFilterOptionLocator
);
await page.locator("body").click();
// Search for the filter option
const searchFiltersInputLocator = page.getByPlaceholder(
tab.searchFiltersPlaceholderText,
{ exact: true }
);
await expect(searchFiltersInputLocator).toBeVisible();
await searchFiltersInputLocator.fill(filterOptionName);
// Select a filter option with a matching name
await getNamedFilterOptionLocator(page, filterOptionName).first().click();
await page.locator("body").click();
const filterTagLocator = getFilterTagLocator(page, filterOptionName);
// Check the filter tag is selected and click it to reset the filter
await expect(filterTagLocator).toBeVisible();
await filterTagLocator.dispatchEvent("click");
}
}
/**
* Run a test that selects the first filter option of each of the filters specified in
* filterNames, then attempts to deselect each through the filter search bar.
* @param page - a Playwright page object
* @param tab - the Tab object to run the test on
* @param filterNames - an array of potential filter names on the selected tab
*/
export async function testDeselectFiltersThroughSearchBar(
page: Page,
tab: TabDescription,
filterNames: string[]
): Promise<void> {
await page.goto(tab.url);
for (const filterName of filterNames) {
// Select each filter option
await expect(page.getByText(filterRegex(filterName))).toBeVisible();
// (dispatchevent necessary because the filter menu sometimes interrupts the click event)
await page.getByText(filterRegex(filterName)).dispatchEvent("click");
const firstFilterOptionLocator = getFirstFilterOptionLocator(page);
const filterOptionName = await getFilterOptionName(
firstFilterOptionLocator
);
await firstFilterOptionLocator.click();
await page.locator("body").click();
// Search for and check the selected filter
const searchFiltersInputLocator = page.getByPlaceholder(
tab.searchFiltersPlaceholderText,
{ exact: true }
);
await expect(searchFiltersInputLocator).toBeVisible();
await searchFiltersInputLocator.fill(filterOptionName);
await getNamedFilterOptionLocator(page, filterOptionName)
.locator("input[type='checkbox']:checked")
.first()
.click();
await page.locator("body").click();
const filterTagLocator = getFilterTagLocator(page, filterOptionName);
await expect(filterTagLocator).not.toBeVisible();
}
}
/**
* Get the first link to a backpage with specified backpage access
* @param page - a Playright page locator
* @param access - the string denoting the level of access desired
* @returns a Playwright locator object to the first link to a backpage with the specified access
*/
const getBackpageLinkLocatorByAccess = (page: Page, access: string): Locator =>
page
.getByRole("row")
.filter({ has: page.getByRole("cell", { name: access }) })
.first()
.getByRole("cell")
.first()
.getByRole("link");
/**
* Make an export request that leaves only the minimal number of checkboxes selected
* @param page - a Playwright page object
* @param exportRequestButtonLocator - a Playwright locator to the button to click after the export request is completed
*/
const makeMinimalExportRequest = async (
page: Page,
exportRequestButtonLocator: Locator
): Promise<void> => {
await expect(exportRequestButtonLocator).toBeEnabled();
// Expect there to be exactly one table on the export page
await expect(page.getByRole("table")).toHaveCount(1);
// Select all checkboxes that are not in a table
const allNonTableCheckboxLocators = await page
.locator("input[type='checkbox']:not(table input[type='checkbox'])")
.all();
for (const checkboxLocator of allNonTableCheckboxLocators) {
await checkboxLocator.click();
await expect(checkboxLocator).toBeChecked();
await expect(checkboxLocator).toBeEnabled();
}
// Check the second checkbox in the table (this should be the checkbox after the "select all checkbox")
const tableLocator = page.getByRole("table");
const allInTableCheckboxLocators = await tableLocator
.getByRole("checkbox")
.all();
const secondCheckboxInTableLocator = allInTableCheckboxLocators[1];
await secondCheckboxInTableLocator.click();
await expect(secondCheckboxInTableLocator).toBeChecked();
await expect(secondCheckboxInTableLocator).toBeEnabled();
// Make sure that no other checkboxes are selected
const otherInTableCheckboxLocators = [
allInTableCheckboxLocators[0],
...allInTableCheckboxLocators.slice(2),
];
for (const otherCheckboxLocator of otherInTableCheckboxLocators) {
await expect(otherCheckboxLocator).not.toBeChecked();
await expect(otherCheckboxLocator).toBeEnabled();
}
// Click the Export Request button
await expect(exportRequestButtonLocator).toBeEnabled();
await exportRequestButtonLocator.click();
};
/**
* Test the export process for the specified tab
* @param context - a Playwright browser context object
* @param page - a Playwright page object
* @param tab - the tab to test on
* @returns - true if the test passes, false if the test fails but does not fail an assertion
*/
export async function testExportBackpage(
context: BrowserContext,
page: Page,
tab: TabDescription
): Promise<boolean> {
if (
tab.backpageExportButtons === undefined ||
tab.backpageAccessTags === undefined
) {
// Fail if this test u on a tab without defined backpages
return false;
}
// Goto the specified tab
await page.goto(tab.url);
// Expect to find row with a granted status indicator
const grantedRowLocator = getBackpageLinkLocatorByAccess(
page,
tab.backpageAccessTags.grantedShortName
);
await expect(grantedRowLocator).toBeVisible();
// Click into the selected row
// dispatchEvent necessary because the table loading component sometimes interrupts a click event
await grantedRowLocator.dispatchEvent("click");
await expect(
page.getByText(tab.backpageExportButtons.detailsName)
).toBeVisible();
// Click the "Export" tab
await page
.getByText(tab.backpageExportButtons.exportTabName, {
exact: true,
})
.click();
await expect(
page.getByText(tab.backpageExportButtons.requestLandingMessage)
).toBeVisible();
await expect(page).toHaveURL(tab.backpageExportButtons.exportUrlRegExp);
await expect(page.getByRole("checkbox").first()).toBeVisible();
const exportRequestButtonLocator = page.getByRole("button", {
name: tab.backpageExportButtons.exportRequestButtonText,
});
// Complete the export request form
await makeMinimalExportRequest(page, exportRequestButtonLocator);
await expect(
page.getByText(tab.backpageExportButtons.actionLandingMessage, {
exact: true,
})
).toBeVisible({ timeout: TIMEOUT_EXPORT_REQUEST });
const exportActionButtonLocator = page.getByRole("button", {
name: tab.backpageExportButtons?.exportActionButtonText,
});
await expect(exportActionButtonLocator).toBeEnabled();
// Click the Export Action Button and await a new browser tab
const newPagePromise = context.waitForEvent("page");
await exportActionButtonLocator.click();
const newPage = await newPagePromise;
// Expect the new browser tab to display the new tab content
await expect(
newPage.getByText(tab.backpageExportButtons?.newTabMessage)
).toBeVisible();
return true;
}
/**
* Test that export access is available on entries where access shows as available
* and is not on entries where access shows as unavailable
* @param page - a Playwright page objext
* @param tab - the tab object to test on
* @returns - true if the test passes, false if the test fails but does not fail an assertion
*/
export async function testBackpageAccess(
page: Page,
tab: TabDescription
): Promise<boolean> {
if (
tab.backpageExportButtons === undefined ||
tab.backpageAccessTags === undefined
) {
// Fail if this test is run on a tab without defined backpages
return false;
}
// Goto the specified tab
await page.goto(tab.url);
// Check that the first "Granted" tab has access granted
const grantedRowLocator = getBackpageLinkLocatorByAccess(
page,
tab.backpageAccessTags.grantedShortName
);
await expect(grantedRowLocator).toBeVisible();
// dispatchEvent necessary because the table loading component sometimes interrupts a click event
await grantedRowLocator.dispatchEvent("click");
await expect(
page.getByText(tab.backpageExportButtons.detailsName)
).toBeVisible();
await expect(
page.getByText(tab.backpageAccessTags.grantedLongName)
).toBeVisible();
await page
.getByText(tab.backpageExportButtons.exportTabName, {
exact: true,
})
.click();
await expect(page).toHaveURL(tab.backpageExportButtons.exportUrlRegExp);
await expect(page.getByRole("checkbox").first()).toBeVisible();
const requestLinkButtonLocator = page.getByRole("button", {
name: tab.backpageExportButtons.exportRequestButtonText,
});
await expect(requestLinkButtonLocator).toBeEnabled();
// Go back to the table page
await page.getByRole("link", { name: tab.tabName }).click();
// Check that the first "Required" tab does not have access granted
const deniedRowLocator = getBackpageLinkLocatorByAccess(
page,
tab.backpageAccessTags.deniedShortName
);
await expect(deniedRowLocator).toBeVisible();
// dispatchEvent necessary because the table loading component sometimes interrupts a click event
await deniedRowLocator.dispatchEvent("click");
await expect(
page.getByText(tab.backpageAccessTags.deniedLongName)
).toBeVisible();
await page
.getByText(tab.backpageExportButtons.exportTabName, {
exact: true,
})
.click();
await expect(page).toHaveURL(tab.backpageExportButtons.exportUrlRegExp);
await expect(
page.getByText(tab.backpageExportButtons.accessNotGrantedMessage, {
exact: true,
})