playwright-cucumber-ts-steps 1.0.0 → 1.0.2

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.
Files changed (60) hide show
  1. package/README.md +195 -256
  2. package/dist/backend/actions/index.js +4 -0
  3. package/dist/backend/actions/interactions.js +23 -0
  4. package/dist/backend/actions/navigation.js +19 -0
  5. package/dist/backend/api/assertions.js +26 -0
  6. package/dist/backend/api/index.js +4 -0
  7. package/dist/backend/api/requests.js +24 -0
  8. package/dist/backend/api/state.js +15 -0
  9. package/dist/backend/assertions/expectVisible.js +8 -0
  10. package/dist/backend/assertions/index.js +5 -0
  11. package/dist/backend/assertions/pageState.js +25 -0
  12. package/dist/backend/assertions/text.js +20 -0
  13. package/dist/backend/assertions/visibility.js +20 -0
  14. package/dist/backend/auth/index.js +71 -0
  15. package/dist/backend/elements/alerts.js +21 -0
  16. package/dist/backend/elements/forms.js +59 -0
  17. package/dist/backend/elements/frames.js +25 -0
  18. package/dist/backend/elements/index.js +5 -0
  19. package/dist/core/registry.js +20 -0
  20. package/dist/core/runner.js +136 -0
  21. package/dist/index.js +10 -0
  22. package/dist/reporting/index.js +43 -0
  23. package/package.json +19 -101
  24. package/LICENSE +0 -21
  25. package/src/actions/clickSteps.ts +0 -429
  26. package/src/actions/cookieSteps.ts +0 -95
  27. package/src/actions/debugSteps.ts +0 -21
  28. package/src/actions/elementFindSteps.ts +0 -961
  29. package/src/actions/fillFormSteps.ts +0 -270
  30. package/src/actions/index.ts +0 -12
  31. package/src/actions/inputSteps.ts +0 -354
  32. package/src/actions/interceptionSteps.ts +0 -325
  33. package/src/actions/miscSteps.ts +0 -1144
  34. package/src/actions/mouseSteps.ts +0 -256
  35. package/src/actions/scrollSteps.ts +0 -122
  36. package/src/actions/storageSteps.ts +0 -308
  37. package/src/assertions/buttonAndTextVisibilitySteps.ts +0 -436
  38. package/src/assertions/cookieSteps.ts +0 -131
  39. package/src/assertions/elementSteps.ts +0 -432
  40. package/src/assertions/formInputSteps.ts +0 -377
  41. package/src/assertions/index.ts +0 -11
  42. package/src/assertions/interceptionRequestsSteps.ts +0 -640
  43. package/src/assertions/locationSteps.ts +0 -315
  44. package/src/assertions/roleTestIdSteps.ts +0 -254
  45. package/src/assertions/semanticSteps.ts +0 -267
  46. package/src/assertions/storageSteps.ts +0 -250
  47. package/src/assertions/visualSteps.ts +0 -275
  48. package/src/custom_setups/loginHooks.ts +0 -154
  49. package/src/helpers/checkPeerDeps.ts +0 -19
  50. package/src/helpers/compareSnapshots.ts +0 -35
  51. package/src/helpers/hooks.ts +0 -212
  52. package/src/helpers/utils/fakerUtils.ts +0 -64
  53. package/src/helpers/utils/index.ts +0 -4
  54. package/src/helpers/utils/optionsUtils.ts +0 -104
  55. package/src/helpers/utils/resolveUtils.ts +0 -74
  56. package/src/helpers/utils/sessionUtils.ts +0 -36
  57. package/src/helpers/world.ts +0 -119
  58. package/src/iframes/frames.ts +0 -15
  59. package/src/index.ts +0 -18
  60. package/src/register.ts +0 -4
