@rickcedwhat/playwright-smart-table 3.1.0 → 3.2.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.
@@ -10,15 +10,21 @@ exports.TYPE_CONTEXT = `
10
10
  export type Selector = string | ((root: Locator | Page) => Locator);
11
11
 
12
12
  export type SmartRow = Locator & {
13
+ getRequestIndex(): number | undefined; // Helper to get the row index if known
14
+ rowIndex?: number;
13
15
  getCell(column: string): Locator;
14
16
  toJSON(): Promise<Record<string, string>>;
15
17
  /**
16
18
  * Fills the row with data. Automatically detects input types (text input, select, checkbox, etc.).
17
19
  */
20
+ fill: (data: Record<string, any>, options?: FillOptions) => Promise<void>;
21
+ /**
22
+ * Alias for fill() to avoid conflict with Locator.fill()
23
+ */
18
24
  smartFill: (data: Record<string, any>, options?: FillOptions) => Promise<void>;
19
25
  };
20
26
 
21
- export type StrategyContext = TableContext;
27
+ export type StrategyContext = TableContext & { rowLocator?: Locator; rowIndex?: number };
22
28
 
23
29
  /**
24
30
  * Defines the contract for a sorting strategy.
@@ -63,45 +69,81 @@ export interface PromptOptions {
63
69
  includeTypes?: boolean;
64
70
  }
65
71
 
72
+ export type FillStrategy = (options: {
73
+ row: SmartRow;
74
+ columnName: string;
75
+ value: any;
76
+ index: number;
77
+ page: Page;
78
+ rootLocator: Locator;
79
+ table: TableResult; // The parent table instance
80
+ fillOptions?: FillOptions;
81
+ }) => Promise<void>;
82
+
83
+ export type { HeaderStrategy } from './strategies/headers';
84
+ export type { ColumnStrategy } from './strategies/columns';
85
+
86
+ /**
87
+ * Configuration options for useTable.
88
+ */
66
89
  export interface TableConfig {
67
- rowSelector?: Selector;
68
- headerSelector?: Selector;
69
- cellSelector?: Selector;
90
+ /** Selector for the table headers */
91
+ headerSelector?: string;
92
+ /** Selector for the table rows */
93
+ rowSelector?: string;
94
+ /** Selector for the cells within a row */
95
+ cellSelector?: string;
96
+ /** Strategy for filling forms within the table */
97
+ fillStrategy?: FillStrategy;
98
+ /** Strategy for discovering headers */
99
+ headerStrategy?: HeaderStrategy;
100
+ /** Strategy for navigating to columns */
101
+ columnStrategy?: ColumnStrategy;
102
+ /** Number of pages to scan for verification */
103
+ maxPages?: number;
104
+
105
+ /** Pagination Strategy */
70
106
  pagination?: PaginationStrategy;
107
+ /** Sorting Strategy */
71
108
  sorting?: SortingStrategy;
72
- maxPages?: number;
73
- /**
109
+ /**
74
110
  * Hook to rename columns dynamically.
75
- * * @param args.text - The default innerText of the header.
76
- * @param args.index - The column index.
77
- * @param args.locator - The specific header cell locator.
78
111
  */
79
112
  headerTransformer?: (args: { text: string, index: number, locator: Locator }) => string | Promise<string>;
113
+ /** Automatically scroll to table on init */
80
114
  autoScroll?: boolean;
81
- /**
82
- * Enable debug mode to log internal state to console.
83
- */
115
+ /** Enable debug logs */
84
116
  debug?: boolean;
85
- /**
86
- * Strategy to reset the table to the initial page.
87
- * Called when table.reset() is invoked.
88
- */
117
+ /** Reset hook */
89
118
  onReset?: (context: TableContext) => Promise<void>;
119
+ /**
120
+ * Custom resolver for finding a cell.
121
+ * Overrides cellSelector logic if provided.
122
+ * Useful for virtualized tables where nth() index doesn't match DOM index.
123
+ */
124
+ cellResolver?: (args: { row: Locator, columnName: string, columnIndex: number, rowIndex?: number }) => Locator;
90
125
  }
91
126
 
92
- /**
93
- * Represents the final, resolved table configuration after default values have been applied.
94
- * All optional properties from TableConfig are now required, except for \`sorting\`.
95
- */
96
- export type FinalTableConfig = Required<Omit<TableConfig, 'sorting'>> & {
97
- sorting?: SortingStrategy;
98
- };
127
+ export interface FinalTableConfig extends TableConfig {
128
+ headerSelector: string;
129
+ rowSelector: string;
130
+ cellSelector: string;
131
+ fillStrategy: FillStrategy;
132
+ headerStrategy: HeaderStrategy;
133
+ columnStrategy: ColumnStrategy;
134
+ maxPages: number;
135
+ pagination: PaginationStrategy;
136
+ autoScroll: boolean;
137
+ debug: boolean;
138
+ headerTransformer: (args: { text: string, index: number, locator: Locator }) => string | Promise<string>;
139
+ onReset: (context: TableContext) => Promise<void>;
140
+ cellResolver?: (args: { row: Locator, columnName: string, columnIndex: number, rowIndex?: number }) => Locator;
141
+ }
99
142
 
100
143
  export interface FillOptions {
101
144
  /**
102
145
  * Custom input mappers for specific columns.
103
146
  * Maps column names to functions that return the input locator for that cell.
104
- * Columns not specified here will use auto-detection.
105
147
  */
106
148
  inputMappers?: Record<string, (cell: Locator) => Locator>;
107
149
  }
@@ -120,20 +162,35 @@ export interface TableResult {
120
162
  * Finds a row on the current page only. Returns immediately (sync).
121
163
  * Throws error if table is not initialized.
122
164
  */
123
- getByRow: (
124
- filters: Record<string, string | RegExp | number>,
125
- options?: { exact?: boolean }
126
- ) => SmartRow;
165
+ getByRow: {
166
+ (index: number): SmartRow;
167
+ (
168
+ filters: Record<string, string | RegExp | number>,
169
+ options?: { exact?: boolean }
170
+ ): SmartRow;
171
+ };
127
172
 
128
173
  /**
129
174
  * Searches for a row across all available data using the configured strategy (pagination, scroll, etc.).
130
175
  * Auto-initializes if needed.
131
176
  */
132
177
  searchForRow: (
133
- filters: Record<string, string | RegExp | number>,
178
+ filters: Record<string, string | RegExp | number>,
134
179
  options?: { exact?: boolean, maxPages?: number }
135
180
  ) => Promise<SmartRow>;
136
181
 
182
+ /**
183
+ * Manually scrolls to a column using the configured ColumnStrategy.
184
+ */
185
+ scrollToColumn: (columnName: string) => Promise<void>;
186
+
187
+ getAllCurrentRows: <T extends { asJSON?: boolean }>(
188
+ options?: { filter?: Record<string, any>, exact?: boolean } & T
189
+ ) => Promise<T['asJSON'] extends true ? Record<string, string>[] : SmartRow[]>;
190
+
191
+ /**
192
+ * @deprecated Use getAllCurrentRows instead. This method will be removed in a future major version.
193
+ */
137
194
  getAllRows: <T extends { asJSON?: boolean }>(
138
195
  options?: { filter?: Record<string, any>, exact?: boolean } & T
139
196
  ) => Promise<T['asJSON'] extends true ? Record<string, string>[] : SmartRow[]>;
@@ -197,5 +254,5 @@ export interface TableResult {
197
254
  /**
198
255
  * Restricted table result that excludes methods that shouldn't be called during iteration.
199
256
  */
200
- export type RestrictedTableResult = Omit<TableResult, 'searchForRow' | 'iterateThroughTable' | 'reset'>;
257
+ export type RestrictedTableResult = Omit<TableResult, 'searchForRow' | 'iterateThroughTable' | 'reset' | 'getAllRows'>;
201
258
  `;
