@cerios/playwright-table 1.1.3 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +7 -7
- package/README.md +875 -447
- package/dist/index.d.mts +317 -92
- package/dist/index.d.ts +317 -92
- package/dist/index.js +737 -222
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +737 -222
- package/dist/index.mjs.map +1 -1
- package/package.json +27 -23
package/dist/index.js
CHANGED
|
@@ -25,8 +25,27 @@ __export(index_exports, {
|
|
|
25
25
|
});
|
|
26
26
|
module.exports = __toCommonJS(index_exports);
|
|
27
27
|
|
|
28
|
-
// src/
|
|
29
|
-
|
|
28
|
+
// src/polling.ts
|
|
29
|
+
async function pollTable(fn, options) {
|
|
30
|
+
var _a, _b;
|
|
31
|
+
if (!options) {
|
|
32
|
+
options = {};
|
|
33
|
+
}
|
|
34
|
+
const timeout = (_a = options.timeout) != null ? _a : 3e4;
|
|
35
|
+
const interval = (_b = options.interval) != null ? _b : 100;
|
|
36
|
+
const startTime = Date.now();
|
|
37
|
+
let lastError;
|
|
38
|
+
while (Date.now() - startTime < timeout) {
|
|
39
|
+
try {
|
|
40
|
+
await fn();
|
|
41
|
+
return;
|
|
42
|
+
} catch (err) {
|
|
43
|
+
lastError = err;
|
|
44
|
+
await new Promise((resolve) => setTimeout(resolve, interval));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
throw lastError || new Error("Playwright Tables: Polling timeout reached");
|
|
48
|
+
}
|
|
30
49
|
|
|
31
50
|
// src/cell-content-type.ts
|
|
32
51
|
var CellContentType = /* @__PURE__ */ ((CellContentType2) => {
|
|
@@ -37,34 +56,127 @@ var CellContentType = /* @__PURE__ */ ((CellContentType2) => {
|
|
|
37
56
|
|
|
38
57
|
// src/table-utils.ts
|
|
39
58
|
var TableUtils = class {
|
|
59
|
+
constructor() {
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Extracts text content from a table cell.
|
|
63
|
+
* @param cell - The Playwright locator for the cell.
|
|
64
|
+
* @param cellContentType - The type of content to extract (InnerText or TextContent).
|
|
65
|
+
* @returns The trimmed text content of the cell, or empty string if no content.
|
|
66
|
+
* @throws Error if the cell locator doesn't exist in the DOM.
|
|
67
|
+
*/
|
|
40
68
|
static async getCellContent(cell, cellContentType) {
|
|
41
|
-
|
|
42
|
-
|
|
69
|
+
try {
|
|
70
|
+
const count = await cell.count();
|
|
71
|
+
if (count === 0) {
|
|
72
|
+
throw new Error("Cell locator does not exist in the DOM");
|
|
73
|
+
}
|
|
74
|
+
const content = cellContentType === "innerText" /* InnerText */ ? await cell.innerText() : await cell.textContent();
|
|
75
|
+
return (content == null ? void 0 : content.trim()) || "";
|
|
76
|
+
} catch (error) {
|
|
77
|
+
const locatorDescription = this.getLocatorDescription(cell);
|
|
78
|
+
throw new Error(
|
|
79
|
+
`Failed to get cell content.
|
|
80
|
+
Locator: ${locatorDescription}
|
|
81
|
+
Content type: ${cellContentType}
|
|
82
|
+
Error: ${error instanceof Error ? error.message : String(error)}`
|
|
83
|
+
);
|
|
84
|
+
}
|
|
43
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* Parses rowspan and colspan attributes from a table cell.
|
|
88
|
+
* Fetches both attributes in parallel for optimal performance.
|
|
89
|
+
* @param cell - The Playwright locator for the cell.
|
|
90
|
+
* @returns Object containing rowspan and colspan values (defaults to 1 if not present).
|
|
91
|
+
* @throws Error if attributes contain invalid values (non-numeric or less than 1).
|
|
92
|
+
*/
|
|
44
93
|
static async parseSpanAttributes(cell) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
94
|
+
try {
|
|
95
|
+
const [rowspanAttr, colspanAttr] = await Promise.all([
|
|
96
|
+
cell.getAttribute("rowspan"),
|
|
97
|
+
cell.getAttribute("colspan")
|
|
98
|
+
]);
|
|
99
|
+
const rowspan = parseInt(rowspanAttr || "1", 10);
|
|
100
|
+
const colspan = parseInt(colspanAttr || "1", 10);
|
|
101
|
+
if (isNaN(rowspan) || rowspan < 1) {
|
|
102
|
+
throw new Error(`Invalid rowspan attribute: "${rowspanAttr}". Expected positive integer.`);
|
|
103
|
+
}
|
|
104
|
+
if (isNaN(colspan) || colspan < 1) {
|
|
105
|
+
throw new Error(`Invalid colspan attribute: "${colspanAttr}". Expected positive integer.`);
|
|
106
|
+
}
|
|
107
|
+
return { rowspan, colspan };
|
|
108
|
+
} catch (error) {
|
|
109
|
+
const locatorDescription = this.getLocatorDescription(cell);
|
|
110
|
+
throw new Error(
|
|
111
|
+
`Failed to parse span attributes.
|
|
112
|
+
Locator: ${locatorDescription}
|
|
113
|
+
Error: ${error instanceof Error ? error.message : String(error)}`
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Extracts a human-readable locator description from a Playwright Locator.
|
|
119
|
+
* This captures Playwright's internal selector chain for better error messages.
|
|
120
|
+
* @param locator - The Playwright locator to describe.
|
|
121
|
+
* @returns A string representation of the locator's selector chain.
|
|
122
|
+
*/
|
|
123
|
+
static getLocatorDescription(locator) {
|
|
124
|
+
const locatorString = locator.toString();
|
|
125
|
+
return locatorString;
|
|
49
126
|
}
|
|
50
127
|
};
|
|
51
128
|
|
|
52
129
|
// src/table-body.ts
|
|
53
130
|
var TableBody = class {
|
|
131
|
+
constructor() {
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Extracts body rows from a table with proper handling of rowspan and colspan.
|
|
135
|
+
*
|
|
136
|
+
* @param rowLocator - The Playwright locator for body rows.
|
|
137
|
+
* @param columnsSelector - CSS selector for locating columns within rows (e.g., "td").
|
|
138
|
+
* @param bodyRowsOptions - Optional configuration for cell content extraction.
|
|
139
|
+
* @param bodyRowsOptions.cellContentType - The type of content to extract (InnerText or TextContent).
|
|
140
|
+
* @returns Promise resolving to an array of processed body rows.
|
|
141
|
+
* @throws Error if columnsSelector is empty or if row processing fails.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* const rows = await TableBody.getRows(
|
|
145
|
+
* page.locator('tbody tr'),
|
|
146
|
+
* 'td',
|
|
147
|
+
* { cellContentType: CellContentType.TextContent }
|
|
148
|
+
* );
|
|
149
|
+
*/
|
|
54
150
|
static async getRows(rowLocator, columnsSelector, bodyRowsOptions) {
|
|
151
|
+
if (!columnsSelector || columnsSelector.trim() === "") {
|
|
152
|
+
throw new Error("columnsSelector cannot be empty");
|
|
153
|
+
}
|
|
154
|
+
const locatorDescription = rowLocator.toString();
|
|
55
155
|
const rows = [];
|
|
56
156
|
const rowsCount = await rowLocator.count();
|
|
157
|
+
if (rowsCount === 0) {
|
|
158
|
+
return rows;
|
|
159
|
+
}
|
|
57
160
|
const spannedCells = {};
|
|
58
161
|
for (let rowIndex = 0; rowIndex < rowsCount; rowIndex++) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
162
|
+
try {
|
|
163
|
+
const row = rowLocator.nth(rowIndex);
|
|
164
|
+
const columns = await this.extractColumns(
|
|
165
|
+
row,
|
|
166
|
+
columnsSelector,
|
|
167
|
+
rowIndex,
|
|
168
|
+
spannedCells,
|
|
169
|
+
bodyRowsOptions == null ? void 0 : bodyRowsOptions.cellContentType
|
|
170
|
+
);
|
|
171
|
+
rows.push(columns);
|
|
172
|
+
} catch (error) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
`Failed to process body row at index ${rowIndex}.
|
|
175
|
+
Body row locator: ${locatorDescription}
|
|
176
|
+
Columns selector: "${columnsSelector}"
|
|
177
|
+
Error: ${error instanceof Error ? error.message : String(error)}`
|
|
178
|
+
);
|
|
179
|
+
}
|
|
68
180
|
}
|
|
69
181
|
return rows;
|
|
70
182
|
}
|
|
@@ -72,35 +184,77 @@ var TableBody = class {
|
|
|
72
184
|
const columns = [];
|
|
73
185
|
const columnLocators = row.locator(columnsSelector);
|
|
74
186
|
const columnCount = await columnLocators.count();
|
|
187
|
+
if (columnCount === 0) {
|
|
188
|
+
this.applySpannedCells(spannedCells, rowIndex, columns);
|
|
189
|
+
return columns;
|
|
190
|
+
}
|
|
191
|
+
const cellDataPromises = [];
|
|
75
192
|
for (let colIndex = 0; colIndex < columnCount; colIndex++) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
193
|
+
cellDataPromises.push(this.fetchCellData(columnLocators.nth(colIndex), cellContentType));
|
|
194
|
+
}
|
|
195
|
+
const cellDataArray = await Promise.all(cellDataPromises);
|
|
196
|
+
const spannedPositions = /* @__PURE__ */ new Set();
|
|
197
|
+
if (spannedCells[rowIndex]) {
|
|
198
|
+
spannedCells[rowIndex].forEach((_, colIndex) => {
|
|
199
|
+
if (spannedCells[rowIndex][colIndex] !== void 0) {
|
|
200
|
+
spannedPositions.add(colIndex);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
let actualColIndex = 0;
|
|
205
|
+
for (let domCellIndex = 0; domCellIndex < columnCount; domCellIndex++) {
|
|
206
|
+
while (spannedPositions.has(actualColIndex)) {
|
|
207
|
+
actualColIndex++;
|
|
208
|
+
}
|
|
209
|
+
const cellData = cellDataArray[domCellIndex];
|
|
210
|
+
this.processCellData(cellData, actualColIndex, columns, rowIndex, spannedCells);
|
|
211
|
+
actualColIndex += cellData.colspan;
|
|
84
212
|
}
|
|
85
213
|
this.applySpannedCells(spannedCells, rowIndex, columns);
|
|
86
214
|
return columns;
|
|
87
215
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
216
|
+
/**
|
|
217
|
+
* Fetches cell content and span attributes in parallel.
|
|
218
|
+
* This method performs DOM operations that can be parallelized across multiple cells.
|
|
219
|
+
*/
|
|
220
|
+
static async fetchCellData(column, cellContentType = "innerText" /* InnerText */) {
|
|
221
|
+
const [content, spanAttributes] = await Promise.all([
|
|
222
|
+
TableUtils.getCellContent(column, cellContentType),
|
|
223
|
+
TableUtils.parseSpanAttributes(column)
|
|
224
|
+
]);
|
|
225
|
+
return {
|
|
226
|
+
content,
|
|
227
|
+
rowspan: spanAttributes.rowspan,
|
|
228
|
+
colspan: spanAttributes.colspan
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Processes pre-fetched cell data and applies it to the columns array.
|
|
233
|
+
* This method is purely computational and doesn't perform any DOM operations.
|
|
234
|
+
*/
|
|
235
|
+
static processCellData(cellData, colIndex, columns, rowIndex, spannedCells) {
|
|
236
|
+
const { content, rowspan, colspan } = cellData;
|
|
237
|
+
if (colIndex < 0) {
|
|
238
|
+
throw new Error(`Invalid column index: ${colIndex}. Must be non-negative.`);
|
|
239
|
+
}
|
|
240
|
+
if (rowIndex < 0) {
|
|
241
|
+
throw new Error(`Invalid row index: ${rowIndex}. Must be non-negative.`);
|
|
242
|
+
}
|
|
91
243
|
for (let span = 0; span < colspan; span++) {
|
|
92
244
|
columns[colIndex + span] = content;
|
|
93
245
|
}
|
|
94
246
|
if (rowspan > 1) {
|
|
95
|
-
this.storeSpannedCells(rowIndex, colIndex, rowspan, content, spannedCells);
|
|
247
|
+
this.storeSpannedCells(rowIndex, colIndex, rowspan, colspan, content, spannedCells);
|
|
96
248
|
}
|
|
97
249
|
}
|
|
98
|
-
static storeSpannedCells(rowIndex, colIndex, rowspan, content, spannedCells) {
|
|
99
|
-
for (let
|
|
100
|
-
if (!spannedCells[rowIndex +
|
|
101
|
-
spannedCells[rowIndex +
|
|
250
|
+
static storeSpannedCells(rowIndex, colIndex, rowspan, colspan, content, spannedCells) {
|
|
251
|
+
for (let rowSpan = 1; rowSpan < rowspan; rowSpan++) {
|
|
252
|
+
if (!spannedCells[rowIndex + rowSpan]) {
|
|
253
|
+
spannedCells[rowIndex + rowSpan] = [];
|
|
254
|
+
}
|
|
255
|
+
for (let colSpan = 0; colSpan < colspan; colSpan++) {
|
|
256
|
+
spannedCells[rowIndex + rowSpan][colIndex + colSpan] = content;
|
|
102
257
|
}
|
|
103
|
-
spannedCells[rowIndex + span][colIndex] = content;
|
|
104
258
|
}
|
|
105
259
|
}
|
|
106
260
|
static applySpannedCells(spannedCells, rowIndex, columns) {
|
|
@@ -116,14 +270,48 @@ var TableBody = class {
|
|
|
116
270
|
|
|
117
271
|
// src/table-header.ts
|
|
118
272
|
var TableHeader = class {
|
|
273
|
+
constructor() {
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Extracts header rows from a table with optional processing for colspan, rowspan, and duplicates.
|
|
277
|
+
*
|
|
278
|
+
* @param headerRowLocator - The Playwright locator for header rows.
|
|
279
|
+
* @param columnsSelector - CSS selector for locating header columns (e.g., "th").
|
|
280
|
+
* @param headerRowOptions - Optional configuration for header processing.
|
|
281
|
+
* @returns Promise resolving to an array of processed header rows.
|
|
282
|
+
* @throws Error if columnsSelector is empty or if no header rows are found.
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* const headers = await TableHeader.getRows(
|
|
286
|
+
* page.locator('thead tr'),
|
|
287
|
+
* 'th',
|
|
288
|
+
* { duplicateSuffix: true, colspan: { enabled: true } }
|
|
289
|
+
* );
|
|
290
|
+
*/
|
|
119
291
|
static async getRows(headerRowLocator, columnsSelector, headerRowOptions) {
|
|
292
|
+
if (!columnsSelector || columnsSelector.trim() === "") {
|
|
293
|
+
throw new Error("columnsSelector cannot be empty");
|
|
294
|
+
}
|
|
295
|
+
const locatorDescription = headerRowLocator.toString();
|
|
120
296
|
const options = this.getDefaultOptions(headerRowOptions);
|
|
121
297
|
const rows = await headerRowLocator.all();
|
|
122
298
|
const headerRows = [];
|
|
299
|
+
if (rows.length === 0) {
|
|
300
|
+
return headerRows;
|
|
301
|
+
}
|
|
123
302
|
const rowSpans = /* @__PURE__ */ new Map();
|
|
124
|
-
for (
|
|
125
|
-
|
|
126
|
-
|
|
303
|
+
for (let rowIndex = 0; rowIndex < rows.length; rowIndex++) {
|
|
304
|
+
try {
|
|
305
|
+
const headerRow = await this.processRow(rows[rowIndex], columnsSelector, rowSpans, options);
|
|
306
|
+
headerRows.push(headerRow);
|
|
307
|
+
} catch (error) {
|
|
308
|
+
throw new Error(
|
|
309
|
+
`Failed to process header row at index ${rowIndex}.
|
|
310
|
+
Header row locator: ${locatorDescription}
|
|
311
|
+
Columns selector: "${columnsSelector}"
|
|
312
|
+
Error: ${error instanceof Error ? error.message : String(error)}`
|
|
313
|
+
);
|
|
314
|
+
}
|
|
127
315
|
}
|
|
128
316
|
this.applyColspanOptions(headerRows, options);
|
|
129
317
|
this.applyDuplicateSuffix(headerRows, options);
|
|
@@ -144,24 +332,53 @@ var TableHeader = class {
|
|
|
144
332
|
static async processRow(row, columnsSelector, rowSpans, options) {
|
|
145
333
|
const headerRow = [];
|
|
146
334
|
const cells = await row.locator(columnsSelector).all();
|
|
335
|
+
if (cells.length === 0) {
|
|
336
|
+
return headerRow;
|
|
337
|
+
}
|
|
338
|
+
const cellDataArray = await Promise.all(cells.map((cell) => this.fetchHeaderCellData(cell, options.cellContentType)));
|
|
147
339
|
let columnIndex = 0;
|
|
148
|
-
for (
|
|
149
|
-
columnIndex =
|
|
340
|
+
for (let i = 0; i < cellDataArray.length; i++) {
|
|
341
|
+
columnIndex = this.processHeaderCellData(
|
|
342
|
+
cellDataArray[i],
|
|
343
|
+
headerRow,
|
|
344
|
+
rowSpans,
|
|
345
|
+
columnIndex,
|
|
346
|
+
options.emptyCellReplacement
|
|
347
|
+
);
|
|
150
348
|
}
|
|
151
349
|
this.addRemainingRowSpans(headerRow, rowSpans, columnIndex);
|
|
152
350
|
return headerRow;
|
|
153
351
|
}
|
|
154
|
-
|
|
352
|
+
/**
|
|
353
|
+
* Fetches header cell content and span attributes in parallel.
|
|
354
|
+
* This method performs DOM operations that can be parallelized across multiple cells.
|
|
355
|
+
*/
|
|
356
|
+
static async fetchHeaderCellData(cell, cellContentType) {
|
|
357
|
+
const [text, spanAttributes] = await Promise.all([
|
|
358
|
+
TableUtils.getCellContent(cell, cellContentType),
|
|
359
|
+
TableUtils.parseSpanAttributes(cell)
|
|
360
|
+
]);
|
|
361
|
+
return {
|
|
362
|
+
text,
|
|
363
|
+
rowspan: spanAttributes.rowspan,
|
|
364
|
+
colspan: spanAttributes.colspan
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
/**
|
|
368
|
+
* Processes pre-fetched header cell data and applies it to the header row.
|
|
369
|
+
* This method is purely computational and doesn't perform any DOM operations.
|
|
370
|
+
*/
|
|
371
|
+
static processHeaderCellData(cellData, headerRow, rowSpans, columnIndex, emptyCellReplacement) {
|
|
155
372
|
while (rowSpans.has(columnIndex)) {
|
|
156
373
|
headerRow.push(rowSpans.get(columnIndex));
|
|
157
374
|
rowSpans.delete(columnIndex);
|
|
158
375
|
columnIndex++;
|
|
159
376
|
}
|
|
160
|
-
let text =
|
|
161
|
-
if (!text &&
|
|
162
|
-
text =
|
|
377
|
+
let text = cellData.text;
|
|
378
|
+
if (!text && emptyCellReplacement) {
|
|
379
|
+
text = this.EMPTY_CELL_PLACEHOLDER;
|
|
163
380
|
}
|
|
164
|
-
const { colspan, rowspan } =
|
|
381
|
+
const { colspan, rowspan } = cellData;
|
|
165
382
|
headerRow.push(text);
|
|
166
383
|
this.handleColspan(headerRow, text, colspan);
|
|
167
384
|
this.handleRowspan(rowSpans, text, columnIndex, colspan, rowspan);
|
|
@@ -169,7 +386,7 @@ var TableHeader = class {
|
|
|
169
386
|
}
|
|
170
387
|
static handleColspan(headerRow, text, colspan) {
|
|
171
388
|
for (let i = 1; i < colspan; i++) {
|
|
172
|
-
headerRow.push(`${text}
|
|
389
|
+
headerRow.push(`${text}${this.COLSPAN_SUFFIX_PREFIX}${i}`);
|
|
173
390
|
}
|
|
174
391
|
}
|
|
175
392
|
static handleRowspan(rowSpans, text, columnIndex, colspan, rowspan) {
|
|
@@ -195,20 +412,15 @@ var TableHeader = class {
|
|
|
195
412
|
}
|
|
196
413
|
}
|
|
197
414
|
static removeColspanCells(headerRows) {
|
|
198
|
-
for (
|
|
199
|
-
|
|
200
|
-
if (/__C\d/.test(headerRow[i])) {
|
|
201
|
-
headerRow.splice(i, 1);
|
|
202
|
-
i--;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
415
|
+
for (let i = 0; i < headerRows.length; i++) {
|
|
416
|
+
headerRows[i] = headerRows[i].filter((cell) => !this.COLSPAN_PATTERN.test(cell));
|
|
205
417
|
}
|
|
206
418
|
}
|
|
207
419
|
static removeColspanSuffix(headerRows) {
|
|
208
420
|
for (const headerRow of headerRows) {
|
|
209
421
|
for (let i = 0; i < headerRow.length; i++) {
|
|
210
|
-
if (
|
|
211
|
-
headerRow[i] = headerRow[i].replace(
|
|
422
|
+
if (this.COLSPAN_PATTERN.test(headerRow[i])) {
|
|
423
|
+
headerRow[i] = headerRow[i].replace(this.COLSPAN_PATTERN, "");
|
|
212
424
|
}
|
|
213
425
|
}
|
|
214
426
|
}
|
|
@@ -219,10 +431,10 @@ var TableHeader = class {
|
|
|
219
431
|
const headerCountMap = /* @__PURE__ */ new Map();
|
|
220
432
|
for (let i = 0; i < headerRow.length; i++) {
|
|
221
433
|
const header = headerRow[i];
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
headerRow[i] = `${header}
|
|
225
|
-
headerCountMap.set(header,
|
|
434
|
+
const currentCount = headerCountMap.get(header);
|
|
435
|
+
if (currentCount !== void 0) {
|
|
436
|
+
headerRow[i] = `${header}${this.DUPLICATE_SUFFIX_PREFIX}${currentCount}`;
|
|
437
|
+
headerCountMap.set(header, currentCount + 1);
|
|
226
438
|
} else {
|
|
227
439
|
headerCountMap.set(header, 1);
|
|
228
440
|
}
|
|
@@ -230,78 +442,10 @@ var TableHeader = class {
|
|
|
230
442
|
}
|
|
231
443
|
}
|
|
232
444
|
};
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
static async waitForRows(rowLocator, cellSelector, rowKind, options) {
|
|
238
|
-
const validateCellsWithContent = async () => {
|
|
239
|
-
const cells = await rowLocator.locator(cellSelector).all();
|
|
240
|
-
const cellsWithContent = (await Promise.all(
|
|
241
|
-
cells.map(async (cell) => {
|
|
242
|
-
const content = await cell.textContent();
|
|
243
|
-
return content !== null && content.trim() !== "" ? cell : null;
|
|
244
|
-
})
|
|
245
|
-
)).filter((cell) => cell !== null);
|
|
246
|
-
(0, import_test.expect)(cellsWithContent.length, `No ${rowKind} cells with content found`).toBeGreaterThan(0);
|
|
247
|
-
};
|
|
248
|
-
const validateRowAmount = (rows2, expectedAmount) => {
|
|
249
|
-
(0, import_test.expect)(rows2.length, `Expected ${expectedAmount} ${rowKind} rows, but found ${rows2.length}`).toBe(expectedAmount);
|
|
250
|
-
};
|
|
251
|
-
const validateCellCount = async (rows2, expectedCount) => {
|
|
252
|
-
const rowsWithMatchingCells = (await Promise.all(
|
|
253
|
-
rows2.map(async (row) => {
|
|
254
|
-
const cellCount = await row.locator(cellSelector).count();
|
|
255
|
-
return cellCount === expectedCount ? row : null;
|
|
256
|
-
})
|
|
257
|
-
)).filter((row) => row !== null);
|
|
258
|
-
(0, import_test.expect)(
|
|
259
|
-
rowsWithMatchingCells.length,
|
|
260
|
-
`Expected amount of ${expectedCount} ${rowKind} cells for row not found`
|
|
261
|
-
).toBeGreaterThan(0);
|
|
262
|
-
};
|
|
263
|
-
const validateCellContentCount = async (rows2, expectedContentCount) => {
|
|
264
|
-
const rowsWithMatchingContent = (await Promise.all(
|
|
265
|
-
rows2.map(async (row) => {
|
|
266
|
-
const cells = await row.locator(cellSelector).all();
|
|
267
|
-
const cellsWithContent = (await Promise.all(
|
|
268
|
-
cells.map(async (cell) => {
|
|
269
|
-
const content = await cell.textContent();
|
|
270
|
-
return content !== null && content.trim() !== "" ? cell : null;
|
|
271
|
-
})
|
|
272
|
-
)).filter((cell) => cell !== null);
|
|
273
|
-
return cellsWithContent.length === expectedContentCount ? row : null;
|
|
274
|
-
})
|
|
275
|
-
)).filter((row) => row !== null);
|
|
276
|
-
(0, import_test.expect)(
|
|
277
|
-
rowsWithMatchingContent.length,
|
|
278
|
-
`Expected amount of ${expectedContentCount} ${rowKind} cells with content for row not found`
|
|
279
|
-
).toBeGreaterThan(0);
|
|
280
|
-
};
|
|
281
|
-
const rows = await rowLocator.all();
|
|
282
|
-
if (options == null ? void 0 : options.row) {
|
|
283
|
-
if (options.row.amount) {
|
|
284
|
-
validateRowAmount(rows, options.row.amount);
|
|
285
|
-
} else {
|
|
286
|
-
(0, import_test.expect)(rows.length, `No ${rowKind} rows found`).toBeGreaterThan(0);
|
|
287
|
-
}
|
|
288
|
-
if (options.row.cell) {
|
|
289
|
-
if (options.row.cell.totalCount) {
|
|
290
|
-
await validateCellCount(rows, options.row.cell.totalCount);
|
|
291
|
-
}
|
|
292
|
-
if (options.row.cell.contentCount) {
|
|
293
|
-
await validateCellContentCount(rows, options.row.cell.contentCount);
|
|
294
|
-
} else {
|
|
295
|
-
await validateCellsWithContent();
|
|
296
|
-
}
|
|
297
|
-
} else {
|
|
298
|
-
await validateCellsWithContent();
|
|
299
|
-
}
|
|
300
|
-
} else {
|
|
301
|
-
await validateCellsWithContent();
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
};
|
|
445
|
+
TableHeader.EMPTY_CELL_PLACEHOLDER = "{{Empty}}";
|
|
446
|
+
TableHeader.COLSPAN_SUFFIX_PREFIX = "__C";
|
|
447
|
+
TableHeader.DUPLICATE_SUFFIX_PREFIX = "__D";
|
|
448
|
+
TableHeader.COLSPAN_PATTERN = /__C\d+/;
|
|
305
449
|
|
|
306
450
|
// src/playwright-table.ts
|
|
307
451
|
var PlaywrightTable = class {
|
|
@@ -336,8 +480,6 @@ var PlaywrightTable = class {
|
|
|
336
480
|
constructor(_tableLocator, _options) {
|
|
337
481
|
this._tableLocator = _tableLocator;
|
|
338
482
|
this._options = _options;
|
|
339
|
-
this._headers = [];
|
|
340
|
-
this._rows = [];
|
|
341
483
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
|
|
342
484
|
this._headerRowLocator = this._tableLocator.locator((_c = (_b = (_a = this._options) == null ? void 0 : _a.header) == null ? void 0 : _b.rowSelector) != null ? _c : "thead>tr");
|
|
343
485
|
this._headerColumnSelector = (_f = (_e = (_d = this._options) == null ? void 0 : _d.header) == null ? void 0 : _e.columnSelector) != null ? _f : "th";
|
|
@@ -346,75 +488,145 @@ var PlaywrightTable = class {
|
|
|
346
488
|
}
|
|
347
489
|
/**
|
|
348
490
|
* Retrieves all header rows of the table.
|
|
349
|
-
* @param options - Optional parameters for
|
|
350
|
-
* @param options.timeout - Timeout in milliseconds for loading table data.
|
|
351
|
-
* @param options.headerRowOptions - See {@link HeaderRowOptions} for available options.
|
|
491
|
+
* @param options - Optional parameters for header row parsing configuration.
|
|
352
492
|
* @returns A promise that resolves to an array of header rows.
|
|
493
|
+
* @throws Error if no header rows found after table loads.
|
|
494
|
+
*
|
|
495
|
+
* @example
|
|
496
|
+
* // Get headers with colspan enabled
|
|
497
|
+
* const headers = await table.getHeaderRows({
|
|
498
|
+
* colspan: { enabled: true, suffix: true },
|
|
499
|
+
* duplicateSuffix: true
|
|
500
|
+
* });
|
|
501
|
+
*
|
|
502
|
+
* @see {@link getMainHeaderRow} for getting only the main header row
|
|
503
|
+
* @see {@link HeaderRowOptions} for all available configuration options
|
|
353
504
|
*/
|
|
354
505
|
async getHeaderRows(options) {
|
|
355
|
-
await this.
|
|
356
|
-
|
|
506
|
+
const table = await this.getTable({
|
|
507
|
+
headerRowOptions: options
|
|
508
|
+
});
|
|
509
|
+
return table.headerRows;
|
|
357
510
|
}
|
|
358
511
|
/**
|
|
359
512
|
* Retrieves the main header row of the table.
|
|
360
|
-
*
|
|
361
|
-
* @param options
|
|
362
|
-
* @param options.headerRowOptions - See {@link HeaderRowOptions} for available options.
|
|
513
|
+
* By default, uses the last header row if multiple exist. Can be customized via constructor.
|
|
514
|
+
* @param options - Optional parameters for header row parsing configuration.
|
|
363
515
|
* @returns A promise that resolves to the main header row.
|
|
516
|
+
* @throws Error if no header rows available.
|
|
517
|
+
* @throws Error if setMainHeaderRow index is out of bounds (negative or >= header row count).
|
|
518
|
+
*
|
|
519
|
+
* @example
|
|
520
|
+
* // Get main header with custom index set in constructor
|
|
521
|
+
* const table = new PlaywrightTable(page.locator('table'), {
|
|
522
|
+
* header: { setMainHeaderRow: 0 } // Use first row as main header
|
|
523
|
+
* });
|
|
524
|
+
* const mainHeader = await table.getMainHeaderRow();
|
|
525
|
+
*
|
|
526
|
+
* @see {@link getHeaderRows} for getting all header rows
|
|
364
527
|
*/
|
|
365
528
|
async getMainHeaderRow(options) {
|
|
366
|
-
await this.
|
|
367
|
-
|
|
529
|
+
const table = await this.getTable({
|
|
530
|
+
headerRowOptions: options
|
|
531
|
+
});
|
|
532
|
+
return this.mainHeaderRow(table.headerRows);
|
|
368
533
|
}
|
|
369
534
|
/**
|
|
370
535
|
* Retrieves all body rows of the table.
|
|
371
|
-
* @param options
|
|
372
|
-
* @param options.
|
|
536
|
+
* @param options - Optional configuration for body row parsing.
|
|
537
|
+
* @param options.cellContentType - Content extraction type. See {@link CellContentType}. Defaults to InnerText.
|
|
373
538
|
* @returns A promise that resolves to an array of body rows.
|
|
539
|
+
* @throws Error if no body rows found after table loads.
|
|
540
|
+
*
|
|
541
|
+
* @example
|
|
542
|
+
* // Get body rows with TextContent (includes hidden elements)
|
|
543
|
+
* const rows = await table.getBodyRows({
|
|
544
|
+
* cellContentType: CellContentType.TextContent
|
|
545
|
+
* });
|
|
546
|
+
*
|
|
547
|
+
* @example
|
|
548
|
+
* // Get body rows with default InnerText
|
|
549
|
+
* const rows = await table.getBodyRows();
|
|
374
550
|
*/
|
|
375
551
|
async getBodyRows(options) {
|
|
376
|
-
await this.
|
|
377
|
-
|
|
378
|
-
bodyRowOptions: options == null ? void 0 : options.bodyRowOptions,
|
|
552
|
+
const table = await this.getTable({
|
|
553
|
+
bodyRowOptions: options,
|
|
379
554
|
headerRowOptions: {
|
|
380
555
|
colspan: { enabled: true }
|
|
381
556
|
}
|
|
382
557
|
});
|
|
383
|
-
return
|
|
558
|
+
return table.bodyRows;
|
|
384
559
|
}
|
|
385
560
|
/**
|
|
386
|
-
* Retrieves a specific cell locator in the body of the table.
|
|
561
|
+
* Retrieves a specific cell locator in the body of the table by row and column index.
|
|
387
562
|
* @param rowNumber - The row number (0-based index).
|
|
388
|
-
* @param headerPosition - The header position (0-based index).
|
|
563
|
+
* @param headerPosition - The column/header position (0-based index).
|
|
389
564
|
* @returns A Playwright Locator for the specified cell.
|
|
565
|
+
*
|
|
566
|
+
* @example
|
|
567
|
+
* // Get cell at row 1, column 2
|
|
568
|
+
* const cellLocator = table.getBodyCellLocator(1, 2);
|
|
569
|
+
* await expect(cellLocator).toHaveText('Expected Text');
|
|
570
|
+
*
|
|
571
|
+
* @example
|
|
572
|
+
* // Get first cell in first row
|
|
573
|
+
* const firstCell = table.getBodyCellLocator(0, 0);
|
|
574
|
+
* await firstCell.click();
|
|
575
|
+
*
|
|
576
|
+
* @see {@link getBodyCellLocatorByRowConditions} for finding cells by content matching
|
|
577
|
+
* @see {@link getAllBodyCellLocatorsByHeaderIndex} for getting all cells in a column
|
|
578
|
+
* @see {@link getAllBodyCellLocatorsByHeaderName} for getting all cells under a header
|
|
390
579
|
*/
|
|
391
580
|
getBodyCellLocator(rowNumber, headerPosition) {
|
|
392
581
|
return this._bodyRowLocator.nth(rowNumber).locator(this._bodyRowColumnSelector).nth(headerPosition);
|
|
393
582
|
}
|
|
394
583
|
/**
|
|
395
584
|
* Retrieves a cell locator in the body of the table based on row conditions and a target header.
|
|
396
|
-
*
|
|
397
|
-
* @param
|
|
398
|
-
* @param
|
|
585
|
+
* Finds the first row where all conditions match, then returns the cell in the target column.
|
|
586
|
+
* @param conditions - An object where keys are header names and values are expected cell contents, or a single [header, value] tuple.
|
|
587
|
+
* @param targetHeader - The header name of the column containing the cell to retrieve.
|
|
588
|
+
* @param options - Optional table loading options. See {@link TableOptions}.
|
|
399
589
|
* @returns A promise that resolves to a Playwright Locator for the matching cell.
|
|
400
|
-
* @throws
|
|
590
|
+
* @throws Error if no row matches all specified conditions.
|
|
591
|
+
* @throws Error if any condition header or target header is not found in the table.
|
|
592
|
+
*
|
|
593
|
+
* @example
|
|
594
|
+
* // Find cell with multiple conditions
|
|
595
|
+
* const emailLocator = await table.getBodyCellLocatorByRowConditions(
|
|
596
|
+
* { "First name": "John", "Status": "Active" },
|
|
597
|
+
* "Email"
|
|
598
|
+
* );
|
|
599
|
+
* await expect(emailLocator).toContainText('@example.com');
|
|
600
|
+
*
|
|
601
|
+
* @example
|
|
602
|
+
* // Find cell with single condition using tuple syntax
|
|
603
|
+
* const actionCell = await table.getBodyCellLocatorByRowConditions(
|
|
604
|
+
* ["Username", "john.doe"],
|
|
605
|
+
* "Actions"
|
|
606
|
+
* );
|
|
607
|
+
* await actionCell.locator('button').click();
|
|
608
|
+
*
|
|
609
|
+
* @example
|
|
610
|
+
* // Find cell with single condition using object syntax
|
|
611
|
+
* const statusCell = await table.getBodyCellLocatorByRowConditions(
|
|
612
|
+
* { "Username": "john.doe" },
|
|
613
|
+
* "Status"
|
|
614
|
+
* );
|
|
615
|
+
*
|
|
616
|
+
* @see {@link getBodyCellLocator} for getting cells by direct index
|
|
617
|
+
* @see {@link getAllBodyCellLocatorsByHeaderName} for getting all cells in a column
|
|
401
618
|
*/
|
|
402
619
|
async getBodyCellLocatorByRowConditions(conditions, targetHeader, options) {
|
|
403
620
|
var _a;
|
|
404
|
-
await this.
|
|
405
|
-
const
|
|
406
|
-
const targetHeaderIndex =
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
for (let rowIndex = 0; rowIndex < this._rows.length; rowIndex++) {
|
|
411
|
-
const row = this._rows[rowIndex];
|
|
621
|
+
const table = await this.getTable(options);
|
|
622
|
+
const mainHeader = this.mainHeaderRow(table.headerRows);
|
|
623
|
+
const targetHeaderIndex = this.getHeaderIndex(targetHeader, mainHeader);
|
|
624
|
+
const conditionsObj = Array.isArray(conditions) ? { [conditions[0]]: conditions[1] } : conditions;
|
|
625
|
+
for (let rowIndex = 0; rowIndex < table.bodyRows.length; rowIndex++) {
|
|
626
|
+
const row = table.bodyRows[rowIndex];
|
|
412
627
|
let matches = true;
|
|
413
|
-
for (const [header, value] of Object.entries(
|
|
414
|
-
const headerIndex =
|
|
415
|
-
if (headerIndex === -1) {
|
|
416
|
-
throw new Error(`Header "${header}" not found.`);
|
|
417
|
-
}
|
|
628
|
+
for (const [header, value] of Object.entries(conditionsObj)) {
|
|
629
|
+
const headerIndex = this.getHeaderIndex(header, mainHeader);
|
|
418
630
|
const cellValue = (_a = row[headerIndex]) != null ? _a : "";
|
|
419
631
|
if (cellValue !== value) {
|
|
420
632
|
matches = false;
|
|
@@ -425,24 +637,42 @@ var PlaywrightTable = class {
|
|
|
425
637
|
return this.getBodyCellLocator(rowIndex, targetHeaderIndex);
|
|
426
638
|
}
|
|
427
639
|
}
|
|
428
|
-
throw new Error(
|
|
640
|
+
throw new Error(
|
|
641
|
+
`No row found matching conditions: ${JSON.stringify(conditionsObj)}
|
|
642
|
+
Target header: "${targetHeader}"
|
|
643
|
+
Searched ${table.bodyRows.length} rows
|
|
644
|
+
Body row locator: ${this._bodyRowLocator.toString()}`
|
|
645
|
+
);
|
|
429
646
|
}
|
|
430
647
|
/**
|
|
431
648
|
* Retrieves all cell locators in the body of the table for a specific header name.
|
|
432
|
-
*
|
|
433
|
-
* @param
|
|
434
|
-
* @
|
|
435
|
-
* @
|
|
649
|
+
* Returns locators for every cell in the column identified by the header.
|
|
650
|
+
* @param header - The header name identifying the column.
|
|
651
|
+
* @param options - Optional table loading options. See {@link TableOptions}.
|
|
652
|
+
* @returns A promise that resolves to an array of Playwright Locators for all cells in the column.
|
|
653
|
+
* @throws Error if the header is not found in the table.
|
|
654
|
+
*
|
|
655
|
+
* @example
|
|
656
|
+
* // Get all cells in the "Status" column
|
|
657
|
+
* const statusCells = await table.getAllBodyCellLocatorsByHeaderName("Status");
|
|
658
|
+
* for (const cell of statusCells) {
|
|
659
|
+
* await expect(cell).toHaveText(/Active|Inactive/);
|
|
660
|
+
* }
|
|
661
|
+
*
|
|
662
|
+
* @example
|
|
663
|
+
* // Get text from all cells in "Name" column
|
|
664
|
+
* const nameCells = await table.getAllBodyCellLocatorsByHeaderName("Name");
|
|
665
|
+
* const names = await Promise.all(nameCells.map(cell => cell.textContent()));
|
|
666
|
+
*
|
|
667
|
+
* @see {@link getAllBodyCellLocatorsByHeaderIndex} for getting cells by column index
|
|
668
|
+
* @see {@link getBodyCellLocatorByRowConditions} for finding specific cells by content
|
|
436
669
|
*/
|
|
437
670
|
async getAllBodyCellLocatorsByHeaderName(header, options) {
|
|
438
|
-
await this.
|
|
439
|
-
const headers =
|
|
440
|
-
const headerIndex =
|
|
441
|
-
if (headerIndex === -1) {
|
|
442
|
-
throw new Error(`Header "${header}" not found.`);
|
|
443
|
-
}
|
|
671
|
+
const table = await this.getTable(options);
|
|
672
|
+
const headers = this.mainHeaderRow(table.headerRows);
|
|
673
|
+
const headerIndex = this.getHeaderIndex(header, headers);
|
|
444
674
|
const locators = [];
|
|
445
|
-
for (let rowIndex = 0; rowIndex <
|
|
675
|
+
for (let rowIndex = 0; rowIndex < table.bodyRows.length; rowIndex++) {
|
|
446
676
|
const cellLocator = this.getBodyCellLocator(rowIndex, headerIndex);
|
|
447
677
|
locators.push(cellLocator);
|
|
448
678
|
}
|
|
@@ -450,28 +680,68 @@ var PlaywrightTable = class {
|
|
|
450
680
|
}
|
|
451
681
|
/**
|
|
452
682
|
* Retrieves all cell locators in the body of the table for a specific header index.
|
|
453
|
-
*
|
|
454
|
-
* @param
|
|
455
|
-
* @
|
|
683
|
+
* Returns locators for every cell in the column at the specified position.
|
|
684
|
+
* @param headerIndex - The header/column index (0-based).
|
|
685
|
+
* @param options - Optional table loading options. See {@link TableOptions}.
|
|
686
|
+
* @returns A promise that resolves to an array of Playwright Locators for all cells in the column.
|
|
687
|
+
* @throws Error if the header index is out of bounds (negative or >= column count).
|
|
688
|
+
*
|
|
689
|
+
* @example
|
|
690
|
+
* // Get all cells in the third column (index 2)
|
|
691
|
+
* const thirdColumnCells = await table.getAllBodyCellLocatorsByHeaderIndex(2);
|
|
692
|
+
* for (const cell of thirdColumnCells) {
|
|
693
|
+
* await expect(cell).toBeVisible();
|
|
694
|
+
* }
|
|
695
|
+
*
|
|
696
|
+
* @example
|
|
697
|
+
* // Get all cells in first column
|
|
698
|
+
* const firstColumnCells = await table.getAllBodyCellLocatorsByHeaderIndex(0);
|
|
699
|
+
* const values = await Promise.all(firstColumnCells.map(c => c.textContent()));
|
|
700
|
+
*
|
|
701
|
+
* @see {@link getAllBodyCellLocatorsByHeaderName} for getting cells by header name
|
|
702
|
+
* @see {@link getBodyCellLocator} for getting a specific cell by row and column index
|
|
456
703
|
*/
|
|
457
704
|
async getAllBodyCellLocatorsByHeaderIndex(headerIndex, options) {
|
|
458
|
-
await this.
|
|
705
|
+
const table = await this.getTable(options);
|
|
706
|
+
const columnCount = this.mainHeaderRow(table.headerRows).length;
|
|
707
|
+
if (headerIndex < 0 || headerIndex >= columnCount) {
|
|
708
|
+
throw new Error(
|
|
709
|
+
`Header index ${headerIndex} out of bounds. Table has ${columnCount} columns.
|
|
710
|
+
Header row locator: ${this._headerRowLocator.toString()}`
|
|
711
|
+
);
|
|
712
|
+
}
|
|
459
713
|
const locators = [];
|
|
460
|
-
for (let rowIndex = 0; rowIndex <
|
|
714
|
+
for (let rowIndex = 0; rowIndex < table.bodyRows.length; rowIndex++) {
|
|
461
715
|
const cellLocator = this.getBodyCellLocator(rowIndex, headerIndex);
|
|
462
716
|
locators.push(cellLocator);
|
|
463
717
|
}
|
|
464
718
|
return locators;
|
|
465
719
|
}
|
|
466
720
|
/**
|
|
467
|
-
* Converts the table data into a JSON object.
|
|
468
|
-
*
|
|
469
|
-
* @
|
|
721
|
+
* Converts the table data into a JSON array where each object represents a row.
|
|
722
|
+
* Keys are header names, values are cell contents.
|
|
723
|
+
* @param options - Optional table loading options. See {@link TableOptions}.
|
|
724
|
+
* @returns A promise that resolves to an array of objects representing table rows.
|
|
725
|
+
* @throws Error if no header rows or body rows found.
|
|
726
|
+
*
|
|
727
|
+
* @example
|
|
728
|
+
* // Convert table to JSON with default options
|
|
729
|
+
* const data = await table.getJson();
|
|
730
|
+
* // Result: [{"Name": "John", "Age": "30"}, {"Name": "Jane", "Age": "25"}]
|
|
731
|
+
*
|
|
732
|
+
* @example
|
|
733
|
+
* // Convert with colspan enabled and duplicate suffix
|
|
734
|
+
* const data = await table.getJson({
|
|
735
|
+
* headerRowOptions: {
|
|
736
|
+
* colspan: { enabled: true, suffix: true },
|
|
737
|
+
* duplicateSuffix: true,
|
|
738
|
+
* emptyCellReplacement: true
|
|
739
|
+
* }
|
|
740
|
+
* });
|
|
470
741
|
*/
|
|
471
742
|
async getJson(options) {
|
|
472
743
|
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
473
|
-
await this.
|
|
474
|
-
timeout: options == null ? void 0 : options.timeout,
|
|
744
|
+
const table = await this.getTable({
|
|
475
745
|
bodyRowOptions: options == null ? void 0 : options.bodyRowOptions,
|
|
476
746
|
headerRowOptions: {
|
|
477
747
|
colspan: {
|
|
@@ -482,8 +752,8 @@ var PlaywrightTable = class {
|
|
|
482
752
|
emptyCellReplacement: (_j = (_i = options == null ? void 0 : options.headerRowOptions) == null ? void 0 : _i.emptyCellReplacement) != null ? _j : true
|
|
483
753
|
}
|
|
484
754
|
});
|
|
485
|
-
const headers = this.mainHeaderRow();
|
|
486
|
-
return
|
|
755
|
+
const headers = this.mainHeaderRow(table.headerRows);
|
|
756
|
+
return table.bodyRows.map((row) => {
|
|
487
757
|
const rowObj = {};
|
|
488
758
|
headers.forEach((header, index) => {
|
|
489
759
|
rowObj[header] = row[index] || "";
|
|
@@ -492,44 +762,289 @@ var PlaywrightTable = class {
|
|
|
492
762
|
});
|
|
493
763
|
}
|
|
494
764
|
/**
|
|
495
|
-
* Waits for the
|
|
496
|
-
* @param options -
|
|
497
|
-
* @returns A promise that resolves when
|
|
765
|
+
* Waits for the table body to be empty (no body rows).
|
|
766
|
+
* @param options - Polling options (timeout, interval, retries).
|
|
767
|
+
* @returns A promise that resolves when table body is empty.
|
|
768
|
+
* @throws Error if table is not empty within timeout period.
|
|
769
|
+
*
|
|
770
|
+
* @example
|
|
771
|
+
* // Wait for table to clear after delete operation
|
|
772
|
+
* await table.waitForEmpty({ timeout: 5000 });
|
|
773
|
+
*
|
|
774
|
+
* @see {@link waitForNonEmpty} for waiting until table has data
|
|
775
|
+
*
|
|
776
|
+
* @remarks
|
|
777
|
+
* **Alternative:** Consider using Playwright's `expect.poll` for more flexibility:
|
|
778
|
+
* ```ts
|
|
779
|
+
* await expect.poll(async () => (await table.getBodyRows()).length).toBe(0);
|
|
780
|
+
* ```
|
|
498
781
|
*/
|
|
499
|
-
async
|
|
500
|
-
await (
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
782
|
+
async waitForEmpty(options) {
|
|
783
|
+
await pollTable(async () => {
|
|
784
|
+
const rows = await this._bodyRowLocator.all();
|
|
785
|
+
if (rows.length > 0) {
|
|
786
|
+
throw new Error(
|
|
787
|
+
`Expected table to be empty, but found ${rows.length} body rows.
|
|
788
|
+
Body row locator: ${this._bodyRowLocator.toString()}`
|
|
789
|
+
);
|
|
790
|
+
}
|
|
791
|
+
}, options);
|
|
792
|
+
}
|
|
793
|
+
/**
|
|
794
|
+
* Waits for the table body to be non-empty (at least one body row with content).
|
|
795
|
+
* A row is considered valid if it has at least one cell containing text.
|
|
796
|
+
* @param options - Polling options (timeout, interval, retries).
|
|
797
|
+
* @returns A promise that resolves when table has at least one body row with content.
|
|
798
|
+
* @throws Error if table remains empty or has no rows with content within timeout period.
|
|
799
|
+
*
|
|
800
|
+
* @example
|
|
801
|
+
* // Wait for table to populate after search
|
|
802
|
+
* await table.waitForNonEmpty({ timeout: 5000 });
|
|
803
|
+
*
|
|
804
|
+
* @see {@link waitForEmpty} for waiting until table is cleared
|
|
805
|
+
*
|
|
806
|
+
* @remarks
|
|
807
|
+
* **Alternative:** Consider using Playwright's `expect.poll` for more flexibility:
|
|
808
|
+
* ```ts
|
|
809
|
+
* await expect.poll(async () => (await table.getBodyRows()).length).toBeGreaterThan(0);
|
|
810
|
+
* ```
|
|
811
|
+
*/
|
|
812
|
+
async waitForNonEmpty(options) {
|
|
813
|
+
await pollTable(async () => {
|
|
814
|
+
const rows = await this._bodyRowLocator.all();
|
|
815
|
+
if (rows.length === 0) {
|
|
816
|
+
throw new Error(
|
|
817
|
+
`Expected table to be non-empty, but found 0 body rows.
|
|
818
|
+
Body row locator: ${this._bodyRowLocator.toString()}`
|
|
819
|
+
);
|
|
820
|
+
}
|
|
821
|
+
let hasRowWithContent = false;
|
|
822
|
+
for (const row of rows) {
|
|
823
|
+
const cells = await row.locator(this._bodyRowColumnSelector).all();
|
|
824
|
+
for (const cell of cells) {
|
|
825
|
+
const content = await cell.innerText();
|
|
826
|
+
if (content.trim().length > 0) {
|
|
827
|
+
hasRowWithContent = true;
|
|
828
|
+
break;
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
if (hasRowWithContent) break;
|
|
832
|
+
}
|
|
833
|
+
if (!hasRowWithContent) {
|
|
834
|
+
throw new Error(
|
|
835
|
+
`Expected table to have at least one row with content, but all ${rows.length} rows are empty.
|
|
836
|
+
Body row locator: ${this._bodyRowLocator.toString()}`
|
|
837
|
+
);
|
|
838
|
+
}
|
|
839
|
+
}, options);
|
|
840
|
+
}
|
|
841
|
+
/**
|
|
842
|
+
* Waits for the table body to have exactly N rows.
|
|
843
|
+
* @param count - The exact number of body rows expected.
|
|
844
|
+
* @param options - Polling options (timeout, interval, retries).
|
|
845
|
+
* @returns A promise that resolves when the table has exactly the specified number of rows.
|
|
846
|
+
* @throws Error if row count doesn't match within timeout period.
|
|
847
|
+
*
|
|
848
|
+
* @example
|
|
849
|
+
* // Wait for exactly 10 rows after pagination
|
|
850
|
+
* await table.waitForExactRowCount(10, { timeout: 5000 });
|
|
851
|
+
*
|
|
852
|
+
* @example
|
|
853
|
+
* // Verify table has exactly 0 rows (alternative to waitForEmpty)
|
|
854
|
+
* await table.waitForExactRowCount(0);
|
|
855
|
+
*
|
|
856
|
+
* @see {@link getRowCount} for getting current row counts
|
|
857
|
+
*
|
|
858
|
+
* @remarks
|
|
859
|
+
* **Alternative:** Consider using Playwright's `expect.poll` for more flexibility:
|
|
860
|
+
* ```ts
|
|
861
|
+
* await expect.poll(async () => (await table.getBodyRows()).length).toBe(10);
|
|
862
|
+
* ```
|
|
863
|
+
*/
|
|
864
|
+
async waitForExactRowCount(count, options) {
|
|
865
|
+
if (count < 0 || !Number.isInteger(count)) {
|
|
866
|
+
throw new Error(`Row count must be a non-negative integer, got: ${count}`);
|
|
867
|
+
}
|
|
868
|
+
await pollTable(async () => {
|
|
869
|
+
const rows = await this._bodyRowLocator.all();
|
|
870
|
+
if (rows.length !== count) {
|
|
871
|
+
throw new Error(
|
|
872
|
+
`Expected exactly ${count} rows, but found ${rows.length}.
|
|
873
|
+
Body row locator: ${this._bodyRowLocator.toString()}`
|
|
874
|
+
);
|
|
875
|
+
}
|
|
876
|
+
}, options);
|
|
877
|
+
}
|
|
878
|
+
/**
|
|
879
|
+
* Gets the count of header and body rows in the table.
|
|
880
|
+
* Lightweight method that doesn't fetch cell data, only counts rows.
|
|
881
|
+
* @returns A promise that resolves to an object with header and body row counts.
|
|
882
|
+
*
|
|
883
|
+
* @example
|
|
884
|
+
* // Get current row counts
|
|
885
|
+
* const counts = await table.getRowCount();
|
|
886
|
+
* console.log(`Headers: ${counts.header}, Body: ${counts.body}`);
|
|
887
|
+
*
|
|
888
|
+
* @example
|
|
889
|
+
* // Assert specific row count
|
|
890
|
+
* const { body } = await table.getRowCount();
|
|
891
|
+
* expect(body).toBe(10);
|
|
892
|
+
*/
|
|
893
|
+
async getRowCount() {
|
|
894
|
+
const [headerRows, bodyRows] = await Promise.all([this._headerRowLocator.all(), this._bodyRowLocator.all()]);
|
|
895
|
+
return {
|
|
896
|
+
header: headerRows.length,
|
|
897
|
+
body: bodyRows.length
|
|
898
|
+
};
|
|
899
|
+
}
|
|
900
|
+
/**
|
|
901
|
+
* Gets all distinct (unique) values from a specific column.
|
|
902
|
+
* Returns a sorted array of unique string values, excluding empty values.
|
|
903
|
+
* @param headerName - The header name of the column to extract distinct values from.
|
|
904
|
+
* @param options - Optional table loading options. See {@link TableOptions}.
|
|
905
|
+
* @returns A promise that resolves to a sorted array of distinct values.
|
|
906
|
+
* @throws Error if the specified header is not found in the table.
|
|
907
|
+
*
|
|
908
|
+
* @example
|
|
909
|
+
* // Get all unique status values
|
|
910
|
+
* const statuses = await table.getDistinctColumnValues("Status");
|
|
911
|
+
* // Result: ["Active", "Inactive", "Pending"]
|
|
912
|
+
*
|
|
913
|
+
* @example
|
|
914
|
+
* // Get distinct countries for filtering
|
|
915
|
+
* const countries = await table.getDistinctColumnValues("Country");
|
|
916
|
+
* console.log(`Available countries: ${countries.join(", ")}`);
|
|
917
|
+
*
|
|
918
|
+
* @example
|
|
919
|
+
* // Verify expected values exist
|
|
920
|
+
* const roles = await table.getDistinctColumnValues("Role");
|
|
921
|
+
* expect(roles).toContain("Admin");
|
|
922
|
+
* expect(roles).toHaveLength(3);
|
|
923
|
+
*
|
|
924
|
+
* @see {@link getAllBodyCellLocatorsByHeaderName} for getting all cell locators in a column
|
|
925
|
+
* @see {@link getJson} for getting full table data
|
|
926
|
+
*/
|
|
927
|
+
async getDistinctColumnValues(headerName, options) {
|
|
928
|
+
var _a;
|
|
929
|
+
const table = await this.getTable(options);
|
|
930
|
+
const mainHeader = this.mainHeaderRow(table.headerRows);
|
|
931
|
+
const headerIndex = this.getHeaderIndex(headerName, mainHeader);
|
|
932
|
+
const values = /* @__PURE__ */ new Set();
|
|
933
|
+
for (const row of table.bodyRows) {
|
|
934
|
+
const value = String((_a = row[headerIndex]) != null ? _a : "").trim();
|
|
935
|
+
if (value) values.add(value);
|
|
936
|
+
}
|
|
937
|
+
return Array.from(values).sort();
|
|
505
938
|
}
|
|
506
939
|
/**
|
|
507
|
-
*
|
|
508
|
-
*
|
|
509
|
-
* @
|
|
940
|
+
* Finds the index of the first body row matching the specified conditions.
|
|
941
|
+
* Returns -1 if no matching row is found.
|
|
942
|
+
* @param conditions - Record of header names and expected cell values to match.
|
|
943
|
+
* @param options - Optional table loading options. See {@link TableOptions}.
|
|
944
|
+
* @returns A promise that resolves to the 0-based index of the first matching row, or -1 if not found.
|
|
945
|
+
* @throws Error if specified headers don't exist in the table.
|
|
946
|
+
*
|
|
947
|
+
* @example
|
|
948
|
+
* // Find index of row where Status is "Active"
|
|
949
|
+
* const index = await table.findRowIndex({ Status: "Active" });
|
|
950
|
+
* if (index >= 0) {
|
|
951
|
+
* console.log(`Found at row ${index}`);
|
|
952
|
+
* }
|
|
953
|
+
*
|
|
954
|
+
* @example
|
|
955
|
+
* // Find row and interact with it
|
|
956
|
+
* const rowIndex = await table.findRowIndex({ Username: "john.doe" });
|
|
957
|
+
* if (rowIndex >= 0) {
|
|
958
|
+
* const cell = table.getBodyCellLocator(rowIndex, 0);
|
|
959
|
+
* await cell.click();
|
|
960
|
+
* }
|
|
961
|
+
*
|
|
962
|
+
* @see {@link getBodyCellLocatorByRowConditions} for getting cell locators from matching rows
|
|
963
|
+
* @see {@link waitForRowByConditions} for waiting until matching row appears
|
|
510
964
|
*/
|
|
511
|
-
async
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
965
|
+
async findRowIndex(conditions, options) {
|
|
966
|
+
var _a;
|
|
967
|
+
const table = await this.getTable({
|
|
968
|
+
headerRowOptions: { colspan: { enabled: true }, ...options == null ? void 0 : options.headerRowOptions },
|
|
969
|
+
bodyRowOptions: options == null ? void 0 : options.bodyRowOptions
|
|
516
970
|
});
|
|
971
|
+
const mainHeader = this.mainHeaderRow(table.headerRows);
|
|
972
|
+
const headerIndices = /* @__PURE__ */ new Map();
|
|
973
|
+
for (const header of Object.keys(conditions)) {
|
|
974
|
+
headerIndices.set(header, this.getHeaderIndex(header, mainHeader));
|
|
975
|
+
}
|
|
976
|
+
for (let rowIndex = 0; rowIndex < table.bodyRows.length; rowIndex++) {
|
|
977
|
+
const row = table.bodyRows[rowIndex];
|
|
978
|
+
let rowMatches = true;
|
|
979
|
+
for (const [header, expectedValue] of Object.entries(conditions)) {
|
|
980
|
+
const headerIndex = headerIndices.get(header);
|
|
981
|
+
const cellValue = String((_a = row[headerIndex]) != null ? _a : "");
|
|
982
|
+
if (cellValue !== expectedValue) {
|
|
983
|
+
rowMatches = false;
|
|
984
|
+
break;
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
if (rowMatches) {
|
|
988
|
+
return rowIndex;
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
return -1;
|
|
517
992
|
}
|
|
518
|
-
async
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
993
|
+
async getTable(options) {
|
|
994
|
+
const [headerRows, bodyRows] = await Promise.all([
|
|
995
|
+
TableHeader.getRows(this._headerRowLocator, this._headerColumnSelector, options == null ? void 0 : options.headerRowOptions),
|
|
996
|
+
TableBody.getRows(this._bodyRowLocator, this._bodyRowColumnSelector, options == null ? void 0 : options.bodyRowOptions)
|
|
997
|
+
]);
|
|
998
|
+
if (headerRows.length === 0) {
|
|
999
|
+
throw new Error(
|
|
1000
|
+
`No header rows found, is table loaded completely?
|
|
1001
|
+
Header row locator: ${this._headerRowLocator.toString()}
|
|
1002
|
+
Table locator: ${this._tableLocator.toString()}`
|
|
1003
|
+
);
|
|
1004
|
+
}
|
|
1005
|
+
if (bodyRows.length === 0) {
|
|
1006
|
+
throw new Error(
|
|
1007
|
+
`No body rows found, is table loaded completely?
|
|
1008
|
+
Body row locator: ${this._bodyRowLocator.toString()}
|
|
1009
|
+
Table locator: ${this._tableLocator.toString()}`
|
|
1010
|
+
);
|
|
1011
|
+
}
|
|
1012
|
+
return { headerRows, bodyRows };
|
|
529
1013
|
}
|
|
530
|
-
mainHeaderRow() {
|
|
531
|
-
var _a, _b;
|
|
532
|
-
|
|
1014
|
+
mainHeaderRow(headers) {
|
|
1015
|
+
var _a, _b, _c;
|
|
1016
|
+
if (headers.length === 0) {
|
|
1017
|
+
throw new Error(
|
|
1018
|
+
`No header rows available. Ensure table has been loaded.
|
|
1019
|
+
Header row locator: ${this._headerRowLocator.toString()}`
|
|
1020
|
+
);
|
|
1021
|
+
}
|
|
1022
|
+
const headerIndex = (_c = (_b = (_a = this._options) == null ? void 0 : _a.header) == null ? void 0 : _b.setMainHeaderRow) != null ? _c : headers.length - 1;
|
|
1023
|
+
if (headerIndex < 0 || headerIndex >= headers.length) {
|
|
1024
|
+
throw new Error(
|
|
1025
|
+
`Header row index ${headerIndex} out of bounds. Table has ${headers.length} header rows.
|
|
1026
|
+
Header row locator: ${this._headerRowLocator.toString()}`
|
|
1027
|
+
);
|
|
1028
|
+
}
|
|
1029
|
+
return headers[headerIndex];
|
|
1030
|
+
}
|
|
1031
|
+
/**
|
|
1032
|
+
* Gets the index of a header in the main header row, throwing an error if not found.
|
|
1033
|
+
* @param header - The header name to find.
|
|
1034
|
+
* @param mainHeader - The main header row to search in.
|
|
1035
|
+
* @returns The 0-based index of the header.
|
|
1036
|
+
* @throws Error if the header is not found.
|
|
1037
|
+
*/
|
|
1038
|
+
getHeaderIndex(header, mainHeader) {
|
|
1039
|
+
const index = mainHeader.indexOf(header);
|
|
1040
|
+
if (index === -1) {
|
|
1041
|
+
throw new Error(
|
|
1042
|
+
`Header "${header}" not found.
|
|
1043
|
+
Available headers: [${mainHeader.join(", ")}]
|
|
1044
|
+
Header row locator: ${this._headerRowLocator.toString()}`
|
|
1045
|
+
);
|
|
1046
|
+
}
|
|
1047
|
+
return index;
|
|
533
1048
|
}
|
|
534
1049
|
};
|
|
535
1050
|
// Annotate the CommonJS export names for ESM import in node:
|