codeceptjs 3.6.0-beta.1.ai-healers → 3.6.1

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 (130) hide show
  1. package/README.md +2 -2
  2. package/bin/codecept.js +2 -1
  3. package/docs/webapi/dontSeeTraffic.mustache +13 -0
  4. package/docs/webapi/flushNetworkTraffics.mustache +5 -0
  5. package/docs/webapi/grabRecordedNetworkTraffics.mustache +10 -0
  6. package/docs/webapi/seeTraffic.mustache +36 -0
  7. package/docs/webapi/startRecordingTraffic.mustache +8 -0
  8. package/docs/webapi/startRecordingWebSocketMessages.mustache +8 -0
  9. package/docs/webapi/stopRecordingTraffic.mustache +5 -0
  10. package/docs/webapi/stopRecordingWebSocketMessages.mustache +7 -0
  11. package/docs/webapi/waitForCookie.mustache +9 -0
  12. package/lib/actor.js +6 -3
  13. package/lib/command/dryRun.js +44 -13
  14. package/lib/helper/Appium.js +36 -12
  15. package/lib/helper/Expect.js +11 -8
  16. package/lib/helper/JSONResponse.js +8 -8
  17. package/lib/helper/MockServer.js +221 -0
  18. package/lib/helper/Playwright.js +107 -371
  19. package/lib/helper/Puppeteer.js +404 -71
  20. package/lib/helper/REST.js +4 -1
  21. package/lib/helper/WebDriver.js +189 -13
  22. package/lib/helper/errors/ElementAssertion.js +38 -0
  23. package/lib/helper/extras/PlaywrightReactVueLocator.js +6 -1
  24. package/lib/helper/network/actions.js +123 -0
  25. package/lib/helper/network/utils.js +187 -0
  26. package/lib/locator.js +36 -5
  27. package/lib/pause.js +4 -9
  28. package/lib/plugin/coverage.js +112 -99
  29. package/lib/step.js +3 -1
  30. package/package.json +49 -38
  31. package/typings/index.d.ts +19 -2
  32. package/typings/promiseBasedTypes.d.ts +505 -41
  33. package/typings/types.d.ts +531 -43
  34. package/docs/advanced.md +0 -351
  35. package/docs/ai.md +0 -365
  36. package/docs/api.md +0 -323
  37. package/docs/basics.md +0 -979
  38. package/docs/bdd.md +0 -539
  39. package/docs/best.md +0 -237
  40. package/docs/books.md +0 -37
  41. package/docs/bootstrap.md +0 -135
  42. package/docs/build/AI.js +0 -124
  43. package/docs/build/ApiDataFactory.js +0 -410
  44. package/docs/build/Appium.js +0 -2027
  45. package/docs/build/Expect.js +0 -422
  46. package/docs/build/FileSystem.js +0 -228
  47. package/docs/build/GraphQL.js +0 -229
  48. package/docs/build/GraphQLDataFactory.js +0 -309
  49. package/docs/build/JSONResponse.js +0 -338
  50. package/docs/build/Mochawesome.js +0 -71
  51. package/docs/build/Nightmare.js +0 -2152
  52. package/docs/build/OpenAI.js +0 -126
  53. package/docs/build/Playwright.js +0 -5110
  54. package/docs/build/Protractor.js +0 -2706
  55. package/docs/build/Puppeteer.js +0 -3905
  56. package/docs/build/REST.js +0 -344
  57. package/docs/build/TestCafe.js +0 -2125
  58. package/docs/build/WebDriver.js +0 -4240
  59. package/docs/changelog.md +0 -2572
  60. package/docs/commands.md +0 -266
  61. package/docs/community-helpers.md +0 -58
  62. package/docs/configuration.md +0 -157
  63. package/docs/continuous-integration.md +0 -22
  64. package/docs/custom-helpers.md +0 -306
  65. package/docs/data.md +0 -379
  66. package/docs/detox.md +0 -235
  67. package/docs/docker.md +0 -136
  68. package/docs/email.md +0 -183
  69. package/docs/examples.md +0 -149
  70. package/docs/heal.md +0 -186
  71. package/docs/helpers/ApiDataFactory.md +0 -266
  72. package/docs/helpers/Appium.md +0 -1374
  73. package/docs/helpers/Detox.md +0 -586
  74. package/docs/helpers/Expect.md +0 -275
  75. package/docs/helpers/FileSystem.md +0 -152
  76. package/docs/helpers/GraphQL.md +0 -151
  77. package/docs/helpers/GraphQLDataFactory.md +0 -226
  78. package/docs/helpers/JSONResponse.md +0 -254
  79. package/docs/helpers/Mochawesome.md +0 -8
  80. package/docs/helpers/MockRequest.md +0 -377
  81. package/docs/helpers/Nightmare.md +0 -1305
  82. package/docs/helpers/OpenAI.md +0 -70
  83. package/docs/helpers/Playwright.md +0 -2759
  84. package/docs/helpers/Polly.md +0 -44
  85. package/docs/helpers/Protractor.md +0 -1769
  86. package/docs/helpers/Puppeteer-firefox.md +0 -86
  87. package/docs/helpers/Puppeteer.md +0 -2317
  88. package/docs/helpers/REST.md +0 -218
  89. package/docs/helpers/TestCafe.md +0 -1321
  90. package/docs/helpers/WebDriver.md +0 -2547
  91. package/docs/hooks.md +0 -340
  92. package/docs/index.md +0 -111
  93. package/docs/installation.md +0 -75
  94. package/docs/internal-api.md +0 -266
  95. package/docs/locators.md +0 -339
  96. package/docs/mobile-react-native-locators.md +0 -67
  97. package/docs/mobile.md +0 -338
  98. package/docs/pageobjects.md +0 -291
  99. package/docs/parallel.md +0 -400
  100. package/docs/playwright.md +0 -632
  101. package/docs/plugins.md +0 -1247
  102. package/docs/puppeteer.md +0 -316
  103. package/docs/quickstart.md +0 -162
  104. package/docs/react.md +0 -70
  105. package/docs/reports.md +0 -392
  106. package/docs/secrets.md +0 -36
  107. package/docs/shadow.md +0 -68
  108. package/docs/shared/keys.mustache +0 -31
  109. package/docs/shared/react.mustache +0 -1
  110. package/docs/testcafe.md +0 -174
  111. package/docs/translation.md +0 -247
  112. package/docs/tutorial.md +0 -271
  113. package/docs/typescript.md +0 -180
  114. package/docs/ui.md +0 -59
  115. package/docs/videos.md +0 -28
  116. package/docs/visual.md +0 -202
  117. package/docs/vue.md +0 -143
  118. package/docs/webdriver.md +0 -701
  119. package/docs/wiki/Books-&-Posts.md +0 -27
  120. package/docs/wiki/Community-Helpers-&-Plugins.md +0 -53
  121. package/docs/wiki/Converting-Playwright-to-Istanbul-Coverage.md +0 -61
  122. package/docs/wiki/Examples.md +0 -145
  123. package/docs/wiki/Google-Summer-of-Code-(GSoC)-2020.md +0 -68
  124. package/docs/wiki/Home.md +0 -16
  125. package/docs/wiki/Migration-to-Appium-v2---CodeceptJS.md +0 -83
  126. package/docs/wiki/Release-Process.md +0 -24
  127. package/docs/wiki/Roadmap.md +0 -23
  128. package/docs/wiki/Tests.md +0 -1393
  129. package/docs/wiki/Upgrading-to-CodeceptJS-3.md +0 -153
  130. package/docs/wiki/Videos.md +0 -19