@@ -1,961 +0,0 @@
1
- import { When } from "@cucumber/cucumber";
2
- import { expect, Locator } from "@playwright/test";
3
- import { CustomWorld } from "../helpers/world";
4
-
5
- // =============================
6
- // WHEN I FIND ELEMENT(S)
7
- // =============================
8
-
9
- /**
10
- * Finds an element by CSS selector and stores it as the current element.
11
- *
12
- * ```gherkin
13
- * When I find element by selector {string}
14
- * ```
15
- *
16
- * @example
17
- * When I find element by selector ".my-class"
18
- *
19
- * @remarks
20
- * This step sets the {@link CustomWorld.element | current element} in the test context.
21
- * Subsequent steps can then interact with this single element. An `expect` assertion is included
22
- * to ensure exactly one element is found.
23
- */
24
- export async function When_I_find_element_by_selector(this: CustomWorld, selector: string) {
25
- this.element = this.page.locator(selector);
26
- await expect(this.element).toHaveCount(1);
27
- }
28
- When("I find element by selector {string}", When_I_find_element_by_selector);
29
-
30
- /**
31
- * Finds a link by its text and stores it as the current locator.
32
- *
33
- * ```gherkin
34
- * When I find link by text {string}
35
- * ```
36
- *
37
- * @example
38
- * When I find link by text "Home"
39
- *
40
- * @remarks
41
- * This step sets the {@link CustomWorld.currentLocator | currentLocator} in the test context,
42
- * allowing subsequent steps to operate on the found link. For example:
43
- *
44
- * ```gherkin
45
- * When I find link by text "Products"
46
- * And I click current element
47
- * ```
48
- */
49
- export async function When_I_find_link_by_text(this: CustomWorld, text: string) {
50
- this.currentLocator = this.getScope().getByRole("link", { name: text });
51
- }
52
- When("I find link by text {string}", When_I_find_link_by_text);
53
-
54
- /**
55
- * Finds a heading by its text and stores it as the current locator.
56
- *
57
- * ```gherkin
58
- * When I find heading by text {string}
59
- * ```
60
- *
61
- * @example
62
- * When I find heading by text "Welcome"
63
- *
64
- * @remarks
65
- * This step sets the {@link CustomWorld.currentLocator | currentLocator} in the test context,
66
- * allowing subsequent steps to operate on the found heading.
67
- */
68
- export async function When_I_find_heading_by_text(this: CustomWorld, text: string) {
69
- this.currentLocator = this.getScope().getByRole("heading", { name: text });
70
- }
71
- When("I find heading by text {string}", When_I_find_heading_by_text);
72
-
73
- /**
74
- * Finds all headings by text and stores them as the current locator.
75
- *
76
- * ```gherkin
77
- * When I find headings by text {string}
78
- * ```
79
- *
80
- * @example
81
- * When I find headings by text "Section"
82
- *
83
- * @remarks
84
- * This step sets the {@link CustomWorld.currentLocator | currentLocator} in the test context
85
- * to a locator that matches *all* headings with the specified text. This is useful for
86
- * verifying multiple instances or iterating.
87
- */
88
- export async function When_I_find_headings_by_text(this: CustomWorld, text: string) {
89
- this.currentLocator = this.getScope().getByRole("heading", { name: text });
90
- }
91
- When("I find headings by text {string}", When_I_find_headings_by_text);
92
-
93
- /**
94
- * Finds all elements by CSS selector and stores them.
95
- *
96
- * ```gherkin
97
- * When I find elements by selector {string}
98
- * ```
99
- *
100
- * @example
101
- * When I find elements by selector ".item"
102
- *
103
- * @remarks
104
- * This step sets the {@link CustomWorld.elements | elements} property in the test context
105
- * to a Playwright `Locator` representing all matching elements. You can then use steps like
106
- * "When I get first element" to pick a specific one.
107
- */
108
- export async function When_I_find_elements_by_selector(this: CustomWorld, selector: string) {
109
- this.elements = this.page.locator(selector);
110
- const count = await this.elements.count();
111
- this.log?.(`Found ${count} elements with selector ${selector}`);
112
- }
113
- When("I find elements by selector {string}", When_I_find_elements_by_selector);
114
-
115
- /**
116
- * Finds an element by its exact text and stores it as the current element.
117
- *
118
- * ```gherkin
119
- * When I find element by text {string}
120
- * ```
121
- *
122
- * @example
123
- * When I find element by text "Submit"
124
- *
125
- * @remarks
126
- * This step uses `page.getByText` with `exact: true` to find an element
127
- * that matches the text precisely. It stores the result in {@link CustomWorld.element | this.element}.
128
- * An `expect` assertion is included to ensure exactly one element is found.
129
- */
130
- export async function When_I_find_element_by_text(this: CustomWorld, text: string) {
131
- this.element = this.page.getByText(text, { exact: true });
132
- await expect(this.element).toHaveCount(1);
133
- }
134
- When("I find element by text {string}", When_I_find_element_by_text);
135
-
136
- /**
137
- * Finds an element by its title attribute and stores it as the current element.
138
- *
139
- * ```gherkin
140
- * When I find element by title {string}
141
- * ```
142
- *
143
- * @example
144
- * When I find element by title "Tooltip"
145
- *
146
- * @remarks
147
- * This step uses `page.getByTitle` to find an element with the specified `title` attribute.
148
- * It stores the result in {@link CustomWorld.element | this.element}.
149
- * An `expect` assertion is included to ensure exactly one element is found.
150
- */
151
- export async function When_I_find_element_by_title(this: CustomWorld, title: string) {
152
- this.element = this.page.getByTitle(title);
153
- await expect(this.element).toHaveCount(1);
154
- }
155
- When("I find element by title {string}", When_I_find_element_by_title);
156
-
157
- /**
158
- * Finds an element by its test id and stores it as the current element.
159
- *
160
- * ```gherkin
161
- * When I find element by testid {string}
162
- * ```
163
- *
164
- * @example
165
- * When I find element by testid "main-content"
166
- *
167
- * @remarks
168
- * This step uses `page.getByTestId` to find an element based on its
169
- * `data-testid` attribute (or configured test ID attribute).
170
- * It stores the result in {@link CustomWorld.element | this.element}.
171
- * An `expect` assertion is included to ensure exactly one element is found.
172
- */
173
- export async function When_I_find_element_by_testid(this: CustomWorld, testid: string) {
174
- this.element = this.page.getByTestId(testid);
175
- await expect(this.element).toHaveCount(1);
176
- }
177
- When("I find element by testid {string}", When_I_find_element_by_testid);
178
-
179
- /**
180
- * Finds an element by its role and stores it as the current element.
181
- *
182
- * ```gherkin
183
- * When I find element by role {string}
184
- * ```
185
- *
186
- * @example
187
- * When I find element by role "button"
188
- *
189
- * @remarks
190
- * This step uses `page.getByRole` to find an element based on its
191
- * ARIA role. It stores the result in {@link CustomWorld.element | this.element}.
192
- * An `expect` assertion is included to ensure exactly one element is found.
193
- */
194
- export async function When_I_find_element_by_role(this: CustomWorld, role: string) {
195
- this.element = this.page.getByRole(role as any);
196
- await expect(this.element).toHaveCount(1);
197
- }
198
- When("I find element by role {string}", When_I_find_element_by_role);
199
-
200
- /**
201
- * Finds an element by its placeholder text and stores it as the current element.
202
- *
203
- * ```gherkin
204
- * When I find element by placeholder text {string}
205
- * ```
206
- *
207
- * @example
208
- * When I find element by placeholder text "Enter your name"
209
- *
210
- * @remarks
211
- * This step uses `page.getByPlaceholder` to find an input or textarea element
212
- * based on its `placeholder` attribute. It stores the result in {@link CustomWorld.element | this.element}.
213
- * An `expect` assertion is included to ensure exactly one element is found.
214
- */
215
- export async function When_I_find_element_by_placeholder_text(this: CustomWorld, text: string) {
216
- this.element = this.page.getByPlaceholder(text);
217
- await expect(this.element).toHaveCount(1);
218
- }
219
- When("I find element by placeholder text {string}", When_I_find_element_by_placeholder_text);
220
-
221
- /**
222
- * Finds an element by its label text and stores it as the current element.
223
- *
224
- * ```gherkin
225
- * When I find element by label text {string}
226
- * ```
227
- *
228
- * @example
229
- * When I find element by label text "Username"
230
- *
231
- * @remarks
232
- * This step uses `page.getByLabel` to find an element associated with a given
233
- * label text (e.g., using a `<label for="...">` or wrapping the input).
234
- * It stores the result in {@link CustomWorld.element | this.element}.
235
- * An `expect` assertion is included to ensure exactly one element is found.
236
- */
237
- export async function When_I_find_element_by_label_text(this: CustomWorld, label: string) {
238
- this.element = this.page.getByLabel(label);
239
- await expect(this.element).toHaveCount(1);
240
- }
241
- When("I find element by label text {string}", When_I_find_element_by_label_text);
242
-
243
- /**
244
- * Finds all elements by label text and stores them as the current locator.
245
- *
246
- * ```gherkin
247
- * When I find elements by label text {string}
248
- * ```
249
- *
250
- * @example
251
- * When I find elements by label text "Username"
252
- *
253
- * @remarks
254
- * This step sets the {@link CustomWorld.currentLocator | currentLocator} to a locator
255
- * representing all elements associated with the given label text. This is useful
256
- * for scenarios where multiple inputs might share a similar label.
257
- */
258
- export async function When_I_find_elements_by_label_text(this: CustomWorld, label: string) {
259
- this.currentLocator = this.getScope().getByLabel(label);
260
- }
261
- When("I find elements by label text {string}", When_I_find_elements_by_label_text);
262
-
263
- /**
264
- * Finds an element by its alt text and stores it as the current element.
265
- *
266
- * ```gherkin
267
- * When I find element by alt text {string}
268
- * ```
269
- *
270
- * @example
271
- * When I find element by alt text "Logo"
272
- *
273
- * @remarks
274
- * This step uses `page.getByAltText` to find elements, typically images,
275
- * based on their `alt` attribute. It stores the result in {@link CustomWorld.element | this.element}.
276
- * An `expect` assertion is included to ensure exactly one element is found.
277
- */
278
- export async function When_I_find_element_by_alt_text(this: CustomWorld, alt: string) {
279
- this.element = this.page.getByAltText(alt);
280
- await expect(this.element).toHaveCount(1);
281
- }
282
- When("I find element by alt text {string}", When_I_find_element_by_alt_text);
283
-
284
- /**
285
- * Finds an input element by its name attribute and stores it as the current element.
286
- *
287
- * ```gherkin
288
- * When I find element by name {string}
289
- * ```
290
- *
291
- * @example
292
- * When I find element by name "email"
293
- *
294
- * @remarks
295
- * This step uses `page.getByRole("textbox", { name })` to specifically find an input
296
- * element (or similar interactive element) with the given `name` attribute.
297
- * It stores the result in {@link CustomWorld.element | this.element}.
298
- * An `expect` assertion is included to ensure exactly one element is found.
299
- */
300
- export async function When_I_find_element_by_name(this: CustomWorld, name: string) {
301
- this.element = this.page.getByRole("textbox", { name });
302
- await expect(this.element).toHaveCount(1);
303
- }
304
- When("I find element by name {string}", When_I_find_element_by_name);
305
-
306
- /**
307
- * Finds all elements by name attribute and stores them as the current locator.
308
- *
309
- * ```gherkin
310
- * When I find elements by name {string}
311
- * ```
312
- *
313
- * @example
314
- * When I find elements by name "email"
315
- *
316
- * @remarks
317
- * This step sets the {@link CustomWorld.currentLocator | currentLocator} to a CSS locator
318
- * targeting all elements with the specified `name` attribute.
319
- */
320
- export async function When_I_find_elements_by_name(this: CustomWorld, name: string) {
321
- this.currentLocator = this.getScope().locator(`[name="${name}"]`);
322
- }
323
- When("I find elements by name {string}", When_I_find_elements_by_name);
324
-
325
- /**
326
- * Finds all buttons by text (supports alias) and stores them as elements.
327
- *
328
- * ```gherkin
329
- * When I find buttons by text {string}
330
- * ```
331
- *
332
- * @example
333
- * When I find buttons by text "Save"
334
- * When I find buttons by text "@buttonAlias"
335
- *
336
- * @remarks
337
- * This step uses `page.getByRole("button", { name })` to find all buttons
338
- * matching the provided text. It supports resolving an alias from `this.data`.
339
- * The result is stored in {@link CustomWorld.elements | this.elements}.
340
- */
341
- export async function When_I_find_buttons_by_text(this: CustomWorld, buttonText: string) {
342
- // 🧠 Resolve alias
343
- if (buttonText.startsWith("@")) {
344
- const alias = buttonText.slice(1);
345
- buttonText = this.data?.[alias];
346
- if (!buttonText) {
347
- throw new Error(`No value found for alias "@${alias}"`);
348
- }
349
- }
350
-
351
- // 🔍 Locate all matching buttons
352
- this.elements = this.page.getByRole("button", {
353
- name: buttonText,
354
- exact: false,
355
- });
356
-
357
- this.log?.(`🔘 Stored all buttons matching text "${buttonText}"`);
358
- }
359
- When("I find buttons by text {string}", When_I_find_buttons_by_text);
360
-
361
- // =============================
362
- // WHEN I GET ELEMENT(S)
363
- // =============================
364
-
365
- /**
366
- * Gets the first element matching the selector and stores it as the current element.
367
- *
368
- * ```gherkin
369
- * When I get element by selector {string}
370
- * ```
371
- *
372
- * @example
373
- * When I get element by selector ".item"
374
- *
375
- * @remarks
376
- * This step targets a single element using a CSS selector and sets it as the
377
- * {@link CustomWorld.element | current element}. It's useful when you expect only
378
- * one element to match or you only need the first one.
379
- */
380
- export async function When_I_get_element_by_selector(this: CustomWorld, selector: string) {
381
- this.element = this.page.locator(selector).first();
382
- }
383
- When("I get element by selector {string}", When_I_get_element_by_selector);
384
-
385
- /**
386
- * Gets all elements matching the selector and stores them.
387
- *
388
- * ```gherkin
389
- * When I get elements by selector {string}
390
- * ```
391
- *
392
- * @example
393
- * When I get elements by selector ".item"
394
- *
395
- * @remarks
396
- * This step sets the {@link CustomWorld.elements | elements} property to a
397
- * Playwright `Locator` representing all elements that match the given CSS selector.
398
- * You can then use other steps like "When I get first element" to work with specific items.
399
- */
400
- export async function When_I_get_elements_by_selector(this: CustomWorld, selector: string) {
401
- this.elements = this.page.locator(selector);
402
- }
403
- When("I get elements by selector {string}", When_I_get_elements_by_selector);
404
-
405
- /**
406
- * Gets the first element from the stored elements collection.
407
- *
408
- * ```gherkin
409
- * When I get first element
410
- * ```
411
- *
412
- * @example
413
- * When I get first element
414
- *
415
- * @remarks
416
- * This step requires a preceding step that populates {@link CustomWorld.elements | this.elements}
417
- * (e.g., "When I find elements by selector"). It then selects the very first
418
- * element from that collection and sets it as the {@link CustomWorld.element | current element}.
419
- */
420
- export async function When_I_get_first_element(this: CustomWorld) {
421
- if (!this.elements)
422
- throw new Error("No element collection found. Use a 'find elements' step first.");
423
- this.element = this.elements.first();
424
- }
425
- When("I get first element", When_I_get_first_element);
426
-
427
- /**
428
- * Gets the last element from the stored elements collection.
429
- *
430
- * ```gherkin
431
- * When I get last element
432
- * ```
433
- *
434
- * @example
435
- * When I get last element
436
- *
437
- * @remarks
438
- * This step requires a preceding step that populates {@link CustomWorld.elements | this.elements}
439
- * (e.g., "When I find elements by selector"). It then selects the very last
440
- * element from that collection and sets it as the {@link CustomWorld.element | current element}.
441
- */
442
- export async function When_I_get_last_element(this: CustomWorld) {
443
- if (!this.elements)
444
- throw new Error("No element collection found. Use a 'find elements' step first.");
445
- this.element = this.elements.last();
446
- }
447
- When("I get last element", When_I_get_last_element);
448
-
449
- /**
450
- * Gets the nth element (1-based index) from the stored elements.
451
- *
452
- * ```gherkin
453
- * When I get {int}st element
454
- * When I get {int}nd element
455
- * When I get {int}rd element
456
- * When I get {int}th element
457
- * ```
458
- *
459
- * @example
460
- * When I get 2nd element
461
- *
462
- * @remarks
463
- * This step requires a preceding step that populates {@link CustomWorld.elements | this.elements}
464
- * (e.g., "When I find elements by selector"). It then selects the element at the
465
- * specified 1-based index from that collection and sets it as the {@link CustomWorld.element | current element}.
466
- * Error handling is included for out-of-bounds indices.
467
- */
468
- export async function When_I_get_nth_element(this: CustomWorld, index: number) {
469
- if (!this.elements)
470
- throw new Error("No elements stored to pick from. Use a 'find elements' step first.");
471
- const count = await this.elements.count();
472
- if (index < 1 || index > count) {
473
- throw new Error(`Cannot get element ${index} — only ${count} found.`);
474
- }
475
- this.element = this.elements.nth(index - 1); // Playwright is 0-based
476
- this.log?.(`Selected ${index} element from stored elements`);
477
- }
478
- When(/^I get (\d+)(?:st|nd|rd|th) element$/, When_I_get_nth_element);
479
-
480
- /**
481
- * Finds all elements by role and stores them in the elements collection.
482
- *
483
- * ```gherkin
484
- * When I find elements by role {string}
485
- * ```
486
- *
487
- * @example
488
- * When I find elements by role "button"
489
- *
490
- * @remarks
491
- * This step uses `page.getByRole` to find all elements with a specific ARIA role
492
- * and stores them in {@link CustomWorld.elements | this.elements}.
493
- * It includes a check to ensure at least one element is found.
494
- */
495
- export async function When_I_find_elements_by_role(this: CustomWorld, role: string) {
496
- const locator = this.page.getByRole(role as any);
497
- const count = await locator.count();
498
-
499
- if (count === 0) {
500
- throw new Error(`No elements found with role "${role}".`);
501
- }
502
-
503
- this.elements = locator;
504
- this.log?.(`Stored ${count} elements with role "${role}"`);
505
- }
506
- When("I find elements by role {string}", When_I_find_elements_by_role);
507
-
508
- /**
509
- * Gets the nth element (0-based index) from the stored elements.
510
- *
511
- * ```gherkin
512
- * When I get {int}rd element
513
- * ```
514
- *
515
- * @example
516
- * When I get 3rd element
517
- *
518
- * @remarks
519
- * This step requires a preceding step that populates {@link CustomWorld.elements | this.elements}.
520
- * It selects the element at the specified **0-based index** from the collection
521
- * and sets it as the {@link CustomWorld.element | current element}.
522
- *
523
- * **Note:** Consider using `When I get {int}(?:st|nd|rd|th) element` for a 1-based index
524
- * which is often more user-friendly in Gherkin.
525
- */
526
- export async function When_I_get_int_rd_element(this: CustomWorld, index: number) {
527
- if (!this.elements)
528
- throw new Error("No element collection found. Use a 'find elements' step first.");
529
- this.element = this.elements.nth(index);
530
- }
531
- When("I get {int}rd element", When_I_get_int_rd_element); // This step pattern is a bit specific; the regex-based one is more general.
532
-
533
- /**
534
- * Gets the currently focused element and stores it as the current element.
535
- *
536
- * ```gherkin
537
- * When I get focused element
538
- * ```
539
- *
540
- * @example
541
- * When I get focused element
542
- *
543
- * @remarks
544
- * This step uses `page.evaluateHandle` to find the `document.activeElement`
545
- * (the currently focused element in the browser DOM) and sets it as the
546
- * {@link CustomWorld.element | current element}. This is useful for testing focus management.
547
- */
548
- export async function When_I_get_focused_element(this: CustomWorld) {
549
- this.element = (await this.page.evaluateHandle(
550
- () => document.activeElement
551
- )) as unknown as Locator;
552
- }
553
- When("I get focused element", When_I_get_focused_element);
554
-
555
- /**
556
- * Stores the text content of the current element as an alias in the test data.
557
- *
558
- * ```gherkin
559
- * When I store element text as {string}
560
- * ```
561
- *
562
- * @example
563
- * When I store element text as "greeting"
564
- *
565
- * @remarks
566
- * This step requires a preceding step that sets the {@link CustomWorld.element | current element}.
567
- * It retrieves the `textContent` of that element and stores it in
568
- * {@link CustomWorld.data | this.data} under the provided alias, allowing it to be
569
- * reused in subsequent steps (e.g., in assertions or input fields).
570
- */
571
- export async function When_I_store_element_text_as(this: CustomWorld, alias: string) {
572
- const element = this.element;
573
- if (!element) throw new Error("No element selected. Use a 'find element' step first.");
574
- const text = await element.textContent();
575
- this.data[alias] = text?.trim();
576
- this.log?.(`Stored text "${text}" as "${alias}"`);
577
- }
578
- When("I store element text as {string}", When_I_store_element_text_as);
579
-
580
- // =============================
581
- // WHEN I FIND TEXTAREA(S)
582
- // =============================
583
-
584
- /**
585
- * Finds a textarea by its label text and stores it as the current element.
586
- *
587
- * ```gherkin
588
- * When I find textarea by label text {string}
589
- * ```
590
- *
591
- * @example
592
- * When I find textarea by label text "Description"
593
- *
594
- * @remarks
595
- * This step uses Playwright's `getByLabel` to locate a textarea associated
596
- * with the given label text. It sets the found textarea as the
597
- * {@link CustomWorld.element | current element}.
598
- */
599
- export async function When_I_find_textarea_by_label_text(this: CustomWorld, label: string) {
600
- this.element = this.page.getByLabel(label);
601
- this.log?.(`Stored textarea with label "${label}"`);
602
- }
603
- When("I find textarea by label text {string}", When_I_find_textarea_by_label_text);
604
-
605
- /**
606
- * Finds a textarea by its placeholder text and stores it as the current element.
607
- *
608
- * ```gherkin
609
- * When I find textarea by placeholder text {string}
610
- * ```
611
- *
612
- * @example
613
- * When I find textarea by placeholder text "Type here"
614
- *
615
- * @remarks
616
- * This step uses Playwright's `getByPlaceholder` to locate a textarea
617
- * based on its `placeholder` attribute. It sets the found textarea as the
618
- * {@link CustomWorld.element | current element}.
619
- */
620
- export async function When_I_find_textarea_by_placeholder_text(
621
- this: CustomWorld,
622
- placeholder: string
623
- ) {
624
- this.element = this.page.getByPlaceholder(placeholder);
625
- this.log?.(`Stored textarea with placeholder "${placeholder}"`);
626
- }
627
- When("I find textarea by placeholder text {string}", When_I_find_textarea_by_placeholder_text);
628
-
629
- /**
630
- * Finds all textareas by label text and stores them as elements.
631
- *
632
- * ```gherkin
633
- * When I find textareas by label text {string}
634
- * ```
635
- *
636
- * @example
637
- * When I find textareas by label text "Comment"
638
- *
639
- * @remarks
640
- * This step uses a CSS selector to find all textareas associated with the
641
- * given label text and stores them in the {@link CustomWorld.elements | elements}
642
- * collection.
643
- */
644
- export async function When_I_find_textareas_by_label_text(this: CustomWorld, label: string) {
645
- this.elements = this.page.locator(`label:has-text("${label}") + textarea`);
646
- this.log?.(`Stored multiple textareas with label "${label}"`);
647
- }
648
- When("I find textareas by label text {string}", When_I_find_textareas_by_label_text);
649
-
650
- /**
651
- * Finds a textarea by its name attribute and stores it as the current element.
652
- *
653
- * ```gherkin
654
- * When I find textarea by name {string}
655
- * ```
656
- *
657
- * @example
658
- * When I find textarea by name "bio"
659
- *
660
- * @remarks
661
- * This step uses a CSS selector to locate a textarea based on its `name` attribute.
662
- * It sets the found textarea as the {@link CustomWorld.element | current element}.
663
- */
664
- export async function When_I_find_textarea_by_name(this: CustomWorld, name: string) {
665
- this.element = this.page.locator(`textarea[name="${name}"]`);
666
- this.log?.(`Stored textarea with name "${name}"`);
667
- }
668
- When("I find textarea by name {string}", When_I_find_textarea_by_name);
669
-
670
- /**
671
- * Finds all textareas by ID and stores them as elements.
672
- *
673
- * ```gherkin
674
- * When I find textareas by ID {string}
675
- * ```
676
- *
677
- * @example
678
- * When I find textareas by ID "my-textarea"
679
- *
680
- * @remarks
681
- * This step uses a CSS selector to find all textareas with the specified ID
682
- * and stores them in the {@link CustomWorld.elements | elements} collection.
683
- */
684
- export async function When_I_find_textareas_by_ID(this: CustomWorld, id: string) {
685
- this.elements = this.page.locator(`textarea#${id}`);
686
- this.log?.(`Stored multiple textareas with ID "${id}"`);
687
- }
688
- When("I find textareas by ID {string}", When_I_find_textareas_by_ID);
689
-
690
- /**
691
- * Finds all textareas by placeholder text and stores them as elements.
692
- *
693
- * ```gherkin
694
- * When I find textareas by placeholder text {string}
695
- * ```
696
- *
697
- * @example
698
- * When I find textareas by placeholder text "Type here"
699
- *
700
- * @remarks
701
- * This step uses a CSS selector to find all textareas with the specified
702
- * `placeholder` attribute and stores them in the {@link CustomWorld.elements | elements}
703
- * collection.
704
- */
705
- export async function When_I_find_textareas_by_placeholder_text_multiple(
706
- this: CustomWorld,
707
- placeholder: string
708
- ) {
709
- this.elements = this.page.locator(`textarea[placeholder="${placeholder}"]`);
710
- this.log?.(`Stored multiple textareas with placeholder "${placeholder}"`);
711
- }
712
- When(
713
- "I find textareas by placeholder text {string}",
714
- When_I_find_textareas_by_placeholder_text_multiple
715
- );
716
-
717
- // =============================
718
- // WHEN I FIND INPUT(S)
719
- // =============================
720
-
721
- /**
722
- * Finds an input by its ID and stores it as the current element.
723
- *
724
- * ```gherkin
725
- * When I find input by ID {string}
726
- * ```
727
- *
728
- * @example
729
- * When I find input by ID "email"
730
- *
731
- * @remarks
732
- * This step uses a CSS selector to locate an input element by its ID.
733
- * It sets the found input as the {@link CustomWorld.element | current element}.
734
- */
735
- export async function When_I_find_input_by_ID(this: CustomWorld, id: string) {
736
- this.element = this.page.locator(`input#${id}`);
737
- this.log?.(`Stored input with ID "${id}"`);
738
- }
739
- When("I find input by ID {string}", When_I_find_input_by_ID);
740
-
741
- /**
742
- * Finds all inputs by ID and stores them as elements.
743
- *
744
- * ```gherkin
745
- * When I find inputs by ID {string}
746
- * ```
747
- *
748
- * @example
749
- * When I find inputs by ID "email"
750
- *
751
- * @remarks
752
- * This step uses a CSS selector to find all input elements with the specified ID
753
- * and stores them in the {@link CustomWorld.elements | elements} collection.
754
- */
755
- export async function When_I_find_inputs_by_ID(this: CustomWorld, id: string) {
756
- this.elements = this.page.locator(`input#${id}`);
757
- this.log?.(`Stored multiple inputs with ID "${id}"`);
758
- }
759
- When("I find inputs by ID {string}", When_I_find_inputs_by_ID);
760
-
761
- /**
762
- * Finds an input by its label text and stores it as the current element.
763
- *
764
- * ```gherkin
765
- * When I find input by label text {string}
766
- * ```
767
- *
768
- * @example
769
- * When I find input by label text "Email"
770
- *
771
- * @remarks
772
- * This step uses Playwright's `getByLabel` to locate an input associated
773
- * with the given label text. It sets the found input as the
774
- * {@link CustomWorld.element | current element}.
775
- */
776
- export async function When_I_find_input_by_label_text(this: CustomWorld, label: string) {
777
- this.element = this.page.getByLabel(label);
778
- this.log?.(`Stored input with label "${label}"`);
779
- }
780
- When("I find input by label text {string}", When_I_find_input_by_label_text);
781
-
782
- /**
783
- * Finds an input by its name attribute and stores it as the current element.
784
- *
785
- * ```gherkin
786
- * When I find input by name {string}
787
- * ```
788
- * * @example
789
- * When I find input by name "username"
790
- *
791
- * @remarks
792
- * This step uses a CSS selector to locate an input element based on its `name` attribute.
793
- * It sets the found input as the {@link CustomWorld.element | current element}.
794
- */
795
- export async function When_I_find_input_by_name(this: CustomWorld, name: string) {
796
- this.element = this.page.locator(`input[name="${name}"]`);
797
- this.log?.(`Stored input with name "${name}"`);
798
- }
799
- When("I find input by name {string}", When_I_find_input_by_name);
800
-
801
- /**
802
- * Finds an input by its placeholder text and stores it as the current element.
803
- *
804
- * ```gherkin
805
- * When I find input by placeholder text {string}
806
- * ```
807
- *
808
- * @example
809
- * When I find input by placeholder text "Enter your email"
810
- *
811
- * @remarks
812
- * This step uses Playwright's `getByPlaceholder` to locate an input
813
- * based on its `placeholder` attribute. It sets the found input as the
814
- * {@link CustomWorld.element | current element}.
815
- */
816
- export async function When_I_find_input_by_placeholder_text(
817
- this: CustomWorld,
818
- placeholder: string
819
- ) {
820
- this.element = this.page.getByPlaceholder(placeholder);
821
- this.log?.(`Stored input with placeholder "${placeholder}"`);
822
- }
823
- When("I find input by placeholder text {string}", When_I_find_input_by_placeholder_text);
824
-
825
- /**
826
- * Finds all inputs by name attribute and stores them as elements.
827
- *
828
- * ```gherkin
829
- * When I find inputs by name {string}
830
- * ```
831
- *
832
- * @example
833
- * When I find inputs by name "username"
834
- *
835
- * @remarks
836
- * This step uses a CSS selector to find all input elements with the specified
837
- * `name` attribute and stores them in the {@link CustomWorld.elements | elements}
838
- * collection.
839
- */
840
- export async function When_I_find_inputs_by_name_multiple(this: CustomWorld, name: string) {
841
- this.elements = this.page.locator(`input[name="${name}"]`);
842
- this.log?.(`Stored multiple inputs with name "${name}"`);
843
- }
844
- When("I find inputs by name {string}", When_I_find_inputs_by_name_multiple);
845
-
846
- /**
847
- * Finds all inputs by placeholder text and stores them as elements.
848
- *
849
- * ```gherkin
850
- * When I find inputs by placeholder text {string}
851
- * ```
852
- *
853
- * @example
854
- * When I find inputs by placeholder text "Search"
855
- *
856
- * @remarks
857
- * This step uses a CSS selector to find all input elements with the specified
858
- * `placeholder` attribute and stores them in the {@link CustomWorld.elements | elements}
859
- * collection.
860
- */
861
- export async function When_I_find_inputs_by_placeholder_text_multiple(
862
- this: CustomWorld,
863
- placeholder: string
864
- ) {
865
- this.elements = this.page.locator(`input[placeholder="${placeholder}"]`);
866
- this.log?.(`Stored multiple inputs with placeholder "${placeholder}"`);
867
- }
868
- When("I find inputs by placeholder text {string}", When_I_find_inputs_by_placeholder_text_multiple);
869
-
870
- /**
871
- * Finds all inputs by label text and stores them as elements.
872
- *
873
- * ```gherkin
874
- * When I find inputs by label text {string}
875
- * ```
876
- *
877
- * @example
878
- * When I find inputs by label text "Email"
879
- *
880
- * @remarks
881
- * This step uses a CSS selector to find all input elements associated with the
882
- * given label text and stores them in the {@link CustomWorld.elements | elements}
883
- * collection.
884
- */
885
- export async function When_I_find_inputs_by_label_text_multiple(this: CustomWorld, label: string) {
886
- this.elements = this.page.locator(`label:has-text("${label}") + input`);
887
- this.log?.(`Stored multiple inputs with label "${label}"`);
888
- }
889
- When("I find inputs by label text {string}", When_I_find_inputs_by_label_text_multiple);
890
-
891
- /**
892
- * Finds all inputs by display value (supports alias) and stores them as elements.
893
- *
894
- * ```gherkin
895
- * When I find inputs by display value {string}
896
- * ```
897
- *
898
- * @example
899
- * When I find inputs by display value "John"
900
- * When I find inputs by display value "@userName"
901
- *
902
- * @remarks
903
- * This step searches for all input elements whose `value` attribute matches
904
- * the provided text or resolved alias. The matching inputs are stored in the
905
- * {@link CustomWorld.elements | elements} collection.
906
- */
907
- export async function When_I_find_inputs_by_display_value_multiple(
908
- this: CustomWorld,
909
- value: string
910
- ) {
911
- // 🧠 Handle alias
912
- if (value.startsWith("@")) {
913
- const alias = value.slice(1);
914
- value = this.data?.[alias];
915
- if (!value) {
916
- throw new Error(`No value found for alias "@${alias}".`);
917
- }
918
- }
919
-
920
- // 🔍 Find all matching inputs
921
- this.elements = this.page.locator(`input[value="${value}"]`);
922
-
923
- this.log?.(`📦 Stored multiple inputs with display value "${value}"`);
924
- }
925
- When("I find inputs by display value {string}", When_I_find_inputs_by_display_value_multiple);
926
-
927
- /**
928
- * Finds an input by display value (supports alias) and stores it as the current element.
929
- *
930
- * ```gherkin
931
- * When I find input by display value {string}
932
- * ```
933
- *
934
- * @example
935
- * When I find input by display value "John"
936
- * When I find input by display value "@userName"
937
- *
938
- * @remarks
939
- * This step searches for a single input element whose `value` attribute matches
940
- * the provided text or resolved alias. It sets the found input as the
941
- * {@link CustomWorld.element | current element}. An `expect` assertion
942
- * is included to ensure the element is visible.
943
- */
944
- export async function When_I_find_input_by_display_value(this: CustomWorld, value: string) {
945
- // 🧠 Handle alias
946
- if (value.startsWith("@")) {
947
- const alias = value.slice(1);
948
- value = this.data?.[alias];
949
- if (!value) {
950
- throw new Error(`No value found for alias "@${alias}".`);
951
- }
952
- }
953
-
954
- // 🎯 Try to find input element with matching display value
955
- const locator = this.page.locator(`input[value="${value}"]`);
956
-
957
- await expect(locator).toBeVisible({ timeout: 5000 });
958
- this.element = locator;
959
- this.log?.(`🔍 Found input with value: "${value}"`);
960
- }
961
- When("I find input by display value {string}", When_I_find_input_by_display_value);