codeceptjs 3.5.12-beta.1 → 3.5.12-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 (213) hide show
  1. package/docs/advanced.md +351 -0
  2. package/docs/ai.md +248 -0
  3. package/docs/api.md +323 -0
  4. package/docs/basics.md +979 -0
  5. package/docs/bdd.md +539 -0
  6. package/docs/best.md +237 -0
  7. package/docs/books.md +37 -0
  8. package/docs/bootstrap.md +135 -0
  9. package/docs/build/ApiDataFactory.js +410 -0
  10. package/docs/build/Appium.js +2027 -0
  11. package/docs/build/Expect.js +422 -0
  12. package/docs/build/FileSystem.js +228 -0
  13. package/docs/build/GraphQL.js +229 -0
  14. package/docs/build/GraphQLDataFactory.js +309 -0
  15. package/docs/build/JSONResponse.js +338 -0
  16. package/docs/build/Mochawesome.js +71 -0
  17. package/docs/build/Nightmare.js +2152 -0
  18. package/docs/build/OpenAI.js +126 -0
  19. package/docs/build/Playwright.js +5110 -0
  20. package/docs/build/Protractor.js +2706 -0
  21. package/docs/build/Puppeteer.js +3905 -0
  22. package/docs/build/REST.js +344 -0
  23. package/docs/build/TestCafe.js +2125 -0
  24. package/docs/build/WebDriver.js +4240 -0
  25. package/docs/changelog.md +2572 -0
  26. package/docs/commands.md +266 -0
  27. package/docs/community-helpers.md +58 -0
  28. package/docs/configuration.md +157 -0
  29. package/docs/continuous-integration.md +22 -0
  30. package/docs/custom-helpers.md +306 -0
  31. package/docs/data.md +379 -0
  32. package/docs/detox.md +235 -0
  33. package/docs/docker.md +136 -0
  34. package/docs/email.md +183 -0
  35. package/docs/examples.md +149 -0
  36. package/docs/helpers/ApiDataFactory.md +266 -0
  37. package/docs/helpers/Appium.md +1374 -0
  38. package/docs/helpers/Detox.md +586 -0
  39. package/docs/helpers/Expect.md +275 -0
  40. package/docs/helpers/FileSystem.md +152 -0
  41. package/docs/helpers/GraphQL.md +151 -0
  42. package/docs/helpers/GraphQLDataFactory.md +226 -0
  43. package/docs/helpers/JSONResponse.md +254 -0
  44. package/docs/helpers/Mochawesome.md +8 -0
  45. package/docs/helpers/MockRequest.md +377 -0
  46. package/docs/helpers/Nightmare.md +1305 -0
  47. package/docs/helpers/OpenAI.md +70 -0
  48. package/docs/helpers/Playwright.md +2759 -0
  49. package/docs/helpers/Polly.md +44 -0
  50. package/docs/helpers/Protractor.md +1769 -0
  51. package/docs/helpers/Puppeteer-firefox.md +86 -0
  52. package/docs/helpers/Puppeteer.md +2317 -0
  53. package/docs/helpers/REST.md +218 -0
  54. package/docs/helpers/TestCafe.md +1321 -0
  55. package/docs/helpers/WebDriver.md +2547 -0
  56. package/docs/hooks.md +340 -0
  57. package/docs/index.md +111 -0
  58. package/docs/installation.md +75 -0
  59. package/docs/internal-api.md +266 -0
  60. package/docs/locators.md +339 -0
  61. package/docs/mobile-react-native-locators.md +67 -0
  62. package/docs/mobile.md +338 -0
  63. package/docs/pageobjects.md +291 -0
  64. package/docs/parallel.md +400 -0
  65. package/docs/playwright.md +632 -0
  66. package/docs/plugins.md +1259 -0
  67. package/docs/puppeteer.md +316 -0
  68. package/docs/quickstart.md +162 -0
  69. package/docs/react.md +70 -0
  70. package/docs/reports.md +392 -0
  71. package/docs/secrets.md +36 -0
  72. package/docs/shadow.md +68 -0
  73. package/docs/shared/keys.mustache +31 -0
  74. package/docs/shared/react.mustache +1 -0
  75. package/docs/testcafe.md +174 -0
  76. package/docs/translation.md +247 -0
  77. package/docs/tutorial.md +271 -0
  78. package/docs/typescript.md +180 -0
  79. package/docs/ui.md +59 -0
  80. package/docs/videos.md +28 -0
  81. package/docs/visual.md +202 -0
  82. package/docs/vue.md +143 -0
  83. package/docs/webapi/amOnPage.mustache +11 -0
  84. package/docs/webapi/appendField.mustache +11 -0
  85. package/docs/webapi/attachFile.mustache +12 -0
  86. package/docs/webapi/blur.mustache +18 -0
  87. package/docs/webapi/checkOption.mustache +13 -0
  88. package/docs/webapi/clearCookie.mustache +9 -0
  89. package/docs/webapi/clearField.mustache +9 -0
  90. package/docs/webapi/click.mustache +25 -0
  91. package/docs/webapi/clickLink.mustache +8 -0
  92. package/docs/webapi/closeCurrentTab.mustache +7 -0
  93. package/docs/webapi/closeOtherTabs.mustache +8 -0
  94. package/docs/webapi/dontSee.mustache +11 -0
  95. package/docs/webapi/dontSeeCheckboxIsChecked.mustache +10 -0
  96. package/docs/webapi/dontSeeCookie.mustache +8 -0
  97. package/docs/webapi/dontSeeCurrentUrlEquals.mustache +10 -0
  98. package/docs/webapi/dontSeeElement.mustache +8 -0
  99. package/docs/webapi/dontSeeElementInDOM.mustache +8 -0
  100. package/docs/webapi/dontSeeInCurrentUrl.mustache +4 -0
  101. package/docs/webapi/dontSeeInField.mustache +11 -0
  102. package/docs/webapi/dontSeeInSource.mustache +8 -0
  103. package/docs/webapi/dontSeeInTitle.mustache +8 -0
  104. package/docs/webapi/doubleClick.mustache +13 -0
  105. package/docs/webapi/downloadFile.mustache +12 -0
  106. package/docs/webapi/dragAndDrop.mustache +9 -0
  107. package/docs/webapi/dragSlider.mustache +11 -0
  108. package/docs/webapi/executeAsyncScript.mustache +24 -0
  109. package/docs/webapi/executeScript.mustache +26 -0
  110. package/docs/webapi/fillField.mustache +16 -0
  111. package/docs/webapi/focus.mustache +13 -0
  112. package/docs/webapi/forceClick.mustache +28 -0
  113. package/docs/webapi/forceRightClick.mustache +18 -0
  114. package/docs/webapi/grabAllWindowHandles.mustache +7 -0
  115. package/docs/webapi/grabAttributeFrom.mustache +10 -0
  116. package/docs/webapi/grabAttributeFromAll.mustache +9 -0
  117. package/docs/webapi/grabBrowserLogs.mustache +9 -0
  118. package/docs/webapi/grabCookie.mustache +11 -0
  119. package/docs/webapi/grabCssPropertyFrom.mustache +11 -0
  120. package/docs/webapi/grabCssPropertyFromAll.mustache +10 -0
  121. package/docs/webapi/grabCurrentUrl.mustache +9 -0
  122. package/docs/webapi/grabCurrentWindowHandle.mustache +6 -0
  123. package/docs/webapi/grabDataFromPerformanceTiming.mustache +20 -0
  124. package/docs/webapi/grabElementBoundingRect.mustache +20 -0
  125. package/docs/webapi/grabGeoLocation.mustache +8 -0
  126. package/docs/webapi/grabHTMLFrom.mustache +10 -0
  127. package/docs/webapi/grabHTMLFromAll.mustache +9 -0
  128. package/docs/webapi/grabNumberOfOpenTabs.mustache +8 -0
  129. package/docs/webapi/grabNumberOfVisibleElements.mustache +9 -0
  130. package/docs/webapi/grabPageScrollPosition.mustache +8 -0
  131. package/docs/webapi/grabPopupText.mustache +5 -0
  132. package/docs/webapi/grabSource.mustache +8 -0
  133. package/docs/webapi/grabTextFrom.mustache +10 -0
  134. package/docs/webapi/grabTextFromAll.mustache +9 -0
  135. package/docs/webapi/grabTitle.mustache +8 -0
  136. package/docs/webapi/grabValueFrom.mustache +9 -0
  137. package/docs/webapi/grabValueFromAll.mustache +8 -0
  138. package/docs/webapi/grabWebElement.mustache +9 -0
  139. package/docs/webapi/grabWebElements.mustache +9 -0
  140. package/docs/webapi/moveCursorTo.mustache +12 -0
  141. package/docs/webapi/openNewTab.mustache +7 -0
  142. package/docs/webapi/pressKey.mustache +12 -0
  143. package/docs/webapi/pressKeyDown.mustache +12 -0
  144. package/docs/webapi/pressKeyUp.mustache +12 -0
  145. package/docs/webapi/pressKeyWithKeyNormalization.mustache +60 -0
  146. package/docs/webapi/refreshPage.mustache +6 -0
  147. package/docs/webapi/resizeWindow.mustache +6 -0
  148. package/docs/webapi/rightClick.mustache +14 -0
  149. package/docs/webapi/saveElementScreenshot.mustache +10 -0
  150. package/docs/webapi/saveScreenshot.mustache +12 -0
  151. package/docs/webapi/say.mustache +10 -0
  152. package/docs/webapi/scrollIntoView.mustache +11 -0
  153. package/docs/webapi/scrollPageToBottom.mustache +6 -0
  154. package/docs/webapi/scrollPageToTop.mustache +6 -0
  155. package/docs/webapi/scrollTo.mustache +12 -0
  156. package/docs/webapi/see.mustache +11 -0
  157. package/docs/webapi/seeAttributesOnElements.mustache +9 -0
  158. package/docs/webapi/seeCheckboxIsChecked.mustache +10 -0
  159. package/docs/webapi/seeCookie.mustache +8 -0
  160. package/docs/webapi/seeCssPropertiesOnElements.mustache +9 -0
  161. package/docs/webapi/seeCurrentUrlEquals.mustache +11 -0
  162. package/docs/webapi/seeElement.mustache +8 -0
  163. package/docs/webapi/seeElementInDOM.mustache +8 -0
  164. package/docs/webapi/seeInCurrentUrl.mustache +8 -0
  165. package/docs/webapi/seeInField.mustache +12 -0
  166. package/docs/webapi/seeInPopup.mustache +8 -0
  167. package/docs/webapi/seeInSource.mustache +7 -0
  168. package/docs/webapi/seeInTitle.mustache +8 -0
  169. package/docs/webapi/seeNumberOfElements.mustache +11 -0
  170. package/docs/webapi/seeNumberOfVisibleElements.mustache +10 -0
  171. package/docs/webapi/seeTextEquals.mustache +9 -0
  172. package/docs/webapi/seeTitleEquals.mustache +8 -0
  173. package/docs/webapi/selectOption.mustache +21 -0
  174. package/docs/webapi/setCookie.mustache +16 -0
  175. package/docs/webapi/setGeoLocation.mustache +12 -0
  176. package/docs/webapi/switchTo.mustache +9 -0
  177. package/docs/webapi/switchToNextTab.mustache +10 -0
  178. package/docs/webapi/switchToPreviousTab.mustache +10 -0
  179. package/docs/webapi/type.mustache +21 -0
  180. package/docs/webapi/uncheckOption.mustache +13 -0
  181. package/docs/webapi/wait.mustache +8 -0
  182. package/docs/webapi/waitForClickable.mustache +11 -0
  183. package/docs/webapi/waitForDetached.mustache +10 -0
  184. package/docs/webapi/waitForElement.mustache +11 -0
  185. package/docs/webapi/waitForEnabled.mustache +6 -0
  186. package/docs/webapi/waitForFunction.mustache +17 -0
  187. package/docs/webapi/waitForInvisible.mustache +10 -0
  188. package/docs/webapi/waitForNumberOfTabs.mustache +9 -0
  189. package/docs/webapi/waitForText.mustache +13 -0
  190. package/docs/webapi/waitForValue.mustache +10 -0
  191. package/docs/webapi/waitForVisible.mustache +10 -0
  192. package/docs/webapi/waitInUrl.mustache +9 -0
  193. package/docs/webapi/waitNumberOfVisibleElements.mustache +10 -0
  194. package/docs/webapi/waitToHide.mustache +10 -0
  195. package/docs/webapi/waitUrlEquals.mustache +10 -0
  196. package/docs/webdriver.md +701 -0
  197. package/docs/wiki/Books-&-Posts.md +27 -0
  198. package/docs/wiki/Community-Helpers-&-Plugins.md +53 -0
  199. package/docs/wiki/Converting-Playwright-to-Istanbul-Coverage.md +61 -0
  200. package/docs/wiki/Examples.md +145 -0
  201. package/docs/wiki/Google-Summer-of-Code-(GSoC)-2020.md +68 -0
  202. package/docs/wiki/Home.md +16 -0
  203. package/docs/wiki/Migration-to-Appium-v2---CodeceptJS.md +83 -0
  204. package/docs/wiki/Release-Process.md +24 -0
  205. package/docs/wiki/Roadmap.md +23 -0
  206. package/docs/wiki/Tests.md +1393 -0
  207. package/docs/wiki/Upgrading-to-CodeceptJS-3.md +153 -0
  208. package/docs/wiki/Videos.md +19 -0
  209. package/lib/css2xpath/js/css_to_xpath.js +20 -0
  210. package/lib/css2xpath/js/expression.js +23 -0
  211. package/lib/css2xpath/js/renderer.js +239 -0
  212. package/lib/locator.js +15 -1
  213. package/package.json +12 -9