package/dist/types.d.ts CHANGED
@@ -1,14 +1,23 @@
1
1
  import type { Locator, Page } from '@playwright/test';
2
2
  export type Selector = string | ((root: Locator | Page) => Locator);
3
3
  export type SmartRow = Locator & {
4
+ getRequestIndex(): number | undefined;
5
+ rowIndex?: number;
4
6
  getCell(column: string): Locator;
5
7
  toJSON(): Promise<Record<string, string>>;
6
8
  /**
7
9
  * Fills the row with data. Automatically detects input types (text input, select, checkbox, etc.).
8
10
  */
11
+ fill: (data: Record<string, any>, options?: FillOptions) => Promise<void>;
12
+ /**
13
+ * Alias for fill() to avoid conflict with Locator.fill()
14
+ */
9
15
  smartFill: (data: Record<string, any>, options?: FillOptions) => Promise<void>;
10
16
  };
11
- export type StrategyContext = TableContext;
17
+ export type StrategyContext = TableContext & {
18
+ rowLocator?: Locator;
19
+ rowIndex?: number;
20
+ };
12
21
  /**
13
22
  * Defines the contract for a sorting strategy.
14
23
  */
@@ -46,47 +55,96 @@ export interface PromptOptions {
46
55
  output?: 'console' | 'error';
47
56
  includeTypes?: boolean;
48
57
  }
