This repository was archived by the owner on Sep 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathToolbarTable.ts
More file actions
232 lines (218 loc) · 7.73 KB
/
ToolbarTable.ts
File metadata and controls
232 lines (218 loc) · 7.73 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
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
import { expect, Page } from "@playwright/test";
export class ToolbarTable {
private readonly _page: Page;
private _tableName: string;
constructor(page: Page, tableName: string) {
this._page = page;
this._tableName = tableName;
}
async verifyPaginationHasTotalResults(totalResults: number) {
const paginationTop = this._page.locator("#pagination-id-top-toggle");
await expect(paginationTop.locator("b").nth(1)).toHaveText(
`${totalResults}`
);
}
async verifyPaginationHasTotalResultsGreatherThan(
totalResults: number,
include: boolean = false
) {
const paginationTop = this._page.locator("#pagination-id-top-toggle");
const totalResultsText = await paginationTop
.locator("b")
.nth(1)
.textContent();
if (include) {
expect(Number(totalResultsText)).toBeGreaterThanOrEqual(totalResults);
} else {
expect(Number(totalResultsText)).toBeGreaterThan(totalResults);
}
}
async filterByText(filterText: string) {
const input = this._page.locator("#search-input");
await input.fill(filterText);
await input.press("Enter");
}
async verifyTableIsSortedBy(columnName: string, asc: boolean = true) {
const table = this.getTable();
await expect(
table.getByRole("columnheader", { name: columnName })
).toHaveAttribute("aria-sort", asc ? "ascending" : "descending");
}
async verifyColumnContainsText(columnName: any, expectedValue: any) {
const table = this.getTable();
await expect(table.locator(`td[data-label="${columnName}"]`)).toContainText(
expectedValue
);
}
/**
* Verifies the pagination count against per page selection
* @param parentElem required to identify the pagination across sections
* Example, SBOM Explorer - Vulnerabilities and Packages section top and bottom sections.
* Parent Element for Vulnerabilities section top Pagination `//div[@id="vulnerability-table-pagination-top"]`
* And bottom section `//div[@id="vulnerability-table-pagination-bottom"]`
*/
async verifyPagination(parentElem: string) {
const section = this._page.locator(parentElem);
const perPageValues = [10, 20, 50, 100];
const totalRows = await this.getTotalRowsFromPagination(parentElem);
for (const value of perPageValues) {
const firstPage = section.getByRole("button", {
name: "Go to first page",
});
if (await firstPage.isEnabled()) {
await firstPage.click();
}
let expectedPagecount = Math.trunc(totalRows / value);
let remainingRows = totalRows % value;
if (remainingRows > 0) {
expectedPagecount += 1;
}
await this.selectPerPage(parentElem, value + " per page");
const progressBar = this._page.getByRole("gridcell", {
name: "Loading...",
});
await progressBar.waitFor({ state: "hidden", timeout: 5000 });
const actualPageCount =
await this.getTotalPagesFromNavigation(parentElem);
await expect(actualPageCount, "Page count mismatches").toEqual(
expectedPagecount
);
await this.navigateToEachPageVerifyRowsCount(
parentElem,
expectedPagecount,
value,
remainingRows
);
}
}
/**
* Retrieves the Total page count from pagination
* @param parentElem required to identify the pagination across sections
* @returns total count from pagination text
*/
async getTotalPagesFromNavigation(parentElem: string): Promise<number> {
const section = this._page.locator(parentElem);
const navTotal = await section.locator(
`xpath=//span[contains(@class,'form-control')]/following-sibling::span`
);
const totalPages = await navTotal.textContent();
return parseInt(totalPages!.replace("of", "").trim(), 10);
}
/**
* Retrieves the Total Row count from pagination
* @param parentElem required to differentiate top and bottom pagination
* @returns total row count from pagination dropdown
*/
async getTotalRowsFromPagination(parentElem: string): Promise<number> {
const tableError = this._page.locator(
`xpath=(//tbody[@aria-label="Table error"])[1]`
);
if (await tableError.isVisible()) {
await expect(tableError, "No Data available").not.toBeVisible();
}
const progressBar = this._page.getByRole("gridcell", {
name: "Loading...",
});
await progressBar.waitFor({ state: "hidden", timeout: 5000 });
const pagination = this._page.locator(parentElem);
const totalResultsText = await pagination
.locator(`xpath=//button//b[not(contains (.,'-'))]`)
.textContent();
return parseInt(totalResultsText!.trim(), 10);
}
/**
* Selects Number of rows per page on the table
* @param perPage Number of rows
*/
async selectPerPage(parentElem: string, perPage: string) {
const pagination = this._page.locator(parentElem);
await pagination.locator(`//button[@aria-haspopup='listbox']`).click();
await this._page.getByRole("menuitem", { name: perPage }).click();
}
/**
* Verifies Number of rows on the table equals to or lesser than the row count given
* @param rowsCount Number of rows
*/
async verifyPerPageToRowCount(rowsCount: number) {
const table = this.getTable();
const rows = await table.locator(`xpath=//tbody/tr`);
const tabRows = await rows.count();
// Bug: https://issues.redhat.com/browse/TC-2353
await expect(tabRows).toEqual(rowsCount);
}
/**
* Navigates to Each page with Next button and verify the rows count
* @param parentElem required to identify the pagination across sections
* @param pageCount Number of Pages expected
* @param perPageRows Number of rows expected per page
* @param remainingRows Number of rows in Last Page
*/
async navigateToEachPageVerifyRowsCount(
parentElem: string,
pageCount: number,
perPageRows: number,
remainingRows: number
) {
const section = this._page.locator(parentElem);
const nextButton = await section.getByLabel("Go to next page");
let expMinCount = 1;
let expMaxCount = perPageRows;
if (pageCount === 1) {
expMaxCount = remainingRows;
}
for (let i = 1; i < pageCount; i++) {
await this.verifyRowsCounterPagination(
parentElem,
expMinCount,
expMaxCount
);
await this.verifyPerPageToRowCount(perPageRows);
await nextButton.isEnabled();
await nextButton.click();
const progressBar = this._page.getByRole("gridcell", {
name: "Loading...",
});
await progressBar.waitFor({ state: "hidden", timeout: 5000 });
expMinCount += perPageRows;
if (i === pageCount - 1) {
expMaxCount = expMaxCount + remainingRows;
} else {
expMaxCount += perPageRows;
}
}
if (remainingRows > 0) {
await this.verifyPerPageToRowCount(remainingRows);
await this.verifyRowsCounterPagination(
parentElem,
expMinCount,
expMaxCount
);
}
await nextButton.isDisabled();
}
/**
*
* @param parentElem required to differentiate top and bottom pagination
* @param expMinCount Expected Min count on the counter
* @param expMaxCount Expected Max count on the counter
*/
async verifyRowsCounterPagination(
parentElem: string,
expMinCount: number,
expMaxCount: number
) {
const pagination = this._page.locator(parentElem);
const pageCounter = await pagination.locator(
`xpath=//button//b[contains (.,"-")]`
);
const counterText = await pageCounter.textContent();
let [min, max] = counterText!
.split("-")
.map((value) => parseInt(value.trim()));
await expect(min).toEqual(expMinCount);
await expect(max).toEqual(expMaxCount);
}
private getTable() {
return this._page.locator(`table[aria-label="${this._tableName}"]`);
}
}