codeceptjs 3.5.12 → 3.5.13-beta.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 (127) hide show
  1. package/README.md +2 -2
  2. package/docs/webapi/amOnPage.mustache +11 -0
  3. package/docs/webapi/appendField.mustache +11 -0
  4. package/docs/webapi/attachFile.mustache +12 -0
  5. package/docs/webapi/blur.mustache +18 -0
  6. package/docs/webapi/checkOption.mustache +13 -0
  7. package/docs/webapi/clearCookie.mustache +9 -0
  8. package/docs/webapi/clearField.mustache +9 -0
  9. package/docs/webapi/click.mustache +25 -0
  10. package/docs/webapi/clickLink.mustache +8 -0
  11. package/docs/webapi/closeCurrentTab.mustache +7 -0
  12. package/docs/webapi/closeOtherTabs.mustache +8 -0
  13. package/docs/webapi/dontSee.mustache +11 -0
  14. package/docs/webapi/dontSeeCheckboxIsChecked.mustache +10 -0
  15. package/docs/webapi/dontSeeCookie.mustache +8 -0
  16. package/docs/webapi/dontSeeCurrentUrlEquals.mustache +10 -0
  17. package/docs/webapi/dontSeeElement.mustache +8 -0
  18. package/docs/webapi/dontSeeElementInDOM.mustache +8 -0
  19. package/docs/webapi/dontSeeInCurrentUrl.mustache +4 -0
  20. package/docs/webapi/dontSeeInField.mustache +11 -0
  21. package/docs/webapi/dontSeeInSource.mustache +8 -0
  22. package/docs/webapi/dontSeeInTitle.mustache +8 -0
  23. package/docs/webapi/doubleClick.mustache +13 -0
  24. package/docs/webapi/downloadFile.mustache +12 -0
  25. package/docs/webapi/dragAndDrop.mustache +9 -0
  26. package/docs/webapi/dragSlider.mustache +11 -0
  27. package/docs/webapi/executeAsyncScript.mustache +24 -0
  28. package/docs/webapi/executeScript.mustache +26 -0
  29. package/docs/webapi/fillField.mustache +16 -0
  30. package/docs/webapi/focus.mustache +13 -0
  31. package/docs/webapi/forceClick.mustache +28 -0
  32. package/docs/webapi/forceRightClick.mustache +18 -0
  33. package/docs/webapi/grabAllWindowHandles.mustache +7 -0
  34. package/docs/webapi/grabAttributeFrom.mustache +10 -0
  35. package/docs/webapi/grabAttributeFromAll.mustache +9 -0
  36. package/docs/webapi/grabBrowserLogs.mustache +9 -0
  37. package/docs/webapi/grabCookie.mustache +11 -0
  38. package/docs/webapi/grabCssPropertyFrom.mustache +11 -0
  39. package/docs/webapi/grabCssPropertyFromAll.mustache +10 -0
  40. package/docs/webapi/grabCurrentUrl.mustache +9 -0
  41. package/docs/webapi/grabCurrentWindowHandle.mustache +6 -0
  42. package/docs/webapi/grabDataFromPerformanceTiming.mustache +20 -0
  43. package/docs/webapi/grabElementBoundingRect.mustache +20 -0
  44. package/docs/webapi/grabGeoLocation.mustache +8 -0
  45. package/docs/webapi/grabHTMLFrom.mustache +10 -0
  46. package/docs/webapi/grabHTMLFromAll.mustache +9 -0
  47. package/docs/webapi/grabNumberOfOpenTabs.mustache +8 -0
  48. package/docs/webapi/grabNumberOfVisibleElements.mustache +9 -0
  49. package/docs/webapi/grabPageScrollPosition.mustache +8 -0
  50. package/docs/webapi/grabPopupText.mustache +5 -0
  51. package/docs/webapi/grabSource.mustache +8 -0
  52. package/docs/webapi/grabTextFrom.mustache +10 -0
  53. package/docs/webapi/grabTextFromAll.mustache +9 -0
  54. package/docs/webapi/grabTitle.mustache +8 -0
  55. package/docs/webapi/grabValueFrom.mustache +9 -0
  56. package/docs/webapi/grabValueFromAll.mustache +8 -0
  57. package/docs/webapi/grabWebElement.mustache +9 -0
  58. package/docs/webapi/grabWebElements.mustache +9 -0
  59. package/docs/webapi/moveCursorTo.mustache +12 -0
  60. package/docs/webapi/openNewTab.mustache +7 -0
  61. package/docs/webapi/pressKey.mustache +12 -0
  62. package/docs/webapi/pressKeyDown.mustache +12 -0
  63. package/docs/webapi/pressKeyUp.mustache +12 -0
  64. package/docs/webapi/pressKeyWithKeyNormalization.mustache +60 -0
  65. package/docs/webapi/refreshPage.mustache +6 -0
  66. package/docs/webapi/resizeWindow.mustache +6 -0
  67. package/docs/webapi/rightClick.mustache +14 -0
  68. package/docs/webapi/saveElementScreenshot.mustache +10 -0
  69. package/docs/webapi/saveScreenshot.mustache +12 -0
  70. package/docs/webapi/say.mustache +10 -0
  71. package/docs/webapi/scrollIntoView.mustache +11 -0
  72. package/docs/webapi/scrollPageToBottom.mustache +6 -0
  73. package/docs/webapi/scrollPageToTop.mustache +6 -0
  74. package/docs/webapi/scrollTo.mustache +12 -0
  75. package/docs/webapi/see.mustache +11 -0
  76. package/docs/webapi/seeAttributesOnElements.mustache +9 -0
  77. package/docs/webapi/seeCheckboxIsChecked.mustache +10 -0
  78. package/docs/webapi/seeCookie.mustache +8 -0
  79. package/docs/webapi/seeCssPropertiesOnElements.mustache +9 -0
  80. package/docs/webapi/seeCurrentUrlEquals.mustache +11 -0
  81. package/docs/webapi/seeElement.mustache +8 -0
  82. package/docs/webapi/seeElementInDOM.mustache +8 -0
  83. package/docs/webapi/seeInCurrentUrl.mustache +8 -0
  84. package/docs/webapi/seeInField.mustache +12 -0
  85. package/docs/webapi/seeInPopup.mustache +8 -0
  86. package/docs/webapi/seeInSource.mustache +7 -0
  87. package/docs/webapi/seeInTitle.mustache +8 -0
  88. package/docs/webapi/seeNumberOfElements.mustache +11 -0
  89. package/docs/webapi/seeNumberOfVisibleElements.mustache +10 -0
  90. package/docs/webapi/seeTextEquals.mustache +9 -0
  91. package/docs/webapi/seeTitleEquals.mustache +8 -0
  92. package/docs/webapi/selectOption.mustache +21 -0
  93. package/docs/webapi/setCookie.mustache +16 -0
  94. package/docs/webapi/setGeoLocation.mustache +12 -0
  95. package/docs/webapi/switchTo.mustache +9 -0
  96. package/docs/webapi/switchToNextTab.mustache +10 -0
  97. package/docs/webapi/switchToPreviousTab.mustache +10 -0
  98. package/docs/webapi/type.mustache +21 -0
  99. package/docs/webapi/uncheckOption.mustache +13 -0
  100. package/docs/webapi/wait.mustache +8 -0
  101. package/docs/webapi/waitForClickable.mustache +11 -0
  102. package/docs/webapi/waitForCookie.mustache +9 -0
  103. package/docs/webapi/waitForDetached.mustache +10 -0
  104. package/docs/webapi/waitForElement.mustache +11 -0
  105. package/docs/webapi/waitForEnabled.mustache +6 -0
  106. package/docs/webapi/waitForFunction.mustache +17 -0
  107. package/docs/webapi/waitForInvisible.mustache +10 -0
  108. package/docs/webapi/waitForNumberOfTabs.mustache +9 -0
  109. package/docs/webapi/waitForText.mustache +13 -0
  110. package/docs/webapi/waitForValue.mustache +10 -0
  111. package/docs/webapi/waitForVisible.mustache +10 -0
  112. package/docs/webapi/waitInUrl.mustache +9 -0
  113. package/docs/webapi/waitNumberOfVisibleElements.mustache +10 -0
  114. package/docs/webapi/waitToHide.mustache +10 -0
  115. package/docs/webapi/waitUrlEquals.mustache +10 -0
  116. package/lib/helper/Appium.js +36 -12
  117. package/lib/helper/Expect.js +10 -7
  118. package/lib/helper/JSONResponse.js +8 -8
  119. package/lib/helper/MockServer.js +221 -0
  120. package/lib/helper/Playwright.js +36 -4
  121. package/lib/helper/Puppeteer.js +33 -0
  122. package/lib/helper/WebDriver.js +32 -0
  123. package/lib/locator.js +1 -1
  124. package/package.json +12 -10
  125. package/typings/index.d.ts +16 -0
  126. package/typings/promiseBasedTypes.d.ts +30 -0
  127. package/typings/types.d.ts +33 -0