58
+ export type FillStrategy = (options: {
59
+ row: SmartRow;
60
+ columnName: string;
61
+ value: any;
62
+ index: number;
63
+ page: Page;
64
+ rootLocator: Locator;
65
+ table: TableResult;
66
+ fillOptions?: FillOptions;
67
+ }) => Promise<void>;
68
+ export type { HeaderStrategy } from './strategies/headers';
69
+ export type { ColumnStrategy } from './strategies/columns';
70
+ import { HeaderStrategy } from './strategies/headers';
71
+ import { ColumnStrategy } from './strategies/columns';
72
+ /**
73
+ * Configuration options for useTable.
74
+ */
49
75
  export interface TableConfig {
50
- rowSelector?: Selector;
51
- headerSelector?: Selector;
52
- cellSelector?: Selector;
76
+ /** Selector for the table headers */
77
+ headerSelector?: string;
78
+ /** Selector for the table rows */
79
+ rowSelector?: string;
80
+ /** Selector for the cells within a row */
81
+ cellSelector?: string;
82
+ /** Strategy for filling forms within the table */
83
+ fillStrategy?: FillStrategy;
84
+ /** Strategy for discovering headers */
85
+ headerStrategy?: HeaderStrategy;
86
+ /** Strategy for navigating to columns */
87
+ columnStrategy?: ColumnStrategy;
88
+ /** Number of pages to scan for verification */
89
+ maxPages?: number;
90
+ /** Pagination Strategy */
53
91
  pagination?: PaginationStrategy;
92
+ /** Sorting Strategy */
54
93
  sorting?: SortingStrategy;
55
- maxPages?: number;
56
94
  /**
57
95
  * Hook to rename columns dynamically.
58
- * * @param args.text - The default innerText of the header.
59
- * @param args.index - The column index.
60
- * @param args.locator - The specific header cell locator.
61
96
  */
62
97
  headerTransformer?: (args: {
63
98
  text: string;
64
99
  index: number;
65
100
  locator: Locator;
66
101
  }) => string | Promise<string>;
102
+ /** Automatically scroll to table on init */
67
103
  autoScroll?: boolean;
68
- /**
69
- * Enable debug mode to log internal state to console.
70
- */
104
+ /** Enable debug logs */
71
105
  debug?: boolean;
72
- /**
73
- * Strategy to reset the table to the initial page.
74
- * Called when table.reset() is invoked.
75
- */
106
+ /** Reset hook */
76
107
  onReset?: (context: TableContext) => Promise<void>;
108
+ /**
109
+ * Custom resolver for finding a cell.
110
+ * Overrides cellSelector logic if provided.
111
+ * Useful for virtualized tables where nth() index doesn't match DOM index.
112
+ */
113
+ cellResolver?: (args: {
114
+ row: Locator;
115
+ columnName: string;
116
+ columnIndex: number;
117
+ rowIndex?: number;
118
+ }) => Locator;
119
+ }
120
+ export interface FinalTableConfig extends TableConfig {
121
+ headerSelector: string;
122
+ rowSelector: string;
123
+ cellSelector: string;
124
+ fillStrategy: FillStrategy;
125
+ headerStrategy: HeaderStrategy;
126
+ columnStrategy: ColumnStrategy;
127
+ maxPages: number;
128
+ pagination: PaginationStrategy;
129
+ autoScroll: boolean;
130
+ debug: boolean;
131
+ headerTransformer: (args: {
132
+ text: string;
133
+ index: number;
134
+ locator: Locator;
135
+ }) => string | Promise<string>;
136
+ onReset: (context: TableContext) => Promise<void>;
137
+ cellResolver?: (args: {
138
+ row: Locator;
139
+ columnName: string;
140
+ columnIndex: number;
141
+ rowIndex?: number;
142
+ }) => Locator;
77
143
  }