@@ -0,0 +1,351 @@
1
+ ---
2
+ permalink: /advanced
3
+ title: Advanced Usage
4
+ ---
5
+
6
+ # Advanced Usage
7
+
8
+ ## Data Driven Tests
9
+
10
+ Execute the same scenario on a different data set.
11
+
12
+ Let's say you want to test login for different user accounts.
13
+ In this case, you need to create a datatable and fill it in with credentials.
14
+ Then use `Data().Scenario` to include this data and generate multiple scenarios:
15
+
16
+ ```js
17
+ // Define data table inside a test or load from another module
18
+ let accounts = new DataTable(['login', 'password']); //
19
+ accounts.add(['davert', '123456']); // adding records to a table
20
+ accounts.add(['admin', '123456']);
21
+
22
+ // You can skip some data. But add them to report as skipped (just like with usual scenarios):
23
+ accounts.xadd(['admin', '23456'])
24
+
25
+ // Pass dataTable to Data()
26
+ // Use special param `current` to get current data set
27
+ Data(accounts).Scenario('Test Login', ({ I, current }) => {
28
+ I.fillField('Username', current.login); // current is reserved!
29
+ I.fillField('Password', current.password);
30
+ I.click('Sign In');
31
+ I.see('Welcome '+ current.login);
32
+ });
33
+
34
+
35
+ // Also you can set only for Data tests. It will launch executes only the current test but with all data options
36
+ Data(accounts).only.Scenario('Test Login', ({ I, current }) => {
37
+ I.fillField('Username', current.login); // current is reserved!
38
+ I.fillField('Password', current.password);
39
+ I.click('Sign In');
40
+ I.see('Welcome '+ current.login);
41
+ });
42
+ ```
43
+
44
+ *Important: you can't use name `current` for pageObjects or helpers in data scenarios*
45
+
46
+ This will produce 2 tests with different data sets.
47
+ Current data set is appended to a test name in output:
48
+
49
+ ```sh
50
+ ✓ Test Login | {"login":"davert","password":"123456"}
51
+ ✓ Test Login | {"login":"admin","password":"123456"}
52
+ S Test Login | {"login":"admin","password":"23456"}
53
+ ```
54
+
55
+ ```js
56
+ // You can filter your data table
57
+ Data(accounts.filter(account => account.login == 'admin')
58
+ .Scenario('Test Login', ({ I, current }) => {
59
+ I.fillField('Username', current.login);
60
+ I.fillField('Password', current.password);
61
+ I.click('Sign In');
62
+ I.see('Welcome '+ current.login);
63
+ });
64
+ ```
65
+
66
+ This will limit data sets accoring passed function:
67
+
68
+ ```sh
69
+ ✓ Test Login | {"login":"admin","password":"123456"}
70
+ S Test Login | {"login":"admin","password":"23456"}
71
+ ```
72
+
73
+ Data sets can also be defined with array, generator, or a function.
74
+
75
+ ```js
76
+ Data(function*() {
77
+ yield { user: 'davert'};
78
+ yield { user: 'andrey'};
79
+ }).Scenario() // ...
80
+ ```
81
+
82
+ *HINT: If you don't use DataTable. add `toString()` method to each object added to data set, so the data could be pretty printed in a test name*
83
+
84
+ ## Tags
85
+
86
+ Append `@tag` to your test name, so
87
+
88
+ ```js
89
+ Scenario('update user profile @slow')
90
+ ```
91
+
92
+ Alternativly, use `tag` method of Scenario to set additional tags:
93
+
94
+ ```js
95
+ Scenario('update user profile', ({ }) => {
96
+ // test goes here
97
+ }).tag('@slow').tag('important');
98
+ ```
99
+
100
+ All tests with `@tag` could be executed with `--grep '@tag'` option.
101
+
102
+ ```sh
103
+ codeceptjs run --grep '@slow'
104
+ ```
105
+
106
+ Use regex for more flexible filtering:
107
+
108
+ * `--grep '(?=.*@smoke2)(?=.*@smoke3)'` - run tests with @smoke2 and @smoke3 in name
109
+ * `--grep "\@smoke2|\@smoke3"` - run tests with @smoke2 or @smoke3 in name
110
+ * `--grep '((?=.*@smoke2)(?=.*@smoke3))|@smoke4'` - run tests with (@smoke2 and @smoke3) or @smoke4 in name
111
+ * `--grep '(?=.*@smoke2)^(?!.*@smoke3)'` - run tests with @smoke2 but without @smoke3 in name
112
+ * `--grep '(?=.*)^(?!.*@smoke4)'` - run all tests except @smoke4
113
+
114
+
115
+
116
+ ## Debug
117
+
118
+ CodeceptJS provides a debug mode in which additional information is printed.
119
+ It can be turned on with `--debug` flag.
120
+
121
+ ```sh
122
+ npx codeceptjs run --debug
123
+ ```
124
+
125
+ to receive even more information turn on `--verbose` flag:
126
+
127
+ ```sh
128
+ npx codeceptjs run --verbose
129
+ ```
130
+
131
+ > You can pause execution and enter **interactive console** mode by calling `pause()` inside your test.
132
+
133
+ To see a complete internal debug of CodeceptJS use `DEBUG` env variable:
134
+
135
+ ```sh
136
+ DEBUG=codeceptjs:* npx codeceptjs run
137
+ ```
138
+
139
+ For an interactive debugging use NodeJS debugger. In **WebStorm**:
140
+
141
+ ```sh
142
+ node $NODE_DEBUG_OPTION ./node_modules/.bin/codeceptjs run
143
+ ```
144
+
145
+ For **Visual Studio Code**, add the following configuration in launch.json:
146
+
147
+ ```json
148
+ {
149
+ "type": "node",
150
+ "request": "launch",
151
+ "name": "codeceptjs",
152
+ "args": ["run", "--grep", "@your_test_tag"],
153
+ "program": "${workspaceFolder}/node_modules/codeceptjs/bin/codecept.js"
154
+ }
155
+ ```
156
+
157
+
158
+ ## Test Options
159
+
160
+ Features and Scenarios have their options that can be set by passing a hash after their names:
161
+
162
+ ```js
163
+ Feature('My feature', {key: val});
164
+
165
+ Scenario('My scenario', {key: val},({ I }) => {});
166
+ ```
167
+
168
+ You can use this options for build your own [plugins](https://codecept.io/hooks/#plugins) with [event listners](https://codecept.io/hooks/#api). Example:
169
+
170
+ ```js
171
+ // for test
172
+ event.dispatcher.on(event.test.before, (test) => {
173
+ ...
174
+ if (test.opts.key) {
175
+ ...
176
+ }
177
+ ...
178
+ });
179
+ // or for suite
180
+ event.dispatcher.on(event.suite.before, (suite) => {
181
+ ...
182
+ if (suite.opts.key) {
183
+ ...
184
+ }
185
+ ...
186
+ });
187
+ ```
188
+
189
+ ## Timeout
190
+
191
+ Tests can get stuck due to various reasons such as network connection issues, crashed browser, etc.
192
+ This can make tests process hang. To prevent these situations timeouts can be used. Timeouts can be set explicitly for flaky parts of code, or implicitly in a config.
193
+
194
+ > Previous timeout implementation was disabled as it had no effect when dealing with steps and promises.
195
+
196
+ ### Steps Timeout
197
+
198
+ It is possible to limit a step execution to specified time with `I.limitTime` command.
199
+ It will set timeout in seconds for the next executed step:
200
+
201
+ ```js
202
+ // limit clicking to 5 seconds
203
+ I.limitTime(5).click('Link')
204
+ ```
205
+
206
+ It is possible to set a timeout for all steps implicitly (except waiters) using [stepTimeout plugin](/plugins/#steptimeout).
207
+
208
+ ### Tests Timeout
209
+
210
+ Test timeout can be set in seconds via Scenario options:
211
+
212
+ ```js
213
+ // limit test to 20 seconds
214
+ Scenario('slow test that should be stopped', { timeout: 20 }, ({ I }) => {
215
+ // ...
216
+ })
217
+ ```
218
+
219
+ This timeout can be set globally in `codecept.conf.js` in seconds:
220
+
221
+ ```js
222
+ exports.config = {
223
+
224
+ // each test must not run longer than 5 mins
225
+ timeout: 300,
226
+
227
+ }
228
+ ```
229
+
230
+ ### Suites Timeout
231
+
232
+ A timeout for a group of tests can be set on Feature level via options.
233
+
234
+ ```js
235
+ // limit all tests in this suite to 30 seconds
236
+ Feature('flaky tests', { timeout: 30 })
237
+ ```
238
+
239
+ ### Timeout Confguration
240
+
241
+ <Badge text="Updated in 3.4" type="warning"/>
242
+
243
+ Timeout rules can be set globally via config.
244
+
245
+ To set a timeout for all running tests provide a **number of seconds** to `timeout` config option:
246
+
247
+
248
+ ```js
249
+ // inside codecept.conf.js or codecept.conf.ts
250
+ timeout: 30, // limit all tests in all suites to 30 secs
251
+ ```
252
+
253
+ It is possible to tune this configuration for a different groups of tests passing options as array and using `grep` option to filter tests:
254
+
255
+ ```js
256
+ // inside codecept.conf.js or codecept.conf.ts
257
+
258
+ timeout: [
259
+ 10, // default timeout is 10secs
260
+
261
+ // but increase timeout for slow tests
262
+ {
263
+ grep: '@slow',
264
+ Feature: 50
265
+ },
266
+ ]
267
+ ```
268
+
269
+ > ℹ️ `grep` value can be string or regexp
270
+
271
+ It is possible to set a timeout for Scenario or Feature:
272
+
273
+ ```js
274
+ // inside codecept.conf.js or codecept.conf.ts
275
+ timeout: [
276
+
277
+ // timeout for Feature with @slow in title
278
+ {
279
+ grep: '@slow',
280
+ Feature: 50
281
+ },
282
+
283
+ // timeout for Scenario with 'flaky0' .. `flaky1` in title
284
+ {
285
+ // regexp can be passed to grep
286
+ grep: /flaky[0-9]/,
287
+ Scenario: 10
288
+ },
289
+
290
+ // timeout for all suites
291
+ {
292
+ Feature: 20
293
+ }
294
+ ]
295
+ ```
296
+
297
+ Global timeouts will be overridden by explicit timeouts of a test or steps.
298
+
299
+ ### Disable Timeouts
300
+
301
+ To execute tests ignoring all timeout settings use `--no-timeouts` option:
302
+
303
+ ```
304
+ npx codeceptjs run --no-timeouts
305
+ ```
306
+
307
+ ## Dynamic Configuration
308
+
309
+ Helpers can be reconfigured per scenario or per feature.
310
+ This might be useful when some tests should be executed with different settings than others.
311
+ In order to reconfigure tests use `.config()` method of `Scenario` or `Feature`.
312
+
313
+ ```js
314
+ Scenario('should be executed in firefox', ({ I }) => {
315
+ // I.amOnPage(..)
316
+ }).config({ browser: 'firefox' })
317
+ ```
318
+
319
+ In this case `config` overrides current config of the first helper.
320
+ To change config of specific helper pass two arguments: helper name and config values:
321
+
322
+ ```js
323
+ Scenario('should create data via v2 version of API', ({ I }) => {
324
+ // I.amOnPage(..)
325
+ }).config('REST', { endpoint: 'https://api.mysite.com/v2' })
326
+ ```
327
+
328
+ Config can also be set by a function, in this case you can get a test object and specify config values based on it.
329
+ This is very useful when running tests against cloud providers, like BrowserStack. This function can also be asynchronous.
330
+
331
+ ```js
332
+ Scenario('should report to BrowserStack', ({ I }) => {
333
+ // I.amOnPage(..)
334
+ }).config((test) => {
335
+ return { desiredCapabilities: {
336
+ project: test.suite.title,
337
+ name: test.title,
338
+ }}
339
+ });
340
+ ```
341
+
342
+ Config changes can be applied to all tests in suite:
343
+
344
+ ```js
345
+ Feature('Admin Panel').config({ url: 'https://mysite.com/admin' });
346
+ ```
347
+
348
+ Please note that some config changes can't be applied on the fly. For instance, if you set `restart: false` in your config and then changing value `browser` won't take an effect as browser is already started and won't be closed untill all tests finish.
349
+
350
+ Configuration changes will be reverted after a test or a suite.
351
+
package/docs/ai.md ADDED
@@ -0,0 +1,248 @@
1
+ ---
2
+ permalink: /ai
3
+ title: Testing with AI 🪄
4
+ ---
5
+
6
+ # 🪄 Testing with AI
7
+
8
+ **CodeceptJS is the first open-source test automation framework with AI** features to improve the testing experience. CodeceptJS uses OpenAI GPT to auto-heal failing tests, assist in writing tests, and more...
9
+
10
+ Think of it as your testing co-pilot built into the testing framework
11
+
12
+ > 🪄 **AI features for testing are experimental**. AI works only for web based testing with Playwright, WebDriver, etc. Those features will be improved based on user's experience.
13
+
14
+
15
+ ## How AI Improves Automated Testing
16
+
17
+ ChatGPT can write automated tests for you. What you need is just ask it "How to write CodeceptJS test" and it will generate the code that can be executed but not the actual code you need. ChatGPT misses the context, of your application.
18
+
19
+ CodeceptJS uses OpenAI API to send the prompt and share the HTML context of a page. So, it can ask: how to write a test for **this** page. GPT model knows how to write CodeceptJS code, how to build good-looking semantic locators and how to analyze HTML to match them. Even more, GPT suggestions can be tested in real-time in a browser, making a feedback loop.
20
+
21
+ CodeceptJS AI can do the following:
22
+
23
+ * 🏋️‍♀️ **assist writing tests** in `pause()` or interactive shell mode
24
+ * 🚑 **self-heal failing tests** (can be used on CI)
25
+ * 💬 send arbitrary prompts to GPT from any tested page attaching its HTML contents
26
+
27
+ ![](/img/fill_form.gif)
28
+
29
+ ### How it works
30
+
31
+ As we can't send a browser window with ChatGPT we are not be able to fully share the context. As only text information is accepted, we can send HTML of the currently tested page. GPT doesn't know what elements are hidden, or what elements are visible. It can operate only on the HTML provided.
32
+
33
+ GPT models have limits on information passed, and HTML pages can be huge. And some information may be irrelevant for testing. For instance, if you test a reading application, you won't probably need text contents of a book inside your HTML, as they won't be used in locators. That's why CodeceptJS send HTML with **all non-interactive HTML elements removed**. So, only links, buttons, fields, and all elements that are set as a link, button, etc will be used by GPT for analysis. In case you have clickable `<div>` but with no `role="button"` it will be ignored. Also, we minify HTML before sending.
34
+
35
+ Even though, the HTML is still quite big and may exceed the token limit. So we recommend using **gpt-3.5-turbo-16k** model, as it accepts 16K tokens (approx. 50K of HTML text), which should be enough for most web pages. It is possible to strictly limit the size of HTML to not exceed GPT tokens limit.
36
+
37
+ > ❗AI features require sending HTML contents to OpenAI. If you use it in enterprise, ensure that your company requirements match [OpenAI complience](https://openai.com/security). If you work on public web applications this should be ok, as HTML code is genrally available to all web application users.
38
+
39
+
40
+ ### Getting Started
41
+
42
+ [Install CodeceptJS 3.5](/installation):
43
+
44
+ ```
45
+ npx create-codeceptjs .
46
+ ```
47
+ [Obtain API token](https://platform.openai.com/account/api-keys) from OpenAI. Please check out [their pricing](https://openai.com/pricing) first, as unlike ChatGPT using OpenAI API requires a paid account.
48
+
49
+ Add `OPENAI_API_KEY` environment variable with a key when running codeceptjs:
50
+
51
+ ```
52
+ OPENAI_API_KEY=sk-******** npx codeceptjs run
53
+ ```
54
+
55
+ This will enable AI features in CodeceptJS.
56
+
57
+ > When running on CI set `OPENAI_API_KEY` as a secured environment variable
58
+
59
+ ### Writing Tests with AI Copilot
60
+
61
+ If AI features are enabled when using [interactive pause](/basics/#debug) with `pause()` command inside tests:
62
+
63
+ For instance, let's create a test to try ai features via `gt` command:
64
+
65
+ ```
66
+ npx codeceptjs gt
67
+ ```
68
+
69
+ Name a test and write the code. We will use `Scenario.only` instead of Scenario to execute only this exact test.
70
+
71
+ ```js
72
+ Feature('ai');
73
+
74
+ Scenario.only('test ai features', ({ I }) => {
75
+ I.amOnPage('https://getbootstrap.com/docs/5.1/examples/checkout/')
76
+ pause();
77
+ });
78
+ ```
79
+
80
+ Now run the test in debug mode:
81
+
82
+ ```
83
+ OPENAI_API_KEY=sk-******** npx codeceptjs run --debug
84
+ ```
85
+
86
+ When pause mode started you can ask GPT to fill in the fields on this page. Use natural language to describe your request, and provide enough details that AI could operate with it. It is important to include at least a space char in your input, otherwise, CodeceptJS will consider the input to be JavaScript code.
87
+
88
+
89
+ ```
90
+ I.fill checkout form with valid values without submitting it
91
+ ```
92
+
93
+ ![](/img/fill_form_1.png)
94
+
95
+ GPT will generate code and data and CodeceptJS will try to execute its code. If it succeeds, the code will be saved to history and you will be able to copy it to your test.
96
+
97
+ ![](/img/fill_form2.png)
98
+
99
+ This AI copilot works best with long static forms. In the case of complex and dynamic single-page applications, it may not perform as well, as the form may not be present on HTML page yet. For instance, interacting with calendars or inputs with real-time validations (like credit cards) can not yet be performed by AI.
100
+
101
+ Please keep in mind that GPT can't react to page changes and operates with static text only. This is why it is not ready yet to write the test completely. However, if you are new to CodeceptJS and automated testing AI copilot may help you write tests more efficiently.
102
+
103
+ > 👶 Enable AI copilot for junior test automation engineers. It may help them to get started with CodeceptJS and to write good semantic locators.
104
+
105
+ ### Self-Healing Tests
106
+
107
+ In large test suites, the cost of maintaining tests goes exponentially. That's why any effort that can improve the stability of tests pays itself. In CodeceptJS 3.5 we introduced a new [heal plugin](/plugins#heal) that will use AI to automatically fix a failing test.
108
+
109
+ Heal plugin can solve exactly one problem: if a locator of an element has changed, and an action can't be performed, **it matches a new locator, tries a command again, and continues executing a test**. For instance, if the "Sign in" button was renamed to "Login" or changed its class, it will detect a new locator of the button and will retry execution.
110
+
111
+ Heal actions **work only on actions like `click`, `fillField`**, etc, and won't work on assertions, waiters, grabbers, etc. Assertions can't be guessed by AI, the same way as grabbers, as this may lead to unpredictable results.
112
+
113
+ If Heal plugin successfully fixes the step, it will print a suggested change at the end of execution. Take it as actionable advice and use it to update the codebase. Heal plugin is supposed to be used on CI, and works automatically without human assistance.
114
+
115
+ To start, enable `heal` plugin in `codecept.conf.js` or `codecept.conf.ts`:
116
+
117
+ ```js
118
+ plugins: {
119
+ heal: {
120
+ enabled: true
121
+ }
122
+ }
123
+ ```
124
+
125
+ and run tests in AI mode with `OPENAI_API_KEY` provided:
126
+
127
+ ```
128
+ OPENAI_API_KEY=sk-******** npx codeceptjs run
129
+ ```
130
+
131
+ ![](/img/heal.png)
132
+
133
+
134
+ ### Arbitrary GPT Prompts
135
+
136
+ What if you want to take ChatGPT on the journey of test automation and ask it questions while browsing pages?
137
+
138
+ This is possible with the new `OpenAI` helper. Enable it in your config and it will automatically attach to Playwright, WebDriver, or another web helper you use. It includes the following methods:
139
+
140
+ * `askGptOnPage` - sends GPT prompt attaching the HTML of the page. Large pages will be split into chunks, according to `chunkSize` config. You will receive responses for all chunks.
141
+ * `askGptOnPageFragment` - sends GPT prompt attaching the HTML of the specific element. This method is recommended over `askGptOnPage` as you can reduce the amount of data to be processed.
142
+ * `askGptGeneralPrompt` - sends GPT prompt without HTML.
143
+
144
+ OpenAI helper won't remove non-interactive elements, so it is recommended to manually control the size of the sent HTML.
145
+
146
+ Here are some good use cases for this helper:
147
+
148
+ * get page summaries
149
+ * inside pause mode navigate through your application and ask to document pages
150
+ * etc...
151
+
152
+ ```js
153
+ // use it inside test or inside interactive pause
154
+ // pretend you are technical writer asking for documentation
155
+ const pageDoc = await I.askGptOnPageFragment('Act as technical writer, describe what is this page for', '#container');
156
+ ```
157
+
158
+ As of now, those use cases do not apply to test automation but maybe you can apply them to your testing setup.
159
+
160
+ ## Configuration
161
+
162
+ AI features can be configured inside `codecept.conf` file under `ai` section:
163
+
164
+ ```js
165
+ ai: {
166
+ model: 'gpt-3.5-turbo-16k',
167
+ temperature: 0.1,
168
+ html: // {}
169
+ }
170
+ ```
171
+
172
+ Available options are:
173
+
174
+ * `model` - [OpenAI model](https://platform.openai.com/docs/models), `gpt-3.5-turbo-16k` is recommended. You may switch to another GPT model, however, consider the speed of processing and size of the input. Models with less than 16K tokens won't be able to process complete HTML even reduced to interactive elements.
175
+ * `temperature` - [temperature](https://platform.openai.com/docs/api-reference/chat/create#chat/create-temperature) is a measure of randomness. Use the lowest value possible for test automation purposes.
176
+ * `html` - configures how HTML is processed before sending it to GPT. This section is highly important to tune to adapt to your application. For instance, the default strategy may remove some important elements, or contrary keep some elements that have no practical usage in test automation.
177
+
178
+ Here is the default config:
179
+
180
+ ```js
181
+ ai: {
182
+ html: {
183
+ maxLength: 50000,
184
+ simplify: true,
185
+ minify: true,
186
+ interactiveElements: ['a', 'input', 'button', 'select', 'textarea', 'option'],
187
+ textElements: ['label', 'h1', 'h2'],
188
+ allowedAttrs: ['id', 'for', 'class', 'name', 'type', 'value', 'tabindex', 'aria-labelledby', 'aria-label', 'label', 'placeholder', 'title', 'alt', 'src', 'role'],
189
+ allowedRoles: ['button', 'checkbox', 'search', 'textbox', 'tab'],
190
+ }
191
+ }
192
+ ```
193
+
194
+ * `maxLength`: the size of HTML to cut to not reach the token limit. 50K is the current default but you may try to increase it or even set it to null.
195
+ * `simplify`: should we process HTML before sending to GPT. This will remove all non-interactive elements from HTML.
196
+ * `minify`: shold HTML be additionally minified. This removed empty attributes, shortens notations, etc.
197
+ * `interactiveElements`: explicit list of all elements that are considered interactive.
198
+ * `textElements`: elements that contain text which can be used for test automation.
199
+ * `allowedAttrs`: explicit list of attributes that may be used to construct locators. If you use special `data-` attributes to enable locators, add them to the list.
200
+ * `allowedRoles`: list of roles that make standard elements interactive.
201
+
202
+ It is recommended to try HTML processing on one of your web pages before launching AI features of CodeceptJS.
203
+
204
+
205
+ To do that open the common page of your application and using DevTools copy the outerHTML of `<html>` element. Don't use `Page Source` for that, as it may not include dynamically added HTML elements. Save this HTML into a file and create a NodeJS script:
206
+
207
+ ```js
208
+ const { removeNonInteractiveElements } = require('codeceptjs/lib/html');
209
+ const fs = require('fs');
210
+
211
+ const htmlOpts = {
212
+ interactiveElements: ['a', 'input', 'button', 'select', 'textarea', 'label', 'option'],
213
+ allowedAttrs: ['id', 'for', 'class', 'name', 'type', 'value', 'aria-labelledby', 'aria-label', 'label', 'placeholder', 'title', 'alt', 'src', 'role'],
214
+ textElements: ['label', 'h1', 'h2'],
215
+ allowedRoles: ['button', 'checkbox', 'search', 'textbox', 'tab'],
216
+ };
217
+
218
+ html = fs.readFileSync('saved.html', 'utf8');
219
+ const result = removeNonInteractiveElements(html, htmlOpts);
220
+
221
+ console.log(result);
222
+ ```
223
+
224
+ Tune the options until you are satisfied with the results and use this as `html` config for `ai` section inside `codecept.conf` file.
225
+ It is also recommended to check the source of [removeNonInteractiveElements](https://github.com/codeceptjs/CodeceptJS/blob/3.x/lib/html.js) and if needed propose improvements to it.
226
+
227
+ For instance, if you use `data-qa` attributes to specify locators and you want to include them in HTML, use the following config:
228
+
229
+ ```js
230
+ {
231
+ // inside codecept.conf.js
232
+ ai: {
233
+ html: {
234
+ allowedAttrs: [
235
+ 'data-qa', 'id', 'for', 'class', 'name', 'type', 'value', 'aria-labelledby', 'aria-label', 'label', 'placeholder', 'title', 'alt', 'src', 'role'
236
+ ]
237
+ }
238
+ }
239
+ }
240
+ ```
241
+
242
+ ## Debugging
243
+
244
+ To debug AI features run tests with `DEBUG="codeceptjs:ai"` flag. This will print all prompts and responses from OpenAI
245
+
246
+ ```
247
+ DEBUG="codeceptjs:ai" OPENAI_API_KEY=sk-******** npx codeceptjs run
248
+ ```