@@ -0,0 +1,21 @@
1
+ Types out the given text into an active field.
2
+ To slow down typing use a second parameter, to set interval between key presses.
3
+ _Note:_ Should be used when [`fillField`](#fillfield) is not an option.
4
+
5
+ ```js
6
+ // passing in a string
7
+ I.type('Type this out.');
8
+
9
+ // typing values with a 100ms interval
10
+ I.type('4141555311111111', 100);
11
+
12
+ // passing in an array
13
+ I.type(['T', 'E', 'X', 'T']);
14
+
15
+ // passing a secret
16
+ I.type(secret('123456'));
17
+ ```
18
+
19
+ @param {string|string[]} key or array of keys to type.
20
+ @param {?number} [delay=null] (optional) delay in ms between key presses
21
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,13 @@
1
+ Unselects a checkbox or radio button.
2
+ Element is located by label or name or CSS or XPath.
3
+
4
+ The second parameter is a context (CSS or XPath locator) to narrow the search.
5
+
6
+ ```js
7
+ I.uncheckOption('#agree');
8
+ I.uncheckOption('I Agree to Terms and Conditions');
9
+ I.uncheckOption('agree', '//form');
10
+ ```
11
+ @param {CodeceptJS.LocatorOrString} field checkbox located by label | name | CSS | XPath | strict locator.
12
+ @param {?CodeceptJS.LocatorOrString} [context=null] (optional, `null` by default) element located by CSS | XPath | strict locator.
13
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,8 @@
1
+ Pauses execution for a number of seconds.
2
+
3
+ ```js
4
+ I.wait(2); // wait 2 secs
5
+ ```
6
+
7
+ @param {number} sec number of second to wait.
8
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,11 @@
1
+ Waits for element to be clickable (by default waits for 1sec).
2
+ Element can be located by CSS or XPath.
3
+
4
+ ```js
5
+ I.waitForClickable('.btn.continue');
6
+ I.waitForClickable('.btn.continue', 5); // wait for 5 secs
7
+ ```
8
+
9
+ @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
10
+ @param {number} [sec] (optional, `1` by default) time in seconds to wait
11
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,9 @@
1
+ Waits for the specified cookie in the cookies.
2
+
3
+ ```js
4
+ I.waitForCookie("token");
5
+ ```
6
+
7
+ @param {string} name expected cookie name.
8
+ @param {number} [sec=3] (optional, `3` by default) time in seconds to wait
9
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,10 @@
1
+ Waits for an element to become not attached to the DOM on a page (by default waits for 1sec).
2
+ Element can be located by CSS or XPath.
3
+
4
+ ```js
5
+ I.waitForDetached('#popup');
6
+ ```
7
+
8
+ @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
9
+ @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
10
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,11 @@
1
+ Waits for element to be present on page (by default waits for 1sec).
2
+ Element can be located by CSS or XPath.
3
+
4
+ ```js
5
+ I.waitForElement('.btn.continue');
6
+ I.waitForElement('.btn.continue', 5); // wait for 5 secs
7
+ ```
8
+
9
+ @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
10
+ @param {number} [sec] (optional, `1` by default) time in seconds to wait
11
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,6 @@
1
+ Waits for element to become enabled (by default waits for 1sec).
2
+ Element can be located by CSS or XPath.
3
+
4
+ @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
5
+ @param {number} [sec=1] (optional) time in seconds to wait, 1 by default.
6
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,17 @@
1
+ Waits for a function to return true (waits for 1 sec by default).
2
+ Running in browser context.
3
+
4
+ ```js
5
+ I.waitForFunction(fn[, [args[, timeout]])
6
+ ```
7
+
8
+ ```js
9
+ I.waitForFunction(() => window.requests == 0);
10
+ I.waitForFunction(() => window.requests == 0, 5); // waits for 5 sec
11
+ I.waitForFunction((count) => window.requests == count, [3], 5) // pass args and wait for 5 sec
12
+ ```
13
+
14
+ @param {string|function} fn to be executed in browser context.
15
+ @param {any[]|number} [argsOrSec] (optional, `1` by default) arguments for function or seconds.
16
+ @param {number} [sec] (optional, `1` by default) time in seconds to wait
17
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,10 @@
1
+ Waits for an element to be removed or become invisible on a page (by default waits for 1sec).
2
+ Element can be located by CSS or XPath.
3
+
4
+ ```js
5
+ I.waitForInvisible('#popup');
6
+ ```
7
+
8
+ @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
9
+ @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
10
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,9 @@
1
+ Waits for number of tabs.
2
+
3
+ ```js
4
+ I.waitForNumberOfTabs(2);
5
+ ```
6
+
7
+ @param {number} expectedTabs expecting the number of tabs.
8
+ @param {number} sec number of secs to wait.
9
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,13 @@
1
+ Waits for a text to appear (by default waits for 1sec).
2
+ Element can be located by CSS or XPath.
3
+ Narrow down search results by providing context.
4
+
5
+ ```js
6
+ I.waitForText('Thank you, form has been submitted');
7
+ I.waitForText('Thank you, form has been submitted', 5, '#modal');
8
+ ```
9
+
10
+ @param {string }text to wait for.
11
+ @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
12
+ @param {CodeceptJS.LocatorOrString} [context] (optional) element located by CSS|XPath|strict locator.
13
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,10 @@
1
+ Waits for the specified value to be in value attribute.
2
+
3
+ ```js
4
+ I.waitForValue('//input', "GoodValue");
5
+ ```
6
+
7
+ @param {LocatorOrString} field input field.
8
+ @param {string }value expected value.
9
+ @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
10
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,10 @@
1
+ Waits for an element to become visible on a page (by default waits for 1sec).
2
+ Element can be located by CSS or XPath.
3
+
4
+ ```js
5
+ I.waitForVisible('#popup');
6
+ ```
7
+
8
+ @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
9
+ @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
10
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,9 @@
1
+ Waiting for the part of the URL to match the expected. Useful for SPA to understand that page was changed.
2
+
3
+ ```js
4
+ I.waitInUrl('/info', 2);
5
+ ```
6
+
7
+ @param {string} urlPart value to check.
8
+ @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
9
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,10 @@
1
+ Waits for a specified number of elements on the page.
2
+
3
+ ```js
4
+ I.waitNumberOfVisibleElements('a', 3);
5
+ ```
6
+
7
+ @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
8
+ @param {number} num number of elements.
9
+ @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
10
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,10 @@
1
+ Waits for an element to hide (by default waits for 1sec).
2
+ Element can be located by CSS or XPath.
3
+
4
+ ```js
5
+ I.waitToHide('#popup');
6
+ ```
7
+
8
+ @param {CodeceptJS.LocatorOrString} locator element located by CSS|XPath|strict locator.
9
+ @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
10
+ @returns {void} automatically synchronized promise through #recorder
@@ -0,0 +1,10 @@
1
+ Waits for the entire URL to match the expected
2
+
3
+ ```js
4
+ I.waitUrlEquals('/info', 2);
5
+ I.waitUrlEquals('http://127.0.0.1:8000/info');
6
+ ```
7
+
8
+ @param {string} urlPart value to check.
9
+ @param {number} [sec=1] (optional, `1` by default) time in seconds to wait
10
+ @returns {void} automatically synchronized promise through #recorder
@@ -2,6 +2,7 @@ let webdriverio;
2
2
 
3
3
  const fs = require('fs');
4
4
  const axios = require('axios').default;
5
+ const { v4: uuidv4 } = require('uuid');
5
6
 
6
7
  const Webdriver = require('./WebDriver');
7
8
  const AssertionFailedError = require('../assert/error');
@@ -1088,17 +1089,40 @@ class Appium extends Webdriver {
1088
1089
  * Appium: support Android and iOS
1089
1090
  */
1090
1091
  async performSwipe(from, to) {
1091
- await this.browser.touchPerform([{
1092
- action: 'press',
1093
- options: from,
1094
- }, {
1095
- action: 'wait',
1096
- options: { ms: 1000 },
1097
- }, {
1098
- action: 'moveTo',
1099
- options: to,
1100
- }, {
1101
- action: 'release',
1092
+ await this.browser.performActions([{
1093
+ id: uuidv4(),
1094
+ type: 'pointer',
1095
+ parameters: {
1096
+ pointerType: 'touch',
1097
+ },
1098
+ actions: [
1099
+ {
1100
+ duration: 0,
1101
+ x: from.x,
1102
+ y: from.y,
1103
+ type: 'pointerMove',
1104
+ origin: 'viewport',
1105
+ },
1106
+ {
1107
+ button: 1,
1108
+ type: 'pointerDown',
1109
+ },
1110
+ {
1111
+ duration: 200,
1112
+ type: 'pause',
1113
+ },
1114
+ {
1115
+ duration: 600,
1116
+ x: to.x,
1117
+ y: to.y,
1118
+ type: 'pointerMove',
1119
+ origin: 'viewport',
1120
+ },
1121
+ {
1122
+ button: 1,
1123
+ type: 'pointerUp',
1124
+ },
1125
+ ],
1102
1126
  }]);
1103
1127
  await this.browser.pause(1000);
1104
1128
  }
@@ -1128,7 +1152,7 @@ class Appium extends Webdriver {
1128
1152
  yoffset = 100;
1129
1153
  }
1130
1154
 
1131
- return this.swipe(locator, 0, yoffset, speed);
1155
+ return this.swipe(parseLocator.call(this, locator), 0, yoffset, speed);
1132
1156
  }
1133
1157
 
1134
1158
  /**
@@ -1,12 +1,15 @@
1
- const chai = require('chai');
2
1
  const output = require('../output');
3
2
 
4
- const { expect } = chai;
3
+ let expect;
5
4
 
6
- chai.use(require('chai-string'));
7
- // @ts-ignore
8
- chai.use(require('chai-exclude'));
9
- chai.use(require('chai-match-pattern'));
5
+ import('chai').then(chai => {
6
+ expect = chai.expect;
7
+ chai.use(require('chai-string'));
8
+ // @ts-ignore
9
+ chai.use(require('chai-exclude'));
10
+ chai.use(require('chai-match-pattern'));
11
+ chai.use(require('chai-json-schema'));
12
+ });
10
13
 
11
14
  /**
12
15
  * This helper allows performing assertions based on Chai.
@@ -179,7 +182,7 @@ class ExpectHelper {
179
182
  expectJsonSchema(targetData, jsonSchema, customErrorMsg = '') {
180
183
  // @ts-ignore
181
184
  output.step(`I expect "${JSON.stringify(targetData)}" to match this JSON schema "${JSON.stringify(jsonSchema)}"`);
182
- chai.use(require('chai-json-schema'));
185
+
183
186
  return expect(targetData, customErrorMsg).to.be.jsonSchema(jsonSchema);
184
187
  }
185
188
 
@@ -1,12 +1,13 @@
1
- const assert = require('assert');
2
- const chai = require('chai');
3
- const joi = require('joi');
4
- const chaiDeepMatch = require('chai-deep-match');
5
1
  const Helper = require('@codeceptjs/helper');
6
2
 
7
- chai.use(chaiDeepMatch);
3
+ let expect;
4
+
5
+ import('chai').then(chai => {
6
+ expect = chai.expect;
7
+ chai.use(require('chai-deep-match'));
8
+ });
8
9
 
9
- const { expect } = chai;
10
+ const joi = require('joi');
10
11
 
11
12
  /**
12
13
  * This helper allows performing assertions on JSON responses paired with following helpers:
@@ -91,7 +92,6 @@ class JSONResponse extends Helper {
91
92
  static _checkRequirements() {
92
93
  try {
93
94
  require('joi');
94
- require('chai');
95
95
  } catch (e) {
96
96
  return ['joi'];
97
97
  }
@@ -194,7 +194,7 @@ class JSONResponse extends Helper {
194
194
  fails++;
195
195
  }
196
196
  }
197
- assert.ok(fails < this.response.data.length, `No elements in array matched ${JSON.stringify(json)}`);
197
+ expect(fails < this.response.data.length, `No elements in array matched ${JSON.stringify(json)}`).to.be.true;
198
198
  } else {
199
199
  expect(this.response.data).to.deep.match(json);
200
200
  }
@@ -0,0 +1,221 @@
1
+ const { mock, settings } = require('pactum');
2
+
3
+ /**
4
+ * ## Configuration
5
+ *
6
+ * This helper should be configured in codecept.conf.(js|ts)
7
+ *
8
+ * @typedef MockServerConfig
9
+ * @type {object}
10
+ * @prop {number} [port=9393] - Mock server port
11
+ * @prop {string} [host="0.0.0.0"] - Mock server host
12
+ * @prop {object} [httpsOpts] - key & cert values are the paths to .key and .crt files
13
+ */
14
+ let config = {
15
+ port: 9393,
16
+ host: '0.0.0.0',
17
+ httpsOpts: {
18
+ key: '',
19
+ cert: '',
20
+ },
21
+ };
22
+
23
+ /**
24
+ * MockServer
25
+ *
26
+ * The MockServer Helper in CodeceptJS empowers you to mock any server or service via HTTP or HTTPS, making it an excellent tool for simulating REST endpoints and other HTTP-based APIs.
27
+ *
28
+ * <!-- configuration -->
29
+ *
30
+ * #### Examples
31
+ *
32
+ * You can seamlessly integrate MockServer with other helpers like REST or Playwright. Here's a configuration example inside the `codecept.conf.js` file:
33
+ *
34
+ * ```javascript
35
+ * {
36
+ * helpers: {
37
+ * REST: {...},
38
+ * MockServer: {
39
+ * // default mock server config
40
+ * port: 9393,
41
+ * host: '0.0.0.0',
42
+ * httpsOpts: {
43
+ * key: '',
44
+ * cert: '',
45
+ * },
46
+ * },
47
+ * }
48
+ * }
49
+ * ```
50
+ *
51
+ * #### Adding Interactions
52
+ *
53
+ * Interactions add behavior to the mock server. Use the `I.addInteractionToMockServer()` method to include interactions. It takes an interaction object as an argument, containing request and response details.
54
+ *
55
+ * ```javascript
56
+ * I.addInteractionToMockServer({
57
+ * request: {
58
+ * method: 'GET',
59
+ * path: '/api/hello'
60
+ * },
61
+ * response: {
62
+ * status: 200,
63
+ * body: {
64
+ * 'say': 'hello to mock server'
65
+ * }
66
+ * }
67
+ * });
68
+ * ```
69
+ *
70
+ * #### Request Matching
71
+ *
72
+ * When a real request is sent to the mock server, it matches the received request with the interactions. If a match is found, it returns the specified response; otherwise, a 404 status code is returned.
73
+ *
74
+ * - Strong match on HTTP Method, Path, Query Params & JSON body.
75
+ * - Loose match on Headers.
76
+ *
77
+ * ##### Strong Match on Query Params
78
+ *
79
+ * You can send different responses based on query parameters:
80
+ *
81
+ * ```javascript
82
+ * I.addInteractionToMockServer({
83
+ * request: {
84
+ * method: 'GET',
85
+ * path: '/api/users',
86
+ * queryParams: {
87
+ * id: 1
88
+ * }
89
+ * },
90
+ * response: {
91
+ * status: 200,
92
+ * body: 'user 1'
93
+ * }
94
+ * });
95
+ *
96
+ * I.addInteractionToMockServer({
97
+ * request: {
98
+ * method: 'GET',
99
+ * path: '/api/users',
100
+ * queryParams: {
101
+ * id: 2
102
+ * }
103
+ * },
104
+ * response: {
105
+ * status: 200,
106
+ * body: 'user 2'
107
+ * }
108
+ * });
109
+ * ```
110
+ *
111
+ * - GET to `/api/users?id=1` will return 'user 1'.
112
+ * - GET to `/api/users?id=2` will return 'user 2'.
113
+ * - For all other requests, it returns a 404 status code.
114
+ *
115
+ * ##### Loose Match on Body
116
+ *
117
+ * When `strict` is set to false, it performs a loose match on query params and response body:
118
+ *
119
+ * ```javascript
120
+ * I.addInteractionToMockServer({
121
+ * strict: false,
122
+ * request: {
123
+ * method: 'POST',
124
+ * path: '/api/users',
125
+ * body: {
126
+ * name: 'john'
127
+ * }
128
+ * },
129
+ * response: {
130
+ * status: 200
131
+ * }
132
+ * });
133
+ * ```
134
+ *
135
+ * - POST to `/api/users` with the body containing `name` as 'john' will return a 200 status code.
136
+ * - POST to `/api/users` without the `name` property in the body will return a 404 status code.
137
+ *
138
+ * Happy testing with MockServer in CodeceptJS! 🚀
139
+ *
140
+ * ## Methods
141
+ */
142
+ class MockServer {
143
+ constructor(passedConfig) {
144
+ settings.setLogLevel('SILENT');
145
+ config = { ...passedConfig };
146
+ if (global.debugMode) {
147
+ settings.setLogLevel('VERBOSE');
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Start the mock server
153
+ * @param {number} [port] start the mock server with given port
154
+ *
155
+ * @returns void
156
+ */
157
+ async startMockServer(port) {
158
+ const _config = { ...config };
159
+ if (port) _config.port = port;
160
+ await mock.setDefaults(_config);
161
+ await mock.start();
162
+ }
163
+
164
+ /**
165
+ * Stop the mock server
166
+ *
167
+ * @returns void
168
+ *
169
+ */
170
+ async stopMockServer() {
171
+ await mock.stop();
172
+ }
173
+
174
+ /**
175
+ * An interaction adds behavior to the mock server
176
+ *
177
+ *
178
+ * ```js
179
+ * I.addInteractionToMockServer({
180
+ * request: {
181
+ * method: 'GET',
182
+ * path: '/api/hello'
183
+ * },
184
+ * response: {
185
+ * status: 200,
186
+ * body: {
187
+ * 'say': 'hello to mock server'
188
+ * }
189
+ * }
190
+ * });
191
+ * ```
192
+ * ```js
193
+ * // with query params
194
+ * I.addInteractionToMockServer({
195
+ * request: {
196
+ * method: 'GET',
197
+ * path: '/api/hello',
198
+ * queryParams: {
199
+ * id: 2
200
+ * }
201
+ * },
202
+ * response: {
203
+ * status: 200,
204
+ * body: {
205
+ * 'say': 'hello to mock server'
206
+ * }
207
+ * }
208
+ * });
209
+ * ```
210
+ *
211
+ * @param {CodeceptJS.MockInteraction|object} interaction add behavior to the mock server
212
+ *
213
+ * @returns void
214
+ *
215
+ */
216
+ async addInteractionToMockServer(interaction) {
217
+ await mock.addInteraction(interaction);
218
+ }
219
+ }
220
+
221
+ module.exports = MockServer;
@@ -4,6 +4,7 @@ const fs = require('fs');
4
4
  const Helper = require('@codeceptjs/helper');
5
5
  const { v4: uuidv4 } = require('uuid');
6
6
  const assert = require('assert');
7
+ const promiseRetry = require('promise-retry');
7
8
  const Locator = require('../locator');
8
9
  const store = require('../store');
9
10
  const recorder = require('../recorder');
@@ -50,6 +51,7 @@ const { createValueEngine, createDisabledEngine } = require('./extras/Playwright
50
51
  const {
51
52
  seeElementError, dontSeeElementError, dontSeeElementInDOMError, seeElementInDOMError,
52
53
  } = require('./errors/ElementAssertion');
54
+ const { log } = require('../output');
53
55
 
54
56
  const pathSeparator = path.sep;
55
57
 
@@ -748,9 +750,7 @@ class Playwright extends Helper {
748
750
  });
749
751
  this.context = await this.page;
750
752
  this.contextLocator = null;
751
- if (this.options.browser === 'chrome') {
752
- await page.bringToFront();
753
- }
753
+ await page.bringToFront();
754
754
  }
755
755
 
756
756
  /**
@@ -1805,7 +1805,7 @@ class Playwright extends Helper {
1805
1805
  let optionToSelect = '';
1806
1806
 
1807
1807
  try {
1808
- optionToSelect = await el.locator('option', { hasText: option }).textContent();
1808
+ optionToSelect = (await el.locator('option', { hasText: option }).textContent()).trim();
1809
1809
  } catch (e) {
1810
1810
  optionToSelect = option;
1811
1811
  }
@@ -2890,6 +2890,38 @@ class Playwright extends Helper {
2890
2890
  }
2891
2891
  }
2892
2892
 
2893
+ /**
2894
+ * {{> waitForCookie }}
2895
+ */
2896
+ async waitForCookie(name, sec) {
2897
+ // by default, we will retry 3 times
2898
+ let retries = 3;
2899
+ const waitTimeout = sec ? sec * 1000 : this.options.waitForTimeout;
2900
+
2901
+ if (sec) {
2902
+ retries = sec;
2903
+ } else {
2904
+ retries = Math.ceil(waitTimeout / 1000) - 1;
2905
+ }
2906
+
2907
+ return promiseRetry(async (retry, number) => {
2908
+ const _grabCookie = async (name) => {
2909
+ const cookies = await this.browserContext.cookies();
2910
+ const cookie = cookies.filter(c => c.name === name);
2911
+ if (cookie.length === 0) throw Error(`Cookie ${name} is not found after ${retries}s`);
2912
+ };
2913
+
2914
+ this.debugSection('Wait for cookie: ', name);
2915
+ if (number > 1) this.debugSection('Retrying... Attempt #', number);
2916
+
2917
+ try {
2918
+ await _grabCookie(name);
2919
+ } catch (e) {
2920
+ retry(e);
2921
+ }
2922
+ }, { retries, maxTimeout: 1000 });
2923
+ }
2924
+
2893
2925
  async _waitForAction() {
2894
2926
  return this.wait(this.options.waitForAction / 1000);
2895
2927
  }