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