@cerios/playwright-table 1.1.3 → 2.0.1

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/dist/index.d.mts CHANGED
@@ -1,5 +1,21 @@
1
1
  import { Locator } from '@playwright/test';
2
2
 
3
+ /**
4
+ * Enum representing the possible types of content that can be extracted from a table cell.
5
+ *
6
+ * - `TextContent`: The raw text content of the cell, including hidden elements and extra whitespace.
7
+ * - `InnerText`: The rendered text content of the cell, as seen by the user, with whitespace collapsed and hidden elements excluded.
8
+ */
9
+ declare enum CellContentType {
10
+ TextContent = "textContent",
11
+ InnerText = "innerText"
12
+ }
13
+
14
+ type PollingOptions = {
15
+ timeout?: number;
16
+ interval?: number;
17
+ };
18
+
3
19
  /**
4
20
  * Represents a single cell value within a table row.
5
21
  *
@@ -27,17 +43,6 @@ type BodyRow = Cell[];
27
43
  */
28
44
  type HeaderRow = Cell[];
29
45
 
30
- /**
31
- * Enum representing the possible types of content that can be extracted from a table cell.
32
- *
33
- * - `TextContent`: The raw text content of the cell, including hidden elements and extra whitespace.
34
- * - `InnerText`: The rendered text content of the cell, as seen by the user, with whitespace collapsed and hidden elements excluded.
35
- */
36
- declare enum CellContentType {
37
- TextContent = "textContent",
38
- InnerText = "innerText"
39
- }
40
-
41
46
  /**
42
47
  * Options for configuring a table header row.
43
48
  *
@@ -56,15 +61,16 @@ type HeaderRowOptions = {
56
61
  enabled?: boolean;
57
62
  suffix?: boolean;
58
63
  };
64
+ normalizeWhitespace?: boolean;
65
+ stripIconGlyphs?: boolean;
59
66
  };
60
67
 
61
68
  /**
62
69
  * Options for loading table data, headers, and rows.
63
70
  *
64
71
  * @example
65
- * // Wait up to 5 seconds and use custom cell content type for body rows
66
- * const options: LoadOptions = {
67
- * timeout: 5000,
72
+ * // Use custom cell content type for body rows and header options
73
+ * const options: TableOptions = {
68
74
  * bodyRowOptions: { cellContentType: CellContentType.TextContent },
69
75
  * headerRowOptions: {
70
76
  * colspan: { enabled: true, suffix: true },
@@ -74,47 +80,15 @@ type HeaderRowOptions = {
74
80
  * };
75
81
  * await table.getBodyRows(options);
76
82
  */
