webship-js 1.0.41 → 1.0.43

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.
@@ -1,11 +1,41 @@
1
1
  const { Given } = require('@cucumber/cucumber');
2
2
  const { When } = require('@cucumber/cucumber');
3
3
  const { Then } = require('@cucumber/cucumber');
4
- require('../../lib/custom-hooks/before-after');
4
+ const {Before, After, BeforeStep, AfterStep} = require('@cucumber/cucumber');
5
+
5
6
 
6
7
  const axios = require('axios');
7
8
  const path = require('path');
8
9
 
10
+
11
+ /**
12
+ * Safely pauses based on global config.
13
+ * Handles case where browser or globals may be undefined.
14
+ * @param {string} key
15
+ */
16
+ function safePause(key) {
17
+ const time = (global.browser?.globals?.minimum_wait_time?.[key]) || 0;
18
+ if (time > 0 && global.browser?.pause) {
19
+ global.browser.pause(time);
20
+ }
21
+ }
22
+
23
+ Before(function () {
24
+ safePause('before_scenario');
25
+ });
26
+
27
+ After(function () {
28
+ safePause('after_scenario');
29
+ });
30
+
31
+ BeforeStep(function () {
32
+ safePause('before_step');
33
+ });
34
+
35
+ AfterStep(function () {
36
+ safePause('after_step');
37
+ });
38
+
9
39
  /**
10
40
  * Opens homepage.
11
41
  *
@@ -223,6 +253,165 @@ When(/^(I |we )*click "([^"]*)?" by( its)*( "([^"]*)?")* (attribute|attr)$/, fun
223
253
 
224
254
  });
225
255
 
256
+ /**
257
+ * Clicks on specific text within a table row that contains a specified identifier text.
258
+ * This step finds a table, locates a row containing the identifier text, then clicks on the target text within that row.
259
+ *
260
+ * Example #1: When I click "Edit" in the "John Smith" row
261
+ * Example #2: When I click "Delete" in the "Product A" row
262
+ * Example #3: When we click "View Details" in the "Order #12345" row
263
+ * Example #4: And I click "Download" in the "Report 2024" row
264
+ *
265
+ */
266
+ When(/^(I |we )*click "([^"]*)?" in( the)* "([^"]*)?" row$/, function (pronounCase, clickText, theCase, rowIdentifier) {
267
+
268
+ // First, find all table elements on the page
269
+ return browser.elements('css selector', 'table', function (tableResult) {
270
+ if (tableResult.value.length === 0) {
271
+ throw new Error('No tables found on the page');
272
+ }
273
+
274
+ // Search through each table for the row containing the identifier
275
+ let foundElement = false;
276
+
277
+ tableResult.value.forEach(function (table, tableIndex) {
278
+ if (foundElement) return; // Skip if already found
279
+
280
+ // Get all rows in this table
281
+ browser.elements('css selector', 'tr', table, function (rowResult) {
282
+ if (rowResult.value.length === 0) return;
283
+
284
+ rowResult.value.forEach(function (row, rowIndex) {
285
+ if (foundElement) return; // Skip if already found
286
+
287
+ // Check if this row contains the identifier text
288
+ browser.getText(row, function (rowText) {
289
+ if (rowText.value && rowText.value.includes(rowIdentifier)) {
290
+
291
+ // Found the correct row, now look for the clickable text within this row
292
+ browser.elements('css selector', '*', row, function (cellElements) {
293
+ cellElements.value.forEach(function (element) {
294
+ if (foundElement) return; // Skip if already found
295
+
296
+ browser.getText(element, function (elementText) {
297
+ if (elementText.value && elementText.value.trim() === clickText) {
298
+ // Found the target text, click it
299
+ browser.click(element);
300
+ foundElement = true;
301
+ return;
302
+ }
303
+ });
304
+
305
+ // Also check if element is a link or button with the text
306
+ browser.getAttribute(element, 'tagName', function (tagName) {
307
+ if (foundElement) return;
308
+
309
+ if (tagName.value && (tagName.value.toLowerCase() === 'a' || tagName.value.toLowerCase() === 'button')) {
310
+ browser.getText(element, function (linkText) {
311
+ if (linkText.value && linkText.value.trim() === clickText) {
312
+ browser.click(element);
313
+ foundElement = true;
314
+ return;
315
+ }
316
+ });
317
+ }
318
+ });
319
+ });
320
+ });
321
+ }
322
+ });
323
+ });
324
+ });
325
+ });
326
+
327
+ // Add a small pause to ensure the operation completes
328
+ browser.pause(1000);
329
+
330
+ // If we didn't find the element, throw an error
331
+ if (!foundElement) {
332
+ throw new Error(`Could not find "${clickText}" in the "${rowIdentifier}" row. Please verify the table structure and text content.`);
333
+ }
334
+ });
335
+ });
336
+
337
+ /**
338
+ * Asserts that specific text is visible within a table row that contains a specified identifier text.
339
+ * This step finds a table, locates a row containing the identifier text, then verifies the target text is visible within that row.
340
+ *
341
+ * Example #1: Then I should see "Active" in the "John Smith" row
342
+ * Example #2: Then I should see "In Stock" in the "Product A" row
343
+ * Example #3: Then we should see "Processing" in the "Order #12345" row
344
+ * Example #4: And I should see "Admin" in the "john.smith@example.com" row
345
+ *
346
+ */
347
+ Then(/^(I |we )*should see "([^"]*)?" in( the)* "([^"]*)?" row$/, function (pronounCase, expectedText, theCase, rowIdentifier) {
348
+
349
+ return browser.execute(function(rowId, expectedTxt) {
350
+ // Find all tables
351
+ const tables = document.querySelectorAll('table');
352
+ if (tables.length === 0) {
353
+ throw new Error('No tables found on the page');
354
+ }
355
+
356
+ // Search through each table for the row containing the identifier
357
+ for (let table of tables) {
358
+ const rows = table.querySelectorAll('tr');
359
+ for (let row of rows) {
360
+ const rowText = row.textContent || row.innerText;
361
+ if (rowText.includes(rowId)) {
362
+ // Found the correct row, now check if it contains the expected text
363
+ if (rowText.includes(expectedTxt)) {
364
+ return true; // Found the text
365
+ }
366
+ }
367
+ }
368
+ }
369
+ return false; // Text not found
370
+ }, [rowIdentifier, expectedText], function(result) {
371
+ if (!result.value) {
372
+ throw new Error(`Could not find "${expectedText}" in the "${rowIdentifier}" row. Please verify the table structure and text content.`);
373
+ }
374
+ });
375
+ });
376
+
377
+ /**
378
+ * Asserts that specific text is NOT visible within a table row that contains a specified identifier text.
379
+ *
380
+ * Example #1: Then I should not see "Admin" in the "Jane Doe" row
381
+ * Example #2: Then I should not see "Out of Stock" in the "Product A" row
382
+ * Example #3: And I should not see "Inactive" in the "john.smith@example.com" row
383
+ *
384
+ */
385
+ Then(/^(I |we )*should not see "([^"]*)?" in( the)* "([^"]*)?" row$/, function (pronounCase, expectedText, theCase, rowIdentifier) {
386
+
387
+ return browser.execute(function(rowId, expectedTxt) {
388
+ // Find all tables
389
+ const tables = document.querySelectorAll('table');
390
+ if (tables.length === 0) {
391
+ throw new Error('No tables found on the page');
392
+ }
393
+
394
+ // Search through each table for the row containing the identifier
395
+ for (let table of tables) {
396
+ const rows = table.querySelectorAll('tr');
397
+ for (let row of rows) {
398
+ const rowText = row.textContent || row.innerText;
399
+ if (rowText.includes(rowId)) {
400
+ // Found the correct row, now check if it contains the expected text
401
+ if (rowText.includes(expectedTxt)) {
402
+ return true; // Found the text (this is bad for "should not see")
403
+ }
404
+ }
405
+ }
406
+ }
407
+ return false; // Text not found (this is good for "should not see")
408
+ }, [rowIdentifier, expectedText], function(result) {
409
+ if (result.value) {
410
+ throw new Error(`Found "${expectedText}" in the "${rowIdentifier}" row, but it should not be there.`);
411
+ }
412
+ });
413
+ });
414
+
226
415
  /**
227
416
  * Reloads current page.
228
417
  *
@@ -1337,3 +1526,173 @@ When(/^(I scroll|we scroll|scrolling)? to bottom of "([^"]*)"$/, function(pronou
1337
1526
  throw new Error(`Failed to scroll to bottom of element "${selector}": ${error.message}`);
1338
1527
  }
1339
1528
  });
1529
+
1530
+ /**
1531
+ * Scrolls right the page by a custom number of pixels specified by the user.
1532
+ *
1533
+ * Example #1: And I scroll right
1534
+ * Example #2: When I scroll right 1000
1535
+ * Example #3: And we scrolling right 300
1536
+ * Example #4: When scrolling right 750
1537
+ *
1538
+ */
1539
+ When(/^(I scroll|we scroll|scrolling)? right( ([^"]*)?)*$/, function(pronounCase, value) {
1540
+ // Parse and validate the scroll value
1541
+ let scrollValue = 350; // default value
1542
+
1543
+ if (value !== null) {
1544
+ scrollValue = parseInt(String(value).trim(), 10);
1545
+
1546
+ // Validate the parsed value
1547
+ if (isNaN(scrollValue)) {
1548
+ throw new Error(`Invalid scroll value: "${value}". Expected a number.`);
1549
+ }
1550
+
1551
+ if (scrollValue < 0) {
1552
+ throw new Error(`Scroll right value must be positive. Received: ${scrollValue}`);
1553
+ }
1554
+
1555
+ if (scrollValue > 10000) {
1556
+ console.warn(`Large scroll value detected: ${scrollValue}px. Consider if this is intentional.`);
1557
+ }
1558
+ }
1559
+
1560
+ // Make the value negative for scrolling right
1561
+ return browser.executeScript(`window.scrollBy( ${scrollValue}, 0);`);
1562
+ });
1563
+
1564
+ /**
1565
+ * Scrolls left the page by a custom number of pixels specified by the user.
1566
+ *
1567
+ * Example #1: And I scroll left
1568
+ * Example #2: When I scroll left 800
1569
+ * Example #3: And we scroll left 500
1570
+ * Example #4: When scrolling left 1200
1571
+ */
1572
+ When(/^(I scroll|we scroll|scrolling)? left( ([^"]*)?)*$/, function(pronounCase, value) {
1573
+ // Default scroll value
1574
+ let scrollValue = 350;
1575
+
1576
+ if (value !== null) {
1577
+ scrollValue = parseInt(String(value).trim(), 10);
1578
+
1579
+ // Validate the parsed value
1580
+ if (isNaN(scrollValue)) {
1581
+ throw new Error(`Invalid scroll value: "${value}". Expected a number.`);
1582
+ }
1583
+
1584
+ if (scrollValue < 0) {
1585
+ throw new Error(`Scroll left value must be positive. Received: ${scrollValue}`);
1586
+ }
1587
+
1588
+ if (scrollValue > 10000) {
1589
+ console.warn(`Large scroll value detected: ${scrollValue}px. Consider if this is intentional.`);
1590
+ }
1591
+ }
1592
+
1593
+ return browser.executeScript(`window.scrollBy( -${scrollValue},0 );`);
1594
+ });
1595
+
1596
+ /**
1597
+ * Scrolls to the very start of the current page, resetting the scroll position to zero.
1598
+ *
1599
+ * Example #1: When I scroll to start
1600
+ * Example #2: And we scrolling to the start
1601
+ * Example #3: When scrolling to the start of the page
1602
+ */
1603
+ When(/^(I scroll|we scroll|scrolling)? to( the)* start( of the page)*$/, function(pronounCase, theCase, pageCase) {
1604
+ return browser.executeScript('window.scrollTo(0, window.scrollY);');
1605
+ });
1606
+
1607
+ /**
1608
+ * Scrolls to the end of the current page using the full document height.
1609
+ *
1610
+ * Example #1: When I scroll to the end
1611
+ * Example #2: And we scroll to end
1612
+ * Example #3: When scrolling to the end of the page
1613
+ */
1614
+ When(/^(I scroll|we scroll|scrolling)? to( the)* end( of the page)*$/, function(pronounCase, theCase, pageCase) {
1615
+ return browser.executeScript('window.scrollTo(document.body.scrollWidth, window.scrollY);');
1616
+ });
1617
+
1618
+ /**
1619
+ * Scrolls to the start of a specific element identified by a CSS selector, resetting its scroll position to zero.
1620
+ *
1621
+ * Example #1: When I scroll to start of "#off-canvas"
1622
+ * Example #2: And we scroll to start of "#sidebar"
1623
+ * Example #3: When scrolling to start of "#main-container"
1624
+ */
1625
+ When(/^(I scroll|we scroll|scrolling)? to start of "([^"]*)"$/, function(pronounCase, selector) {
1626
+ // Validate selector
1627
+ if (!selector || selector.trim() === '') {
1628
+ throw new Error('Selector cannot be empty. Please provide a valid CSS selector.');
1629
+ }
1630
+
1631
+ // Validate selector format (basic check)
1632
+ if (selector.includes('"') || selector.includes("'")) {
1633
+ throw new Error(`Invalid selector format: "${selector}". Selector should not contain quotes.`);
1634
+ }
1635
+
1636
+ try {
1637
+ // Check if element exists before scrolling
1638
+ const elementExists = browser.executeScript(`
1639
+ return document.querySelector("${selector}") !== null;
1640
+ `);
1641
+
1642
+ if (!elementExists) {
1643
+ throw new Error(`Element with selector "${selector}" not found.`);
1644
+ }
1645
+
1646
+ browser.executeScript(`
1647
+ const element = document.querySelector("${selector}");
1648
+ if (element) {
1649
+ element.scrollTop = 0;
1650
+ }
1651
+ `);
1652
+
1653
+ return browser.pause(2000);
1654
+ } catch (error) {
1655
+ throw new Error(`Failed to scroll to top of element "${selector}": ${error.message}`);
1656
+ }
1657
+ });
1658
+
1659
+ /**
1660
+ * Scrolls to the end of a specific element identified by a CSS selector, moving to its maximum scroll height.
1661
+ *
1662
+ * Example #1: When I scroll to end of "#off-canvas"
1663
+ * Example #2: And we scrolling to end of "#sidebar"
1664
+ * Example #3: When scrolling to end of "#main-container"
1665
+ */
1666
+ When(/^(I scroll|we scroll|scrolling)? to end of "([^"]*)"$/, function(pronounCase, selector) {
1667
+ // Validate selector
1668
+ if (!selector || selector.trim() === '') {
1669
+ throw new Error('Selector cannot be empty. Please provide a valid CSS selector.');
1670
+ }
1671
+
1672
+ // Validate selector format (basic check)
1673
+ if (selector.includes('"') || selector.includes("'")) {
1674
+ throw new Error(`Invalid selector format: "${selector}". Selector should not contain quotes.`);
1675
+ }
1676
+
1677
+ try {
1678
+ // Check if element exists before scrolling
1679
+ const elementExists = browser.executeScript(`
1680
+ return document.querySelector("${selector}") !== null;
1681
+ `);
1682
+
1683
+ if (!elementExists) {
1684
+ throw new Error(`Element with selector "${selector}" not found.`);
1685
+ }
1686
+
1687
+ browser.executeScript(`
1688
+ const element = document.querySelector("${selector}");
1689
+ if (element) {
1690
+ element.scrollTop = element.scrollWidth;
1691
+ }
1692
+ `);
1693
+
1694
+ return browser.pause(2000);
1695
+ } catch (error) {
1696
+ throw new Error(`Failed to scroll to bottom of element "${selector}": ${error.message}`);
1697
+ }
1698
+ });
@@ -1,30 +0,0 @@
1
- const {Before, After, BeforeStep, AfterStep} = require('@cucumber/cucumber');
2
-
3
- /**
4
- * Safely pauses based on global config.
5
- * Handles case where browser or globals may be undefined.
6
- * @param {string} key
7
- */
8
- function safePause(key) {
9
- const time = (global.browser?.globals?.minimum_wait_time?.[key]) || 0;
10
- if (time > 0 && global.browser?.pause) {
11
- global.browser.pause(time);
12
- }
13
- }
14
-
15
- // Register hooks
16
- Before(function () {
17
- safePause('before_scenario');
18
- });
19
-
20
- After(function () {
21
- safePause('after_scenario');
22
- });
23
-
24
- BeforeStep(function () {
25
- safePause('before_step');
26
- });
27
-
28
- AfterStep(function () {
29
- safePause('after_step');
30
- });