package/docs/basics.md DELETED
@@ -1,979 +0,0 @@
1
- ---
2
- permalink: /basics
3
- title: Getting Started
4
- ---
5
-
6
- # Getting Started
7
-
8
- CodeceptJS is a modern end to end testing framework with a special BDD-style syntax. The tests are written as a linear scenario of the user's action on a site.
9
-
10
- ```js
11
- Feature('CodeceptJS demo');
12
-
13
- Scenario('check Welcome page on site', ({ I }) => {
14
- I.amOnPage('/');
15
- I.see('Welcome');
16
- });
17
- ```
18
-
19
- Tests are expected to be written in **ECMAScript 7**.
20
-
21
- Each test is described inside a `Scenario` function with the `I` object passed into it.
22
- The `I` object is an **actor**, an abstraction for a testing user. The `I` is a proxy object for currently enabled **Helpers**.
23
-
24
- ## Architecture
25
-
26
- CodeceptJS bypasses execution commands to helpers. Depending on the helper enabled, your tests will be executed differently.
27
-
28
- The following is a diagram of the CodeceptJS architecture:
29
-
30
- ![architecture](/img/architecture.png)
31
-
32
- All helpers share the same API, so it's easy to migrate tests from one backend to another.
33
- However, because of the difference in backends and their limitations, they are not guaranteed to be compatible with each other. For instance, you can't set request headers in WebDriver but you can do so in Playwright or Puppeteer.
34
-
35
- **Pick one helper, as it defines how tests are executed.** If requirements change it's easy to migrate to another.
36
-
37
- ---
38
-
39
- Refer to following guides to more information on:
40
-
41
- * [▶ Playwright](/playwright)
42
- * [▶ WebDriver](/webdriver)
43
- * [▶ Puppeteer](/puppeteer)
44
- * [▶ TestCafe](/testcafe)
45
-
46
- > ℹ Depending on a helper selected a list of available actions may change.
47
-
48
- To list all available commands for the current configuration run `codeceptjs list`
49
- or enable [auto-completion by generating TypeScript definitions](#intellisense).
50
-
51
- > 🤔 It is possible to access API of a backend you use inside a test or a [custom helper](/helpers/). For instance, to use Puppeteer API inside a test use [`I.usePuppeteerTo`](/helpers/Puppeteer/#usepuppeteerto) inside a test. Similar methods exist for each helper.
52
-
53
-
54
- ## Writing Tests
55
-
56
- Tests are written from a user's perspective. There is an actor (represented as `I`) which contains actions taken from helpers. A test is written as a sequence of actions performed by an actor:
57
-
58
- ```js
59
- I.amOnPage('/');
60
- I.click('Login');
61
- I.see('Please Login', 'h1');
62
- // ...
63
- ```
64
-
65
- ### Opening a Page
66
-
67
- A test should usually start by navigating the browser to a website.
68
-
69
- Start a test by opening a page. Use the `I.amOnPage()` command for this:
70
-
71
- ```js
72
- // When "http://site.com" is url in config
73
- I.amOnPage('/'); // -> opens http://site.com/
74
- I.amOnPage('/about'); // -> opens http://site.com/about
75
- I.amOnPage('https://google.com'); // -> https://google.com
76
- ```
77
-
78
- When an URL doesn't start with a protocol (http:// or https://) it is considered to be a relative URL and will be appended to the URL which was initially set-up in the config.
79
-
80
- > It is recommended to use a relative URL and keep the base URL in the config file, so you can easily switch between development, stage, and production environments.
81
-
82
-
83
- ### Locating Element
84
-
85
- Element can be found by CSS or XPath locators.
86
-
87
- ```js
88
- I.seeElement('.user'); // element with CSS class user
89
- I.seeElement('//button[contains(., "press me")]'); // button
90
- ```
91
-
92
- By default CodeceptJS tries to guess the locator type.
93
- In order to specify the exact locator type you can pass an object called **strict locator**.
94
-
95
- ```js
96
- I.seeElement({css: 'div.user'});
97
- I.seeElement({xpath: '//div[@class=user]'});
98
- ```
99
-
100
- Strict locators allow to specify additional locator types:
101
-
102
- ```js
103
- // locate form element by name
104
- I.seeElement({name: 'password'});
105
- // locate element by React component and props
106
- I.seeElement({react: 'user-profile', props: {name: 'davert'}});
107
- ```
108
-
109
- In [mobile testing](https://codecept.io/mobile/#locating-elements) you can use `~` to specify the accessibility id to locate an element. In web application you can locate elements by their `aria-label` value.
110
-
111
- ```js
112
- // locate element by [aria-label] attribute in web
113
- // or by accessibility id in mobile
114
- I.seeElement('~username');
115
- ```
116
-
117
- > [▶ Learn more about using locators in CodeceptJS](/locators).
118
-
119
- ### Clicking
120
-
121
- CodeceptJS provides a flexible syntax to specify an element to click.
122
-
123
- By default CodeceptJS tries to find the button or link with the exact text on it
124
-
125
- ```js
126
- // search for link or button
127
- I.click('Login');
128
- ```
129
-
130
- If none was found, CodeceptJS tries to find a link or button containing that text. In case an image is clickable its `alt` attribute will be checked for text inclusion. Form buttons will also be searched by name.
131
-
132
- To narrow down the results you can specify a context in the second parameter.
133
-
134
- ```js
135
- I.click('Login', '.nav'); // search only in .nav
136
- I.click('Login', {css: 'footer'}); // search only in footer
137
- ```
138
-
139
- > To skip guessing the locator type, pass in a strict locator - A locator starting with '#' or '.' is considered to be CSS. Locators starting with '//' or './/' are considered to be XPath.
140
-
141
- You are not limited to buttons and links. Any element can be found by passing in valid CSS or XPath:
142
-
143
- ```js
144
- // click element by CSS
145
- I.click('#signup');
146
- // click element located by special test-id attribute
147
- I.click('//dev[@test-id="myid"]');
148
- ```
149
-
150
- > ℹ If click doesn't work in a test but works for user, it is possible that frontend application is not designed for automated testing. To overcome limitation of standard click in this edgecase use `forceClick` method. It will emulate click instead of sending native event. This command will click an element no matter if this element is visible or animating. It will send JavaScript "click" event to it.
151
-
152
- ### Filling Fields
153
-
154
- Clicking the links is not what takes the most time during testing a web site. If your site consists only of links you can skip test automation. The most waste of time goes into the testing of forms. CodeceptJS provides several ways of doing that.
155
-
156
- Let's submit this sample form for a test:
157
-
158
- ![](https://user-images.githubusercontent.com/220264/80355863-494a8280-8881-11ea-9b41-ba1f07abf094.png)
159
-
160
- ```html
161
- <form method="post" action="/update" id="update_form">
162
- <label for="user_name">Name</label>
163
- <input type="text" name="user[name]" id="user_name" /><br>
164
- <label for="user_email">Email</label>
165
- <input type="text" name="user[email]" id="user_email" /><br>
166
- <label for="user_role">Role</label>
167
- <select id="user_role" name="user[role]">
168
- <option value="0">Admin</option>
169
- <option value="1">User</option>
170
- </select><br>
171
- <input type="checkbox" id="accept" /> <label for="accept">Accept changes</label>
172
- <div>
173
- <input type="submit" name="submitButton" class="btn btn-primary" value="Save" />
174
- </div>
175
- </form>
176
- ```
177
-
178
- We need to fill in all those fields and click the "Update" button. CodeceptJS matches form elements by their label, name, or by CSS or XPath locators.
179
-
180
- ```js
181
- // we are using label to match user_name field
182
- I.fillField('Name', 'Miles');
183
- // we can use input name
184
- I.fillField('user[email]','miles@davis.com');
185
- // select element by label, choose option by text
186
- I.selectOption('Role','Admin');
187
- // click 'Save' button, found by text
188
- I.checkOption('Accept');
189
- I.click('Save');
190
- ```
191
-
192
- > ℹ `selectOption` works only with standard `<select>` <select></select> HTML elements. If your selectbox is created by React, Vue, or as a component of any other framework, this method potentially won't work with it. Use `click` to manipulate it.
193
-
194
- > ℹ `checkOption` also works only with standard `<input type="checkbox">` <input type="checkbox"> HTML elements. If your checkbox is created by React, Vue, or as a component of any other framework, this method potentially won't work with it. Use `click` to manipulate it.
195
-
196
- Alternative scenario:
197
-
198
- ```js
199
- // we are using CSS
200
- I.fillField('#user_name', 'Miles');
201
- I.fillField('#user_email','miles@davis.com');
202
- // select element by label, option by value
203
- I.selectOption('#user_role','1');
204
- // click 'Update' button, found by name
205
- I.click('submitButton', '#update_form');
206
- ```
207
-
208
- To fill in sensitive data use the `secret` function, it won't expose actual value in logs.
209
-
210
- ```js
211
- I.fillField('password', secret('123456'));
212
- ```
213
-
214
- > ℹ️ Learn more about [masking secret](/secrets/) output
215
-
216
- ### Assertions
217
-
218
- In order to verify the expected behavior of a web application, its content should be checked.
219
- CodeceptJS provides built-in assertions for that. They start with a `see` (or `dontSee`) prefix.
220
-
221
- The most general and common assertion is `see`, which checks visilibility of a text on a page:
222
-
223
- ```js
224
- // Just a visible text on a page
225
- I.see('Hello');
226
- // text inside .msg element
227
- I.see('Hello', '.msg');
228
- // opposite
229
- I.dontSee('Bye');
230
- ```
231
-
232
- You should provide a text as first argument and, optionally, a locator to search for a text in a context.
233
-
234
- You can check that specific element exists (or not) on a page, as it was described in [Locating Element](#locating-element) section.
235
-
236
- ```js
237
- I.seeElement('.notice');
238
- I.dontSeeElement('.error');
239
- ```
240
-
241
- Additional assertions:
242
-
243
- ```js
244
- I.seeInCurrentUrl('/user/miles');
245
- I.seeInField('user[name]', 'Miles');
246
- I.seeInTitle('My Website');
247
- ```
248
-
249
- To see all possible assertions, check the helper's reference.
250
-
251
- > ℹ If you need custom assertions, you can install an assertion libarary like `chai`, use grabbers to obtain information from a browser and perform assertions. However, it is recommended to put custom assertions into a helper for further reuse.
252
-
253
- ### Grabbing
254
-
255
- Sometimes you need to retrieve data from a page to use it in the following steps of a scenario.
256
- Imagine the application generates a password, and you want to ensure that user can login using this password.
257
-
258
- ```js
259
- Scenario('login with generated password', async ({ I }) => {
260
- I.fillField('email', 'miles@davis.com');
261
- I.click('Generate Password');
262
- const password = await I.grabTextFrom('#password');
263
- I.click('Login');
264
- I.fillField('email', 'miles@davis.com');
265
- I.fillField('password', password);
266
- I.click('Log in!');
267
- I.see('Hello, Miles');
268
- });
269
- ```
270
-
271
- The `grabTextFrom` action is used to retrieve the text from an element. All actions starting with the `grab` prefix are expected to return data. In order to synchronize this step with a scenario you should pause the test execution with the `await` keyword of ES6. To make it work, your test should be written inside a async function (notice `async` in its definition).
272
-
273
- ```js
274
- Scenario('use page title', async ({ I }) => {
275
- // ...
276
- const password = await I.grabTextFrom('#password');
277
- I.fillField('password', password);
278
- });
279
- ```
280
-
281
- ### Waiting
282
-
283
- In modern web applications, rendering is done on the client-side.
284
- Sometimes that may cause delays. A test may fail while trying to click an element which has not appeared on a page yet.
285
- To handle these cases, the `wait*` methods has been introduced.
286
-
287
- ```js
288
- I.waitForElement('#agree_button', 30); // secs
289
- // clicks a button only when it is visible
290
- I.click('#agree_button');
291
- ```
292
-
293
- ## How It Works
294
-
295
- Tests are written in a synchronous way. This improves the readability and maintainability of tests.
296
- While writing tests you should not think about promises, and instead should focus on the test scenario.
297
-
298
- However, behind the scenes **all actions are wrapped in promises**, inside of the `I` object.
299
- [Global promise](https://github.com/codeceptjs/CodeceptJS/blob/master/lib/recorder.js) chain is initialized before each test and all `I.*` calls will be appended to it, as well as setup and teardown.
300
-
301
- > 📺 [Learn how CodeceptJS](https://www.youtube.com/watch?v=MDLLpHAwy_s) works with promises by watching video on YouTube
302
-
303
- If you want to get information from a running test you can use `await` inside the **async function**, and utilize special methods of helpers started with the `grab` prefix.
304
-
305
- ```js
306
- Scenario('try grabbers', async ({ I }) => {
307
- let title = await I.grabTitle();
308
- });
309
- ```
310
-
311
- then you can use those variables in assertions:
312
-
313
- ```js
314
- var title = await I.grabTitle();
315
- var assert = require('assert');
316
- assert.equal(title, 'CodeceptJS');
317
- ```
318
-
319
- It is important to understand the usage of **async** functions in CodeceptJS. While non-returning actions can be called without await, if an async function uses `grab*` action it must be called with `await`:
320
-
321
- ```js
322
- // a helper function
323
- async function getAllUsers(I) {
324
- const users = await I.grabTextFrom('.users');
325
- return users.filter(u => u.includes('active'))
326
- }
327
-
328
- // a test
329
- Scenario('try helper functions', async ({ I }) => {
330
- // we call function with await because it includes `grab`
331
- const users = await getAllUsers(I);
332
- });
333
- ```
334
-
335
- If you miss `await` you get commands unsynchrhonized. And this will result to an error like this:
336
-
337
- ```
338
- (node:446390) UnhandledPromiseRejectionWarning: ...
339
- at processTicksAndRejections (internal/process/task_queues.js:95:5)
340
- (node:446390) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
341
- ```
342
-
343
- If you face that error please make sure that all async functions are called with `await`.
344
-
345
- ## Running Tests
346
-
347
- To launch tests use the `run` command, and to execute tests in [multiple threads](/advanced/parallel) using `run-workers` command.
348
-
349
- ### Level of Detail
350
-
351
- To see the step-by-step output of running tests, add the `--steps` flag:
352
-
353
- ```
354
- npx codeceptjs run --steps
355
- ```
356
-
357
- To see a more detailed output add the `--debug` flag:
358
-
359
- ```
360
- npx codeceptjs run --debug
361
- ```
362
-
363
- To see very detailed output informations use the `--verbose` flag:
364
-
365
- ```
366
- npx codeceptjs run --verbose
367
- ```
368
-
369
- ### Filter
370
-
371
- A single test file can be executed if you provide a relative path to such a file:
372
-
373
- ```
374
- npx codeceptjs run github_test.js
375
-
376
- # or
377
-
378
- npx codeceptjs run admin/login_test.js
379
- ```
380
-
381
- To filter a test by name use the `--grep` parameter, which will execute all tests with names matching the regex pattern.
382
-
383
- To run all tests with the `slow` word in it:
384
-
385
- ```
386
- npx codeceptjs run --grep "slow"
387
- ```
388
-
389
- It is recommended to [filter tests by tags](/advanced/#tags).
390
-
391
-
392
- > For more options see [full reference of `run` command](/commands/#run).
393
-
394
- ### Parallel Run
395
-
396
- Tests can be executed in parallel mode by using [NodeJS workers](https://nodejs.org/api/worker_threads.html). Use `run-workers` command with the number of workers (threads) to split tests into different workers.
397
-
398
- ```
399
- npx codeceptjs run-workers 3
400
- ```
401
-
402
- Tests are split by scenarios, not by files. Results are aggregated and shown up in the main process.
403
-
404
- ## Configuration
405
-
406
- Configuration is set in the `codecept.conf.js` file which was created during the `init` process.
407
- Inside the config file you can enable and configure helpers and plugins, and set bootstrap and teardown scripts.
408
-
409
- ```js
410
- exports.config = {
411
- helpers: {
412
- // enabled helpers with their configs
413
- },
414
- plugins: {
415
- // list of used plugins
416
- },
417
- include: {
418
- // current actor and page objects
419
- }
420
- }
421
- ```
422
-
423
- > ▶ See complete [configuration reference](/configuration).
424
-
425
- You can have multiple configuration files for a the same project, in this case you can specify a config file to be used with `-c` when running.
426
-
427
- ```
428
- npx codeceptjs run -c codecept.ci.conf.js
429
- ```
430
-
431
- Tuning configuration for helpers like WebDriver, Puppeteer can be hard, as it requires good understanding of how these technologies work. Use the [`@codeceptjs/configure`](https://github.com/codeceptjs/configure) package with common configuration recipes.
432
-
433
- For instance, you can set the window size or toggle headless mode, no matter of which helpers are actually used.
434
-
435
- ```js
436
- const { setHeadlessWhen, setWindowSize } = require('@codeceptjs/configure');
437
-
438
- // run headless when CI environment variable set
439
- setHeadlessWhen(process.env.CI);
440
- // set window size for any helper: Puppeteer, WebDriver, TestCafe
441
- setWindowSize(1600, 1200);
442
-
443
- exports.config = {
444
- // ...
445
- }
446
- ```
447
-
448
- > ▶ See more [configuration recipes](https://github.com/codeceptjs/configure)
449
-
450
- ## Debug
451
-
452
- CodeceptJS allows to write and debug tests on the fly while keeping your browser opened.
453
- By using the interactive shell you can stop execution at any point and type in any CodeceptJS commands.
454
-
455
- This is especially useful while writing a new scratch. After opening a page call `pause()` to start interacting with a page:
456
-
457
- ```js
458
- I.amOnPage('/');
459
- pause();
460
- ```
461
-
462
- Try to perform your scenario step by step. Then copy succesful commands and insert them into a test.
463
-
464
- ### Pause
465
-
466
- Test execution can be paused in any place of a test with `pause()` call.
467
- Variables can also be passed to `pause({data: 'hi', func: () => console.log('hello')})` which can be accessed in Interactive shell.
468
-
469
- This launches the interactive console where you can call any action from the `I` object.
470
-
471
- ```
472
- Interactive shell started
473
- Press ENTER to resume test
474
- Use JavaScript syntax to try steps in action
475
- - Press ENTER to run the next step
476
- - Press TAB twice to see all available commands
477
- - Type exit + Enter to exit the interactive shell
478
- - Prefix => to run js commands
479
- I.
480
- ```
481
-
482
- Type in different actions to try them, copy and paste successful ones into the test file.
483
-
484
- Press `ENTER` to resume test execution.
485
-
486
- To **debug test step-by-step** press Enter, the next step will be executed and interactive shell will be shown again.
487
-
488
- To see all available commands, press TAB two times to see list of all actions included in the `I` object.
489
-
490
- > The interactive shell can be started outside of test context by running `npx codeceptjs shell`
491
-
492
- PageObjects and other variables can also be passed to as object:
493
-
494
- ```js
495
- pause({ loginPage, data: 'hi', func: () => console.log('hello') });
496
- ```
497
-
498
- Inside a pause mode you can use `loginPage`, `data`, `func` variables.
499
- Arbitrary JavaScript code can be executed when used `=> ` prefix:
500
-
501
- ```js
502
- I.=> loginPage.open()
503
- I.=> func()
504
- I.=> 2 + 5
505
- ```
506
-
507
- ### Pause on Fail
508
-
509
- To start interactive pause automatically for a failing test you can run tests with [pauseOnFail Plugin](/plugins/#pauseonfail).
510
- When a test fails, the pause mode will be activated, so you can inspect current browser session before it is closed.
511
-
512
- > **[pauseOnFail plugin](/plugins/#pauseOnFail) can be used** for new setups
513
-
514
- To run tests with pause on fail enabled use `-p pauseOnFail` option
515
-
516
- ```
517
- npx codeceptjs run -p pauseOnFail
518
- ```
519
-
520
- > To enable pause after a test without a plugin you can use `After(pause)` inside a test file.
521
-
522
-
523
- ### Screenshot on Failure
524
-
525
- By default CodeceptJS saves a screenshot of a failed test.
526
- This can be configured in [screenshotOnFail Plugin](/plugins/#screenshotonfail)
527
-
528
- > **[screenshotOnFail plugin](/plugins/#screenshotonfail) is enabled by default** for new setups
529
-
530
- ### Step By Step Report
531
-
532
- To see how the test was executed, use [stepByStepReport Plugin](/plugins/#stepbystepreport). It saves a screenshot of each passed step and shows them in a nice slideshow.
533
-
534
- ## Before
535
-
536
- Common preparation steps like opening a web page or logging in a user, can be placed in the `Before` or `Background` hooks:
537
-
538
- ```js
539
- Feature('CodeceptJS Demonstration');
540
-
541
- Before(({ I }) => { // or Background
542
- I.amOnPage('/documentation');
543
- });
544
-
545
- Scenario('test some forms', ({ I }) => {
546
- I.click('Create User');
547
- I.see('User is valid');
548
- I.dontSeeInCurrentUrl('/documentation');
549
- });
550
-
551
- Scenario('test title', ({ I }) => {
552
- I.seeInTitle('Example application');
553
- });
554
- ```
555
-
556
- Same as `Before` you can use `After` to run teardown for each scenario.
557
-
558
- ## BeforeSuite
559
-
560
- If you need to run complex a setup before all tests and have to teardown this afterwards, you can use the `BeforeSuite` and `AfterSuite` functions.
561
- `BeforeSuite` and `AfterSuite` have access to the `I` object, but `BeforeSuite/AfterSuite` don't have any access to the browser, because it's not running at this moment.
562
- You can use them to execute handlers that will setup your environment. `BeforeSuite/AfterSuite` will work only for the file it was declared in (so you can declare different setups for files)
563
-
564
- ```js
565
- BeforeSuite(({ I }) => {
566
- I.syncDown('testfolder');
567
- });
568
-
569
- AfterSuite(({ I }) => {
570
- I.syncUp('testfolder');
571
- I.clearDir('testfolder');
572
- });
573
- ```
574
-
575
- ## Retries
576
-
577
- ### Auto Retry
578
-
579
- Each failed step is auto-retried by default via [retryFailedStep Plugin](/plugins/#retryfailedstep).
580
- If this is not expected, this plugin can be disabled in a config.
581
-
582
- > **[retryFailedStep plugin](/plugins/#retryfailedstep) is enabled by default** incide global configuration
583
-
584
- ### Retry Step
585
-
586
- Unless you use retryFailedStep plugin you can manually control retries in your project.
587
-
588
- If you have a step which often fails, you can retry execution for this single step.
589
- Use the `retry()` function before an action to ask CodeceptJS to retry it on failure:
590
-
591
- ```js
592
- I.retry().see('Welcome');
593
- ```
594
-
595
- If you'd like to retry a step more than once, pass the amount as a parameter:
596
-
597
- ```js
598
- I.retry(3).see('Welcome');
599
- ```
600
-
601
- Additional options can be provided to `retry`, so you can set the additional options (defined in [promise-retry](https://www.npmjs.com/package/promise-retry) library).
602
-
603
-
604
- ```js
605
- // retry action 3 times waiting for 0.1 second before next try
606
- I.retry({ retries: 3, minTimeout: 100 }).see('Hello');
607
-
608
- // retry action 3 times waiting no more than 3 seconds for last retry
609
- I.retry({ retries: 3, maxTimeout: 3000 }).see('Hello');
610
-
611
- // retry 2 times if error with message 'Node not visible' happens
612
- I.retry({
613
- retries: 2,
614
- when: err => err.message === 'Node not visible'
615
- }).seeElement('#user');
616
- ```
617
-
618
- Pass a function to the `when` option to retry only when an error matches the expected one.
619
-
620
- ### Retry Multiple Steps
621
-
622
- To retry a group of steps enable [retryTo plugin](/plugins/#retryto):
623
-
624
- ```js
625
- // retry these steps 5 times before failing
626
- await retryTo((tryNum) => {
627
- I.switchTo('#editor frame');
628
- I.click('Open');
629
- I.see('Opened')
630
- }, 5);
631
- ```
632
-
633
- ### Retry Scenario
634
-
635
- When you need to rerun scenarios a few times, add the `retries` option to the `Scenario` declaration.
636
-
637
- CodeceptJS implements retries the same way [Mocha does](https://mochajs.org#retry-tests);
638
- You can set the number of a retries for a feature:
639
-
640
- ```js
641
- Scenario('Really complex', ({ I }) => {
642
- // test goes here
643
- }).retry(2);
644
-
645
- // alternative
646
- Scenario('Really complex', { retries: 2 },({ I }) => {});
647
- ```
648
-
649
- This scenario will be restarted two times on a failure.
650
- Unlike retry step, there is no `when` condition supported for retries on a scenario level.
651
-
652
- ### Retry Before <Badge text="Since 3.4" type="warning"/>
653
-
654
- To retry `Before`, `BeforeSuite`, `After`, `AfterSuite` hooks, add corresponding option to a `Feature`:
655
-
656
- * `retryBefore`
657
- * `retryBeforeSuite`
658
- * `retryAfter`
659
- * `retryAfterSuite`
660
-
661
- For instance, to retry Before hook 3 times:
662
-
663
- ```js
664
- Feature('this have a flaky Befure', { retryBefore: 3 })
665
- ```
666
-
667
- Multiple options of different values can be set at the same time
668
-
669
- ### Retry Feature
670
-
671
- To set this option for all scenarios in a file, add `retry` to a feature:
672
-
673
- ```js
674
- Feature('Complex JS Stuff').retry(3);
675
- // or
676
- Feature('Complex JS Stuff', { retries: 3 })
677
- ```
678
-
679
- Every Scenario inside this feature will be rerun 3 times.
680
- You can make an exception for a specific scenario by passing the `retries` option to a Scenario.
681
-
682
- ### Retry Configuration <Badge text="Since 3.4" type="warning"/>
683
-
684
- It is possible to set retry rules globally via `retry` config option. The configuration is flexible and allows multiple formats.
685
- The simplest config would be:
686
-
687
- ```js
688
- // inside codecept.conf.js
689
- retry: 3
690
- ```
691
-
692
- This will enable Feature Retry for all executed feature, retrying failing tests 3 times.
693
-
694
- An object can be used to tune retries of a Before/After hook, Scenario or Feature
695
-
696
- ```js
697
- // inside codecept.conf.js
698
- retry: {
699
- Feature: ...,
700
- Scenario: ...,
701
- Before: ...,
702
- BeforeSuite: ...,
703
- After: ...,
704
- AfterSuite: ...,
705
- }
706
- ```
707
-
708
- Multiple retry configs can be added via array. To use different retry configs for different subset of tests use `grep` option inside a retry config:
709
-
710
- ```js
711
- // inside codecept.conf.js
712
- retry: [
713
- {
714
- // enable this config only for flaky tests
715
- grep: '@flaky',
716
- Before: 3
717
- Scenario: 3
718
- },
719
- {
720
- // retry less when running slow tests
721
- grep: '@slow'
722
- Scenario: 1
723
- Before: 1
724
- }, {
725
- // retry all BeforeSuite
726
- BeforeSuite: 3
727
- }
728
- ]
729
- ```
730
-
731
- When using `grep` with `Before`, `After`, `BeforeSuite`, `AfterSuite`, a suite title will be checked for included value.
732
-
733
- > ℹ️ `grep` value can be string or regexp
734
-
735
- Rules are applied in the order of array element, so the last option will override a previous one. Global retries config can be overridden in a file as described previously.
736
-
737
- ### Retry Run
738
-
739
- On the highest level of the "retry pyramid" there is an option to retry a complete run multiple times.
740
- Even this is the slowest option of all, it can be helpful to detect flaky tests.
741
-
742
- [`run-rerun`](https://codecept.io/commands/#run-rerun) command will restart the run multiple times to values you provide. You can set minimal and maximal number of restarts in configuration file.
743
-
744
- ```
745
- npx codeceptjs run-rerun
746
- ```
747
-
748
-
749
- [Here are some ideas](https://github.com/codeceptjs/CodeceptJS/pull/231#issuecomment-249554933) on where to use BeforeSuite hooks.
750
-
751
- ## Within
752
-
753
- To specify the exact area on a page where actions can be performed you can use the `within` function.
754
- Everything executed in its context will be narrowed to context specified by locator:
755
-
756
- Usage: `within('section', ()=>{})`
757
-
758
- ```js
759
- I.amOnPage('https://github.com');
760
- within('.js-signup-form', () => {
761
- I.fillField('user[login]', 'User');
762
- I.fillField('user[email]', 'user@user.com');
763
- I.fillField('user[password]', 'user@user.com');
764
- I.click('button');
765
- });
766
- I.see('There were problems creating your account.');
767
- ```
768
-
769
- > ⚠ `within` can cause problems when used incorrectly. If you see a weird behavior of a test try to refactor it to not use `within`. It is recommended to keep within for simplest cases when possible.
770
- > Since `within` returns a Promise, it may be necessary to `await` the result even when you're not intending to use the return value.
771
-
772
- `within` can also work with IFrames. A special `frame` locator is required to locate the iframe and get into its context.
773
-
774
-
775
- See example:
776
-
777
- ```js
778
- within({frame: "#editor"}, () => {
779
- I.see('Page');
780
- });
781
- ```
782
-
783
- > ℹ IFrames can also be accessed via `I.switchTo` command of a corresponding helper.
784
-
785
- Nested IFrames can be set by passing an array *(WebDriver & Puppeteer only)*:
786
-
787
- ```js
788
- within({frame: [".content", "#editor"]}, () => {
789
- I.see('Page');
790
- });
791
- ```
792
-
793
- When running steps inside, a within block will be shown with a shift:
794
-
795
- ![within](/img/within.png)
796
-
797
- Within can return a value, which can be used in a scenario:
798
-
799
- ```js
800
- // inside async function
801
- const val = await within('#sidebar', () => {
802
- return I.grabTextFrom({ css: 'h1' });
803
- });
804
- I.fillField('Description', val);
805
- ```
806
-
807
- ## Conditional Actions
808
-
809
- There is a way to execute unsuccessful actions to without failing a test.
810
- This might be useful when you might need to click "Accept cookie" button but probably cookies were already accepted.
811
- To handle these cases `tryTo` function was introduced:
812
-
813
- ```js
814
- tryTo(() => I.click('Accept', '.cookies'));
815
- ```
816
-
817
- You may also use `tryTo` for cases when you deal with uncertainty on page:
818
-
819
- * A/B testing
820
- * soft assertions
821
- * cookies & gdpr
822
-
823
- `tryTo` function is enabled by default via [tryTo plugin](/plugins/#tryto)
824
-
825
- ## Comments
826
-
827
- There is a simple way to add additional comments to your test scenario:
828
- Use the `say` command to print information to screen:
829
-
830
- ```js
831
- I.say('I am going to publish post');
832
- I.say('I enter title and body');
833
- I.say('I expect post is visible on site');
834
- ```
835
-
836
- Use the second parameter to pass in a color value (ASCII).
837
-
838
- ```js
839
- I.say('This is red', 'red'); //red is used
840
- I.say('This is blue', 'blue'); //blue is used
841
- I.say('This is by default'); //cyan is used
842
- ```
843
-
844
-
845
- ## IntelliSense
846
-
847
- ![Edit](/img/edit.gif)
848
-
849
- To get autocompletion when working with CodeceptJS, use Visual Studio Code or another IDE that supports TypeScript Definitions.
850
-
851
- Generate step definitions with:
852
-
853
- ```sh
854
- npx codeceptjs def
855
- ```
856
-
857
- Create a file called `jsconfig.json` in your project root directory, unless you already have one.
858
-
859
- ```jsconfig.json
860
- {
861
- "compilerOptions": {
862
- "allowJs": true,
863
- }
864
- }
865
- ```
866
-
867
- Alternatively, you can include `/// <reference path="./steps.d.ts" />` into your test files
868
- to get method autocompletion while writing tests.
869
-
870
-
871
- ## Multiple Sessions
872
-
873
- CodeceptJS allows to run several browser sessions inside a test. This can be useful for testing communication between users inside a chat or other systems. To open another browser use the `session()` function as shown in the example:
874
-
875
- ```js
876
- Scenario('test app', ({ I }) => {
877
- I.amOnPage('/chat');
878
- I.fillField('name', 'davert');
879
- I.click('Sign In');
880
- I.see('Hello, davert');
881
- session('john', () => {
882
- // another session started
883
- I.amOnPage('/chat');
884
- I.fillField('name', 'john');
885
- I.click('Sign In');
886
- I.see('Hello, john');
887
- });
888
- // switching back to default session
889
- I.fillField('message', 'Hi, john');
890
- // there is a message from current user
891
- I.see('me: Hi, john', '.messages');
892
- session('john', () => {
893
- // let's check if john received it
894
- I.see('davert: Hi, john', '.messages');
895
- });
896
- });
897
- ```
898
-
899
- The `session` function expects the first parameter to be the name of the session. You can switch back to this session by using the same name.
900
-
901
- You can override the configuration for the session by passing a second parameter:
902
-
903
- ```js
904
- session('john', { browser: 'firefox' } , () => {
905
- // run this steps in firefox
906
- I.amOnPage('/');
907
- });
908
- ```
909
-
910
- or just start the session without switching to it. Call `session` passing only its name:
911
-
912
- ```js
913
- Scenario('test', ({ I }) => {
914
- // opens 3 additional browsers
915
- session('john');
916
- session('mary');
917
- session('jane');
918
-
919
- I.amOnPage('/');
920
-
921
- // switch to session by its name
922
- session('mary', () => {
923
- I.amOnPage('/login');
924
- });
925
- }
926
- ```
927
- `session` can return a value which can be used in a scenario:
928
-
929
- ```js
930
- // inside async function
931
- const val = await session('john', () => {
932
- I.amOnPage('/info');
933
- return I.grabTextFrom({ css: 'h1' });
934
- });
935
- I.fillField('Description', val);
936
- ```
937
-
938
- Functions passed into a session can use the `I` object, page objects, and any other objects declared for the scenario.
939
- This function can also be declared as async (but doesn't work as generator).
940
-
941
- Also, you can use `within` inside a session, but you can't call session from inside `within`.
942
-
943
-
944
- ## Skipping
945
-
946
- Like in Mocha you can use `x` and `only` to skip tests or to run a single test.
947
-
948
- * `xScenario` - skips current test
949
- * `Scenario.skip` - skips current test
950
- * `Scenario.only` - executes only the current test
951
- * `xFeature` - skips current suite <Badge text="Since 2.6.6" type="warning"/>
952
- * `Feature.skip` - skips the current suite <Badge text="Since 2.6.6" type="warning"/>
953
-
954
-
955
- ## Todo Test
956
-
957
- You can use `Scenario.todo` when you are planning on writing tests.
958
-
959
- This test will be skipped like with regular `Scenario.skip` but with additional message "Test not implemented!":
960
-
961
- Use it with a test body as a test plan:
962
-
963
- ```js
964
- Scenario.todo('Test', I => {
965
- /**
966
- * 1. Click to field
967
- * 2. Fill field
968
- *
969
- * Result:
970
- * 3. Field contains text
971
- */
972
- });
973
- ```
974
-
975
- Or even without a test body:
976
-
977
- ```js
978
- Scenario.todo('Test');
979
- ```