77
- type LoadOptions = {
78
- /** Timeout in milliseconds for loading table data. */
79
- timeout?: number;
83
+ type TableOptions = {
80
84
  /** See {@link HeaderRowOptions} for available options. */
81
85
  headerRowOptions?: HeaderRowOptions;
82
86
  /** Options for parsing body rows. */
83
87
  bodyRowOptions?: {
84
88
  cellContentType?: CellContentType;
85
89
  };
86
- };
87
- /**
88
- * Options for waiting for table rows to appear or meet certain conditions.
89
- *
90
- * @example
91
- * // Wait for at least 3 body rows, each with 3 cells containing content, within 2 seconds
92
- * const waitOptions: WaitForTableRowsOptions = {
93
- * timeout: 2000,
94
- * row: {
95
- * amount: 3,
96
- * cell: {
97
- * totalCount: 3,
98
- * contentCount: 3,
99
- * },
100
- * },
101
- * };
102
- * await table.waitForBodyRows(waitOptions);
103
- */
104
- type WaitForTableRowsOptions = {
105
- /** Timeout in milliseconds for waiting. */
106
- timeout?: number;
107
- /** Row and cell count/content conditions. */
108
- row?: {
109
- /** Expected number of rows. */
110
- amount?: number;
111
- cell?: {
112
- /** Expected number of cells per row. */
113
- totalCount?: number;
114
- /** Expected number of cells with content per row. */
115
- contentCount?: number;
116
- };
117
- };
90
+ /** @deprecated Use `bodyRowOptions.cellContentType` instead. */
91
+ cellContentType?: CellContentType;
118
92
  };
119
93
  /**
120
94
  * Represents a table in Playwright, providing methods to interact with its headers, rows, and cells.
@@ -122,8 +96,6 @@ type WaitForTableRowsOptions = {
122
96
  declare class PlaywrightTable {
123
97
  private readonly _tableLocator;
124
98
  private readonly _options?;
125
- private _headers;
126
- private _rows;
127
99
  private _bodyRowLocator;
128
100
  private _bodyRowColumnSelector;
129
101
  private _headerRowLocator;
@@ -169,78 +141,344 @@ declare class PlaywrightTable {
169
141
  } | undefined);
170
142
  /**
171
143
  * Retrieves all header rows of the table.
172
- * @param options - Optional parameters for timeout and header row options.
173
- * @param options.timeout - Timeout in milliseconds for loading table data.
174
- * @param options.headerRowOptions - See {@link HeaderRowOptions} for available options.
144
+ * @param options - Optional parameters for header row parsing configuration.
175
145
  * @returns A promise that resolves to an array of header rows.
146
+ * @throws Error if no header rows found after table loads.
147
+ *
148
+ * @example
149
+ * // Get headers with colspan enabled
150
+ * const headers = await table.getHeaderRows({
151
+ * colspan: { enabled: true, suffix: true },
152
+ * duplicateSuffix: true
153
+ * });
154
+ *
155
+ * @see {@link getMainHeaderRow} for getting only the main header row
156
+ * @see {@link HeaderRowOptions} for all available configuration options
176
157
  */
177
- getHeaderRows(options?: Pick<LoadOptions, "timeout" | "headerRowOptions">): Promise<HeaderRow[]>;
158
+ getHeaderRows(options?: HeaderRowOptions): Promise<HeaderRow[]>;
178
159
  /**
179
160
  * Retrieves the main header row of the table.
180
- * @param options - Optional parameters for timeout and header row options.
181
- * @param options.timeout - Timeout in milliseconds for loading table data.
182
- * @param options.headerRowOptions - See {@link HeaderRowOptions} for available options.
161
+ * By default, uses the last header row if multiple exist. Can be customized via constructor.
162
+ * @param options - Optional parameters for header row parsing configuration.
183
163
  * @returns A promise that resolves to the main header row.
164
+ * @throws Error if no header rows available.
165
+ * @throws Error if setMainHeaderRow index is out of bounds (negative or >= header row count).
166
+ *
167
+ * @example
168
+ * // Get main header with custom index set in constructor
169
+ * const table = new PlaywrightTable(page.locator('table'), {
170
+ * header: { setMainHeaderRow: 0 } // Use first row as main header
171
+ * });
172
+ * const mainHeader = await table.getMainHeaderRow();
173
+ *
174
+ * @see {@link getHeaderRows} for getting all header rows
184
175
  */
185
- getMainHeaderRow(options?: Pick<LoadOptions, "timeout" | "headerRowOptions">): Promise<HeaderRow>;
176
+ getMainHeaderRow(options?: HeaderRowOptions): Promise<HeaderRow>;
186
177
  /**
187
178
  * Retrieves all body rows of the table.
188
- * @param options.timout - Timeout in milliseconds for loading table data.
189
- * @param options.bodyRowOptions.cellContentType - The content type for the body rows.
179
+ * @param options - Optional table or body-row configuration.
180
+ * @param options.cellContentType - Legacy shortcut for body row content extraction. See {@link CellContentType}.
181
+ * @param options.bodyRowOptions - Body row parsing options.
182
+ * @param options.headerRowOptions - Header parsing options used when resolving row/colspan layouts.
190
183
  * @returns A promise that resolves to an array of body rows.
184
+ * @throws Error if no body rows found after table loads.
185
+ *
186
+ * @example
187
+ * // Get body rows with TextContent (includes hidden elements)
188
+ * const rows = await table.getBodyRows({
189
+ * cellContentType: CellContentType.TextContent
190
+ * });
191
+ *
192
+ * @example
193
+ * // Get body rows with default InnerText
194
+ * const rows = await table.getBodyRows();
195
+ *
196
+ * @example
197
+ * // Use full table options (for example to sanitize noisy treegrid headers)
198
+ * const rows = await table.getBodyRows({
199
+ * bodyRowOptions: { cellContentType: CellContentType.InnerText },
200
+ * headerRowOptions: { normalizeWhitespace: true, stripIconGlyphs: true }
201
+ * });
191
202
  */
192
- getBodyRows(options?: Pick<LoadOptions, "timeout" | "bodyRowOptions">): Promise<BodyRow[]>;
203
+ getBodyRows(options?: TableOptions): Promise<BodyRow[]>;
193
204
  /**
194
- * Retrieves a specific cell locator in the body of the table.
205
+ * Retrieves a specific cell locator in the body of the table by row and column index.
195
206
  * @param rowNumber - The row number (0-based index).
196
- * @param headerPosition - The header position (0-based index).
207
+ * @param headerPosition - The column/header position (0-based index).
197
208
  * @returns A Playwright Locator for the specified cell.
209
+ *
210
+ * @example
211
+ * // Get cell at row 1, column 2
212
+ * const cellLocator = table.getBodyCellLocator(1, 2);
213
+ * await expect(cellLocator).toHaveText('Expected Text');
214
+ *
215
+ * @example
216
+ * // Get first cell in first row
217
+ * const firstCell = table.getBodyCellLocator(0, 0);
218
+ * await firstCell.click();
219
+ *
220
+ * @see {@link getBodyCellLocatorByRowConditions} for finding cells by content matching
221
+ * @see {@link getAllBodyCellLocatorsByHeaderIndex} for getting all cells in a column
222
+ * @see {@link getAllBodyCellLocatorsByHeaderName} for getting all cells under a header
198
223
  */
199
224
  getBodyCellLocator(rowNumber: number, headerPosition: number): Locator;
200
225
  /**
201
226
  * Retrieves a cell locator in the body of the table based on row conditions and a target header.
202
- * @param conditions - A record of header names and their expected values.
203
- * @param targetHeader - The target header name for the desired cell.
204
- * @param options - See {@link LoadOptions} for available options.
227
+ * Finds the first row where all conditions match, then returns the cell in the target column.
228
+ * @param conditions - An object where keys are header names and values are expected cell contents, or a single [header, value] tuple.
229
+ * @param targetHeader - The header name of the column containing the cell to retrieve.
230
+ * @param options - Optional table loading options. See {@link TableOptions}.
205
231
  * @returns A promise that resolves to a Playwright Locator for the matching cell.
206
- * @throws An error if no matching row is found or if a header is not found.
232
+ * @throws Error if no row matches all specified conditions.
233
+ * @throws Error if any condition header or target header is not found in the table.
234
+ *
235
+ * @example
236
+ * // Find cell with multiple conditions
237
+ * const emailLocator = await table.getBodyCellLocatorByRowConditions(
238
+ * { "First name": "John", "Status": "Active" },
239
+ * "Email"
240
+ * );
241
+ * await expect(emailLocator).toContainText('@example.com');
242
+ *
243
+ * @example
244
+ * // Find cell with single condition using tuple syntax
245
+ * const actionCell = await table.getBodyCellLocatorByRowConditions(
246
+ * ["Username", "john.doe"],
247
+ * "Actions"
248
+ * );
249
+ * await actionCell.locator('button').click();
250
+ *
251
+ * @example
252
+ * // Find cell with single condition using object syntax
253
+ * const statusCell = await table.getBodyCellLocatorByRowConditions(
254
+ * { "Username": "john.doe" },
255
+ * "Status"
256
+ * );
257
+ *
258
+ * @see {@link getBodyCellLocator} for getting cells by direct index
259
+ * @see {@link getAllBodyCellLocatorsByHeaderName} for getting all cells in a column
207
260
  */
208
- getBodyCellLocatorByRowConditions(conditions: Record<string, string>, targetHeader: string, options?: LoadOptions): Promise<Locator>;
261
+ getBodyCellLocatorByRowConditions(conditions: Record<string, string> | [string, string], targetHeader: string, options?: TableOptions): Promise<Locator>;
209
262
  /**
210
263
  * Retrieves all cell locators in the body of the table for a specific header name.
211
- * @param header - The header name.
212
- * @param options - See {@link LoadOptions} for available options.
213
- * @returns A promise that resolves to an array of Playwright Locators for the cells.
214
- * @throws An error if the header is not found.
264
+ * Returns locators for every cell in the column identified by the header.
265
+ * @param header - The header name identifying the column.
266
+ * @param options - Optional table loading options. See {@link TableOptions}.
267
+ * @returns A promise that resolves to an array of Playwright Locators for all cells in the column.
268
+ * @throws Error if the header is not found in the table.
269
+ *
270
+ * @example
271
+ * // Get all cells in the "Status" column
272
+ * const statusCells = await table.getAllBodyCellLocatorsByHeaderName("Status");
273
+ * for (const cell of statusCells) {
274
+ * await expect(cell).toHaveText(/Active|Inactive/);
275
+ * }
276
+ *
277
+ * @example
278
+ * // Get text from all cells in "Name" column
279
+ * const nameCells = await table.getAllBodyCellLocatorsByHeaderName("Name");
280
+ * const names = await Promise.all(nameCells.map(cell => cell.textContent()));
281
+ *
282
+ * @see {@link getAllBodyCellLocatorsByHeaderIndex} for getting cells by column index
283
+ * @see {@link getBodyCellLocatorByRowConditions} for finding specific cells by content
215
284
  */
216
- getAllBodyCellLocatorsByHeaderName(header: string, options?: LoadOptions): Promise<Locator[]>;
285
+ getAllBodyCellLocatorsByHeaderName(header: string, options?: TableOptions): Promise<Locator[]>;
217
286
  /**
218
287
  * Retrieves all cell locators in the body of the table for a specific header index.
219
- * @param headerIndex - The header index (0-based).
220
- * @param options - See {@link LoadOptions} for available options.
221
- * @returns A promise that resolves to an array of Playwright Locators for the cells.
288
+ * Returns locators for every cell in the column at the specified position.
289
+ * @param headerIndex - The header/column index (0-based).
290
+ * @param options - Optional table loading options. See {@link TableOptions}.
291
+ * @returns A promise that resolves to an array of Playwright Locators for all cells in the column.
292
+ * @throws Error if the header index is out of bounds (negative or >= column count).
293
+ *
294
+ * @example
295
+ * // Get all cells in the third column (index 2)
296
+ * const thirdColumnCells = await table.getAllBodyCellLocatorsByHeaderIndex(2);
297
+ * for (const cell of thirdColumnCells) {
298
+ * await expect(cell).toBeVisible();
299
+ * }
300
+ *
301
+ * @example
302
+ * // Get all cells in first column
303
+ * const firstColumnCells = await table.getAllBodyCellLocatorsByHeaderIndex(0);
304
+ * const values = await Promise.all(firstColumnCells.map(c => c.textContent()));
305
+ *
306
+ * @see {@link getAllBodyCellLocatorsByHeaderName} for getting cells by header name
307
+ * @see {@link getBodyCellLocator} for getting a specific cell by row and column index
222
308
  */
223
- getAllBodyCellLocatorsByHeaderIndex(headerIndex: number, options?: LoadOptions): Promise<Locator[]>;
309
+ getAllBodyCellLocatorsByHeaderIndex(headerIndex: number, options?: TableOptions): Promise<Locator[]>;
224
310
  /**
225
- * Converts the table data into a JSON object.
226
- * @param options - See {@link LoadOptions} for available options.
227
- * @returns A promise that resolves to a JSON representation of the table.
311
+ * Converts the table data into a JSON array where each object represents a row.
312
+ * Keys are header names, values are cell contents.
313
+ * @param options - Optional table loading options. See {@link TableOptions}.
314
+ * @returns A promise that resolves to an array of objects representing table rows.
315
+ * @throws Error if no header rows or body rows found.
316
+ *
317
+ * @example
318
+ * // Convert table to JSON with default options
319
+ * const data = await table.getJson();
320
+ * // Result: [{"Name": "John", "Age": "30"}, {"Name": "Jane", "Age": "25"}]
321
+ *
322
+ * @example
323
+ * // Convert with colspan enabled and duplicate suffix
324
+ * const data = await table.getJson({
325
+ * headerRowOptions: {
326
+ * colspan: { enabled: true, suffix: true },
327
+ * duplicateSuffix: true,
328
+ * emptyCellReplacement: true
329
+ * }
330
+ * });
228
331
  */
229
- getJson(options?: LoadOptions): Promise<any>;
332
+ getJson(options?: TableOptions): Promise<Record<string, string>[]>;
230
333
  /**
231
- * Waits for the header rows to be loaded.
232
- * @param options - See {@link WaitForTableRowsOptions} for available options.
233
- * @returns A promise that resolves when the header rows are loaded.
334
+ * Waits for the table body to be empty (no body rows).
335
+ * @param options - Polling options (timeout, interval, retries).
336
+ * @returns A promise that resolves when table body is empty.
337
+ * @throws Error if table is not empty within timeout period.
338
+ *
339
+ * @example
340
+ * // Wait for table to clear after delete operation
341
+ * await table.waitForEmpty({ timeout: 5000 });
342
+ *
343
+ * @see {@link waitForNonEmpty} for waiting until table has data
344
+ *
345
+ * @remarks
346
+ * **Alternative:** Consider using Playwright's `expect.poll` for more flexibility:
347
+ * ```ts
348
+ * await expect.poll(async () => (await table.getBodyRows()).length).toBe(0);
349
+ * ```
234
350
  */
235
- waitForHeaderRows(options?: WaitForTableRowsOptions): Promise<void>;
351
+ waitForEmpty(options?: PollingOptions): Promise<void>;
236
352
  /**
237
- * Waits for the body rows to be loaded.
238
- * @param options - See {@link WaitForTableRowsOptions} for available options.
239
- * @returns A promise that resolves when the body rows are loaded.
353
+ * Waits for the table body to be non-empty (at least one body row with content).
354
+ * A row is considered valid if it has at least one cell containing text.
355
+ * @param options - Polling options (timeout, interval, retries).
356
+ * @returns A promise that resolves when table has at least one body row with content.
357
+ * @throws Error if table remains empty or has no rows with content within timeout period.
358
+ *
359
+ * @example
360
+ * // Wait for table to populate after search
361
+ * await table.waitForNonEmpty({ timeout: 5000 });
362
+ *
363
+ * @see {@link waitForEmpty} for waiting until table is cleared
364
+ *
365
+ * @remarks
366
+ * **Alternative:** Consider using Playwright's `expect.poll` for more flexibility:
367
+ * ```ts
368
+ * await expect.poll(async () => (await table.getBodyRows()).length).toBeGreaterThan(0);
369
+ * ```
240
370
  */
241
- waitForBodyRows(options?: WaitForTableRowsOptions): Promise<void>;
242
- private load;
371
+ waitForNonEmpty(options?: PollingOptions): Promise<void>;
372
+ /**
373
+ * Waits for the table body to have exactly N rows.
374
+ * @param count - The exact number of body rows expected.
375
+ * @param options - Polling options (timeout, interval, retries).
376
+ * @returns A promise that resolves when the table has exactly the specified number of rows.
377
+ * @throws Error if row count doesn't match within timeout period.
378
+ *
379
+ * @example
380
+ * // Wait for exactly 10 rows after pagination
381
+ * await table.waitForExactRowCount(10, { timeout: 5000 });
382
+ *
383
+ * @example
384
+ * // Verify table has exactly 0 rows (alternative to waitForEmpty)
385
+ * await table.waitForExactRowCount(0);
386
+ *
387
+ * @see {@link getRowCount} for getting current row counts
388
+ *
389
+ * @remarks
390
+ * **Alternative:** Consider using Playwright's `expect.poll` for more flexibility:
391
+ * ```ts
392
+ * await expect.poll(async () => (await table.getBodyRows()).length).toBe(10);
393
+ * ```
394
+ */
395
+ waitForExactRowCount(count: number, options?: PollingOptions): Promise<void>;
396
+ /**
397
+ * Gets the count of header and body rows in the table.
398
+ * Lightweight method that doesn't fetch cell data, only counts rows.
399
+ * @returns A promise that resolves to an object with header and body row counts.
400
+ *
401
+ * @example
402
+ * // Get current row counts
403
+ * const counts = await table.getRowCount();
404
+ * console.log(`Headers: ${counts.header}, Body: ${counts.body}`);
405
+ *
406
+ * @example
407
+ * // Assert specific row count
408
+ * const { body } = await table.getRowCount();
409
+ * expect(body).toBe(10);
410
+ */
411
+ getRowCount(): Promise<{
412
+ header: number;
413
+ body: number;
414
+ }>;
415
+ /**
416
+ * Gets all distinct (unique) values from a specific column.
417
+ * Returns a sorted array of unique string values, excluding empty values.
418
+ * @param headerName - The header name of the column to extract distinct values from.
419
+ * @param options - Optional table loading options. See {@link TableOptions}.
420
+ * @returns A promise that resolves to a sorted array of distinct values.
421
+ * @throws Error if the specified header is not found in the table.
422
+ *
423
+ * @example
424
+ * // Get all unique status values
425
+ * const statuses = await table.getDistinctColumnValues("Status");
426
+ * // Result: ["Active", "Inactive", "Pending"]
427
+ *
428
+ * @example
429
+ * // Get distinct countries for filtering
430
+ * const countries = await table.getDistinctColumnValues("Country");
431
+ * console.log(`Available countries: ${countries.join(", ")}`);
432
+ *
433
+ * @example
434
+ * // Verify expected values exist
435
+ * const roles = await table.getDistinctColumnValues("Role");
436
+ * expect(roles).toContain("Admin");
437
+ * expect(roles).toHaveLength(3);
438
+ *
439
+ * @see {@link getAllBodyCellLocatorsByHeaderName} for getting all cell locators in a column
440
+ * @see {@link getJson} for getting full table data
441
+ */
442
+ getDistinctColumnValues(headerName: string, options?: TableOptions): Promise<string[]>;
443
+ /**
444
+ * Finds the index of the first body row matching the specified conditions.
445
+ * Returns -1 if no matching row is found.
446
+ * @param conditions - Record of header names and expected cell values to match.
447
+ * @param options - Optional table loading options. See {@link TableOptions}.
448
+ * @returns A promise that resolves to the 0-based index of the first matching row, or -1 if not found.
449
+ * @throws Error if specified headers don't exist in the table.
450
+ *
451
+ * @example
452
+ * // Find index of row where Status is "Active"
453
+ * const index = await table.findRowIndex({ Status: "Active" });
454
+ * if (index >= 0) {
455
+ * console.log(`Found at row ${index}`);
456
+ * }
457
+ *
458
+ * @example
459
+ * // Find row and interact with it
460
+ * const rowIndex = await table.findRowIndex({ Username: "john.doe" });
461
+ * if (rowIndex >= 0) {
462
+ * const cell = table.getBodyCellLocator(rowIndex, 0);
463
+ * await cell.click();
464
+ * }
465
+ *
466
+ * @see {@link getBodyCellLocatorByRowConditions} for getting cell locators from matching rows
467
+ * @see {@link waitForRowByConditions} for waiting until matching row appears
468
+ */
469
+ findRowIndex(conditions: Record<string, string>, options?: TableOptions): Promise<number>;
470
+ private getTable;
471
+ private normalizeGetBodyRowsOptions;
472
+ private resolveHeaderRowOptions;
243
473
  private mainHeaderRow;
474
+ /**
475
+ * Gets the index of a header in the main header row, throwing an error if not found.
476
+ * @param header - The header name to find.
477
+ * @param mainHeader - The main header row to search in.
478
+ * @returns The 0-based index of the header.
479
+ * @throws Error if the header is not found.
480
+ */
481
+ private getHeaderIndex;
244
482
  }
245
483
 
246
- export { type BodyRow, type Cell, CellContentType, type HeaderRow, type HeaderRowOptions, type LoadOptions, PlaywrightTable, type WaitForTableRowsOptions };
484
+ export { type BodyRow, type Cell, CellContentType, type HeaderRow, type HeaderRowOptions, PlaywrightTable, type PollingOptions, type TableOptions };