78
- /**
79
- * Represents the final, resolved table configuration after default values have been applied.
80
- * All optional properties from TableConfig are now required, except for `sorting`.
81
- */
82
- export type FinalTableConfig = Required<Omit<TableConfig, 'sorting'>> & {
83
- sorting?: SortingStrategy;
84
- };
85
144
  export interface FillOptions {
86
145
  /**
87
146
  * Custom input mappers for specific columns.
88
147
  * Maps column names to functions that return the input locator for that cell.
89
- * Columns not specified here will use auto-detection.
90
148
  */
91
149
  inputMappers?: Record<string, (cell: Locator) => Locator>;
92
150
  }
@@ -104,9 +162,12 @@ export interface TableResult {
104
162
  * Finds a row on the current page only. Returns immediately (sync).
105
163
  * Throws error if table is not initialized.
106
164
  */
107
- getByRow: (filters: Record<string, string | RegExp | number>, options?: {
108
- exact?: boolean;
109
- }) => SmartRow;
165
+ getByRow: {
166
+ (index: number): SmartRow;
167
+ (filters: Record<string, string | RegExp | number>, options?: {
168
+ exact?: boolean;
169
+ }): SmartRow;
170
+ };
110
171
  /**
111
172
  * Searches for a row across all available data using the configured strategy (pagination, scroll, etc.).
112
173
  * Auto-initializes if needed.
@@ -115,6 +176,19 @@ export interface TableResult {
115
176
  exact?: boolean;
116
177
  maxPages?: number;
117
178
  }) => Promise<SmartRow>;
179
+ /**
180
+ * Manually scrolls to a column using the configured ColumnStrategy.
181
+ */
182
+ scrollToColumn: (columnName: string) => Promise<void>;
183
+ getAllCurrentRows: <T extends {
184
+ asJSON?: boolean;
185
+ }>(options?: {
186
+ filter?: Record<string, any>;
187
+ exact?: boolean;
188
+ } & T) => Promise<T['asJSON'] extends true ? Record<string, string>[] : SmartRow[]>;
189
+ /**
190
+ * @deprecated Use getAllCurrentRows instead. This method will be removed in a future major version.
191
+ */
118
192
  getAllRows: <T extends {
119
193
  asJSON?: boolean;
120
194
  }>(options?: {
@@ -188,4 +262,4 @@ export interface TableResult {
188
262
  /**
189
263
  * Restricted table result that excludes methods that shouldn't be called during iteration.
190
264
  */
191
- export type RestrictedTableResult = Omit<TableResult, 'searchForRow' | 'iterateThroughTable' | 'reset'>;
265
+ export type RestrictedTableResult = Omit<TableResult, 'searchForRow' | 'iterateThroughTable' | 'reset' | 'getAllRows'>;
@@ -1,25 +1,23 @@
1
1
  import type { Locator } from '@playwright/test';
2
2
  import { TableConfig, Selector, TableResult, PaginationStrategy } from './types';
3
+ import { FillStrategies } from './strategies/fill';
4
+ import { HeaderStrategies } from './strategies/headers';
5
+ import { ColumnStrategies } from './strategies/columns';
3
6
  /**
4
- * A collection of pre-built pagination strategies.
7
+ * Main hook to interact with a table.
5
8
  */
9
+ export declare const useTable: (rootLocator: Locator, configOptions?: TableConfig) => TableResult;
6
10
  export declare const PaginationStrategies: {
7
11
  clickNext: (nextButtonSelector: Selector, timeout?: number) => PaginationStrategy;
8
12
  clickLoadMore: (buttonSelector: Selector, timeout?: number) => PaginationStrategy;
9
13
  infiniteScroll: (timeout?: number) => PaginationStrategy;
10
14
  };
11
- /**
12
- * @deprecated Use `PaginationStrategies` instead. This alias will be removed in a future major version.
13
- */
14
15
  export declare const TableStrategies: {
15
16
  clickNext: (nextButtonSelector: Selector, timeout?: number) => PaginationStrategy;
16
17
  clickLoadMore: (buttonSelector: Selector, timeout?: number) => PaginationStrategy;
17
18
  infiniteScroll: (timeout?: number) => PaginationStrategy;
18
19
  };
19
- /**
20
- * A collection of pre-built sorting strategies.
21
- */
22
20
  export declare const SortingStrategies: {
23
21
  AriaSort: () => import("./types").SortingStrategy;
24
22
  };
25
- export declare const useTable: (rootLocator: Locator, configOptions?: TableConfig) => TableResult;
23
+ export { FillStrategies, HeaderStrategies, ColumnStrategies };