-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathSearchPageMatchers.ts
More file actions
152 lines (133 loc) · 4.34 KB
/
SearchPageMatchers.ts
File metadata and controls
152 lines (133 loc) · 4.34 KB
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
import { expect as baseExpect } from "@playwright/test";
import type { SearchPage } from "../pages/search-page/SearchPage";
import type { MatcherResult } from "./types";
export interface SearchPageMatchers {
toHaveAutoFillHidden(): Promise<MatcherResult>;
toHaveRelevantAutoFillResults(searchText: string): Promise<MatcherResult>;
toHaveAutoFillCategoriesWithinLimit(limit: number): Promise<MatcherResult>;
}
type SearchPageMatcherDefinitions = {
readonly [K in keyof SearchPageMatchers]: (
receiver: SearchPage,
...args: Parameters<SearchPageMatchers[K]>
) => Promise<MatcherResult>;
};
export const searchPageAssertions =
baseExpect.extend<SearchPageMatcherDefinitions>({
toHaveAutoFillHidden: async (
searchPage: SearchPage,
): Promise<MatcherResult> => {
try {
const menu = searchPage.getAutoFillMenu();
await baseExpect(menu).not.toBeVisible();
return {
pass: true,
message: () => "Autofill menu is not visible",
};
} catch (error) {
return {
pass: false,
message: () =>
error instanceof Error ? error.message : String(error),
};
}
},
toHaveRelevantAutoFillResults: async (
searchPage: SearchPage,
searchText: string,
): Promise<MatcherResult> => {
try {
const menu = searchPage.getAutoFillMenu();
await baseExpect(menu).toBeVisible();
const menuItems = searchPage.getAutoFillMenuItems();
const count = await menuItems.count();
if (count === 0) {
return {
pass: false,
message: () => "Autofill menu has no items",
};
}
// Check that at least one menu item contains the search text
const searchTextLower = searchText.toLowerCase();
let foundMatch = false;
for (let i = 0; i < count; i++) {
const item = menuItems.nth(i);
const text = await item.textContent();
if (text?.toLowerCase().includes(searchTextLower)) {
foundMatch = true;
break;
}
}
if (!foundMatch) {
return {
pass: false,
message: () =>
`No autofill items contain search text "${searchText}"`,
};
}
return {
pass: true,
message: () => `Autofill has relevant results for "${searchText}"`,
};
} catch (error) {
return {
pass: false,
message: () =>
error instanceof Error ? error.message : String(error),
};
}
},
toHaveAutoFillCategoriesWithinLimit: async (
searchPage: SearchPage,
limit: number,
): Promise<MatcherResult> => {
try {
const menuItems = searchPage.getAutoFillMenuLinks();
const categoryCount: Record<string, number> = {
advisories: 0,
packages: 0,
sboms: 0,
vulnerabilities: 0,
};
const count = await menuItems.count();
for (let i = 0; i < count; i++) {
const link = menuItems.nth(i);
const href = await link.getAttribute("href");
if (href?.includes("/advisories/")) {
categoryCount.advisories++;
} else if (href?.includes("/packages/")) {
categoryCount.packages++;
} else if (href?.includes("/sboms/")) {
categoryCount.sboms++;
} else if (href?.includes("/vulnerabilities/")) {
categoryCount.vulnerabilities++;
}
}
// Check if any category exceeds the limit
const violations: string[] = [];
for (const [category, count] of Object.entries(categoryCount)) {
if (count > limit) {
violations.push(`${category}: ${count} > ${limit}`);
}
}
if (violations.length > 0) {
return {
pass: false,
message: () =>
`Categories exceed limit of ${limit}: ${violations.join(", ")}`,
};
}
return {
pass: true,
message: () =>
`All categories within limit of ${limit}: ${JSON.stringify(categoryCount)}`,
};
} catch (error) {
return {
pass: false,
message: () =>
error instanceof Error ? error.message : String(error),
};
}
},
});