codeceptjs 4.0.0-rc.17 → 4.0.0-rc.19

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 (240) hide show
  1. package/bin/codecept.js +15 -2
  2. package/bin/codeceptq.js +49 -0
  3. package/bin/mcp-server.js +733 -196
  4. package/docs/advanced.md +201 -0
  5. package/docs/agents.md +159 -0
  6. package/docs/ai.md +537 -0
  7. package/docs/aitrace.md +266 -0
  8. package/docs/api.md +332 -0
  9. package/docs/assertions.md +415 -0
  10. package/docs/auth.md +318 -0
  11. package/docs/basics.md +424 -0
  12. package/docs/bdd.md +539 -0
  13. package/docs/best.md +240 -0
  14. package/docs/bootstrap.md +132 -0
  15. package/docs/commands.md +352 -0
  16. package/docs/community-helpers.md +63 -0
  17. package/docs/configuration.md +230 -0
  18. package/docs/continuous-integration.md +497 -0
  19. package/docs/custom-helpers.md +297 -0
  20. package/docs/data.md +448 -0
  21. package/docs/debugging.md +332 -0
  22. package/docs/detox.md +235 -0
  23. package/docs/docker.md +136 -0
  24. package/docs/effects.md +179 -0
  25. package/docs/element-based-testing.md +295 -0
  26. package/docs/element-selection.md +125 -0
  27. package/docs/els.md +328 -0
  28. package/docs/examples.md +161 -0
  29. package/docs/heal.md +213 -0
  30. package/docs/helpers/ApiDataFactory.md +267 -0
  31. package/docs/helpers/Appium.md +1405 -0
  32. package/docs/helpers/Detox.md +665 -0
  33. package/docs/helpers/ExpectHelper.md +275 -0
  34. package/docs/helpers/FileSystem.md +152 -0
  35. package/docs/helpers/GraphQL.md +152 -0
  36. package/docs/helpers/GraphQLDataFactory.md +226 -0
  37. package/docs/helpers/JSONResponse.md +255 -0
  38. package/docs/helpers/Mochawesome.md +8 -0
  39. package/docs/helpers/MockRequest.md +377 -0
  40. package/docs/helpers/MockServer.md +212 -0
  41. package/docs/helpers/Playwright.md +2969 -0
  42. package/docs/helpers/Polly.md +44 -0
  43. package/docs/helpers/Protractor.md +1769 -0
  44. package/docs/helpers/Puppeteer-firefox.md +86 -0
  45. package/docs/helpers/Puppeteer.md +2690 -0
  46. package/docs/helpers/REST.md +289 -0
  47. package/docs/helpers/SoftExpectHelper.md +352 -0
  48. package/docs/helpers/WebDriver.md +2682 -0
  49. package/docs/hooks.md +339 -0
  50. package/docs/index.md +111 -0
  51. package/docs/installation.md +83 -0
  52. package/docs/internal-api.md +265 -0
  53. package/docs/internal-test-server.md +89 -0
  54. package/docs/locators.md +355 -0
  55. package/docs/mcp.md +485 -0
  56. package/docs/migration-4.md +556 -0
  57. package/docs/mobile.md +338 -0
  58. package/docs/pageobjects.md +399 -0
  59. package/docs/parallel.md +585 -0
  60. package/docs/playwright.md +714 -0
  61. package/docs/plugins.md +866 -0
  62. package/docs/puppeteer.md +314 -0
  63. package/docs/quickstart.md +120 -0
  64. package/docs/react.md +70 -0
  65. package/docs/reports.md +483 -0
  66. package/docs/retry.md +274 -0
  67. package/docs/secrets.md +150 -0
  68. package/docs/sessions.md +80 -0
  69. package/docs/shadow.md +68 -0
  70. package/docs/test-structure.md +275 -0
  71. package/docs/timeouts.md +183 -0
  72. package/docs/translation.md +247 -0
  73. package/docs/tutorial.md +271 -0
  74. package/docs/typescript.md +374 -0
  75. package/docs/web-element.md +251 -0
  76. package/docs/webdriver.md +708 -0
  77. package/docs/within.md +55 -0
  78. package/lib/aria.js +260 -0
  79. package/lib/command/dryRun.js +23 -3
  80. package/lib/command/init.js +247 -266
  81. package/lib/command/list.js +150 -10
  82. package/lib/command/query.js +218 -0
  83. package/lib/config.js +77 -4
  84. package/lib/container.js +34 -2
  85. package/lib/element/WebElement.js +37 -0
  86. package/lib/globals.js +11 -10
  87. package/lib/helper/Playwright.js +5 -6
  88. package/lib/helper/extras/PlaywrightReactVueLocator.js +45 -36
  89. package/lib/html.js +90 -16
  90. package/lib/index.js +9 -1
  91. package/lib/locator.js +2 -2
  92. package/lib/mocha/factory.js +5 -1
  93. package/lib/mocha/inject.js +1 -1
  94. package/lib/parser.js +2 -2
  95. package/lib/pause.js +38 -4
  96. package/lib/plugin/aiTrace.js +72 -84
  97. package/lib/plugin/browser.js +77 -0
  98. package/lib/plugin/expose.js +159 -0
  99. package/lib/plugin/heal.js +44 -1
  100. package/lib/plugin/pageInfo.js +51 -48
  101. package/lib/plugin/pause.js +131 -0
  102. package/lib/plugin/pauseOnFail.js +10 -34
  103. package/lib/plugin/screencast.js +287 -0
  104. package/lib/plugin/screenshot.js +563 -0
  105. package/lib/plugin/screenshotOnFail.js +8 -170
  106. package/lib/utils/pluginParser.js +151 -0
  107. package/lib/utils/trace.js +297 -0
  108. package/lib/utils.js +25 -0
  109. package/lib/workers.js +1 -15
  110. package/package.json +12 -10
  111. package/typings/index.d.ts +0 -5
  112. package/docs/webapi/amOnPage.mustache +0 -11
  113. package/docs/webapi/appendField.mustache +0 -16
  114. package/docs/webapi/attachFile.mustache +0 -24
  115. package/docs/webapi/blur.mustache +0 -18
  116. package/docs/webapi/checkOption.mustache +0 -13
  117. package/docs/webapi/clearCookie.mustache +0 -9
  118. package/docs/webapi/clearField.mustache +0 -14
  119. package/docs/webapi/click.mustache +0 -29
  120. package/docs/webapi/clickLink.mustache +0 -8
  121. package/docs/webapi/closeCurrentTab.mustache +0 -7
  122. package/docs/webapi/closeOtherTabs.mustache +0 -8
  123. package/docs/webapi/dontSee.mustache +0 -11
  124. package/docs/webapi/dontSeeCheckboxIsChecked.mustache +0 -10
  125. package/docs/webapi/dontSeeCookie.mustache +0 -8
  126. package/docs/webapi/dontSeeCurrentPathEquals.mustache +0 -10
  127. package/docs/webapi/dontSeeCurrentUrlEquals.mustache +0 -10
  128. package/docs/webapi/dontSeeElement.mustache +0 -12
  129. package/docs/webapi/dontSeeElementInDOM.mustache +0 -8
  130. package/docs/webapi/dontSeeInCurrentUrl.mustache +0 -4
  131. package/docs/webapi/dontSeeInField.mustache +0 -16
  132. package/docs/webapi/dontSeeInSource.mustache +0 -8
  133. package/docs/webapi/dontSeeInTitle.mustache +0 -8
  134. package/docs/webapi/dontSeeTraffic.mustache +0 -13
  135. package/docs/webapi/doubleClick.mustache +0 -13
  136. package/docs/webapi/downloadFile.mustache +0 -12
  137. package/docs/webapi/dragAndDrop.mustache +0 -9
  138. package/docs/webapi/dragSlider.mustache +0 -11
  139. package/docs/webapi/executeAsyncScript.mustache +0 -24
  140. package/docs/webapi/executeScript.mustache +0 -26
  141. package/docs/webapi/fillField.mustache +0 -21
  142. package/docs/webapi/flushNetworkTraffics.mustache +0 -5
  143. package/docs/webapi/focus.mustache +0 -13
  144. package/docs/webapi/forceClick.mustache +0 -28
  145. package/docs/webapi/forceRightClick.mustache +0 -18
  146. package/docs/webapi/grabAllWindowHandles.mustache +0 -7
  147. package/docs/webapi/grabAttributeFrom.mustache +0 -10
  148. package/docs/webapi/grabAttributeFromAll.mustache +0 -9
  149. package/docs/webapi/grabBrowserLogs.mustache +0 -9
  150. package/docs/webapi/grabCookie.mustache +0 -11
  151. package/docs/webapi/grabCssPropertyFrom.mustache +0 -11
  152. package/docs/webapi/grabCssPropertyFromAll.mustache +0 -10
  153. package/docs/webapi/grabCurrentUrl.mustache +0 -9
  154. package/docs/webapi/grabCurrentWindowHandle.mustache +0 -6
  155. package/docs/webapi/grabDataFromPerformanceTiming.mustache +0 -20
  156. package/docs/webapi/grabElementBoundingRect.mustache +0 -20
  157. package/docs/webapi/grabGeoLocation.mustache +0 -8
  158. package/docs/webapi/grabHTMLFrom.mustache +0 -10
  159. package/docs/webapi/grabHTMLFromAll.mustache +0 -9
  160. package/docs/webapi/grabNumberOfOpenTabs.mustache +0 -8
  161. package/docs/webapi/grabNumberOfVisibleElements.mustache +0 -9
  162. package/docs/webapi/grabPageScrollPosition.mustache +0 -8
  163. package/docs/webapi/grabPopupText.mustache +0 -5
  164. package/docs/webapi/grabRecordedNetworkTraffics.mustache +0 -10
  165. package/docs/webapi/grabSource.mustache +0 -8
  166. package/docs/webapi/grabTextFrom.mustache +0 -10
  167. package/docs/webapi/grabTextFromAll.mustache +0 -9
  168. package/docs/webapi/grabTitle.mustache +0 -8
  169. package/docs/webapi/grabValueFrom.mustache +0 -9
  170. package/docs/webapi/grabValueFromAll.mustache +0 -8
  171. package/docs/webapi/grabWebElement.mustache +0 -9
  172. package/docs/webapi/grabWebElements.mustache +0 -9
  173. package/docs/webapi/moveCursorTo.mustache +0 -16
  174. package/docs/webapi/openNewTab.mustache +0 -7
  175. package/docs/webapi/pressKey.mustache +0 -12
  176. package/docs/webapi/pressKeyDown.mustache +0 -12
  177. package/docs/webapi/pressKeyUp.mustache +0 -12
  178. package/docs/webapi/pressKeyWithKeyNormalization.mustache +0 -60
  179. package/docs/webapi/refreshPage.mustache +0 -6
  180. package/docs/webapi/resizeWindow.mustache +0 -6
  181. package/docs/webapi/rightClick.mustache +0 -14
  182. package/docs/webapi/saveElementScreenshot.mustache +0 -10
  183. package/docs/webapi/saveScreenshot.mustache +0 -12
  184. package/docs/webapi/say.mustache +0 -10
  185. package/docs/webapi/scrollIntoView.mustache +0 -11
  186. package/docs/webapi/scrollPageToBottom.mustache +0 -6
  187. package/docs/webapi/scrollPageToTop.mustache +0 -6
  188. package/docs/webapi/scrollTo.mustache +0 -12
  189. package/docs/webapi/see.mustache +0 -11
  190. package/docs/webapi/seeAttributesOnElements.mustache +0 -9
  191. package/docs/webapi/seeCheckboxIsChecked.mustache +0 -10
  192. package/docs/webapi/seeCookie.mustache +0 -8
  193. package/docs/webapi/seeCssPropertiesOnElements.mustache +0 -9
  194. package/docs/webapi/seeCurrentPathEquals.mustache +0 -10
  195. package/docs/webapi/seeCurrentUrlEquals.mustache +0 -11
  196. package/docs/webapi/seeElement.mustache +0 -12
  197. package/docs/webapi/seeElementInDOM.mustache +0 -8
  198. package/docs/webapi/seeInCurrentUrl.mustache +0 -8
  199. package/docs/webapi/seeInField.mustache +0 -17
  200. package/docs/webapi/seeInPopup.mustache +0 -8
  201. package/docs/webapi/seeInSource.mustache +0 -7
  202. package/docs/webapi/seeInTitle.mustache +0 -8
  203. package/docs/webapi/seeNumberOfElements.mustache +0 -11
  204. package/docs/webapi/seeNumberOfVisibleElements.mustache +0 -10
  205. package/docs/webapi/seeTextEquals.mustache +0 -9
  206. package/docs/webapi/seeTitleEquals.mustache +0 -8
  207. package/docs/webapi/seeTraffic.mustache +0 -36
  208. package/docs/webapi/selectOption.mustache +0 -26
  209. package/docs/webapi/setCookie.mustache +0 -16
  210. package/docs/webapi/setGeoLocation.mustache +0 -12
  211. package/docs/webapi/startRecordingTraffic.mustache +0 -8
  212. package/docs/webapi/startRecordingWebSocketMessages.mustache +0 -8
  213. package/docs/webapi/stopRecordingTraffic.mustache +0 -5
  214. package/docs/webapi/stopRecordingWebSocketMessages.mustache +0 -7
  215. package/docs/webapi/switchTo.mustache +0 -9
  216. package/docs/webapi/switchToNextTab.mustache +0 -10
  217. package/docs/webapi/switchToPreviousTab.mustache +0 -10
  218. package/docs/webapi/type.mustache +0 -21
  219. package/docs/webapi/uncheckOption.mustache +0 -13
  220. package/docs/webapi/wait.mustache +0 -8
  221. package/docs/webapi/waitForClickable.mustache +0 -11
  222. package/docs/webapi/waitForCookie.mustache +0 -9
  223. package/docs/webapi/waitForDetached.mustache +0 -10
  224. package/docs/webapi/waitForDisabled.mustache +0 -6
  225. package/docs/webapi/waitForElement.mustache +0 -11
  226. package/docs/webapi/waitForEnabled.mustache +0 -6
  227. package/docs/webapi/waitForFunction.mustache +0 -17
  228. package/docs/webapi/waitForInvisible.mustache +0 -10
  229. package/docs/webapi/waitForNumberOfTabs.mustache +0 -9
  230. package/docs/webapi/waitForText.mustache +0 -13
  231. package/docs/webapi/waitForValue.mustache +0 -10
  232. package/docs/webapi/waitForVisible.mustache +0 -10
  233. package/docs/webapi/waitInUrl.mustache +0 -9
  234. package/docs/webapi/waitNumberOfVisibleElements.mustache +0 -10
  235. package/docs/webapi/waitToHide.mustache +0 -10
  236. package/docs/webapi/waitUrlEquals.mustache +0 -10
  237. package/lib/helper/AI.js +0 -214
  238. package/lib/plugin/pauseOn.js +0 -167
  239. package/lib/plugin/stepByStepReport.js +0 -432
  240. package/lib/plugin/subtitles.js +0 -89
@@ -0,0 +1,585 @@
1
+ ---
2
+ permalink: /parallel
3
+ title: Parallel Execution
4
+ ---
5
+
6
+ # Parallel Execution
7
+
8
+ CodeceptJS has multiple approaches for running tests in parallel:
9
+
10
+ - **Test Sharding** - distributes tests across multiple machines for CI matrix execution
11
+ - `run-workers` - which spawns [NodeJS Worker](https://nodejs.org/api/worker_threads.html) in a thread. Tests are split by scenarios, scenarios are mixed between groups, each worker runs tests from its own group.
12
+ - `run-multiple` - which spawns a subprocess with CodeceptJS. Tests are split by files and configured in `codecept.conf.js`.
13
+
14
+ Workers are faster and simpler to start, while `run-multiple` requires additional configuration and can be used to run tests in different browsers at once.
15
+
16
+ ## Test Sharding for CI Matrix
17
+
18
+ Test sharding allows you to split your test suite across multiple machines or CI workers without manual configuration. This is particularly useful for CI/CD pipelines where you want to run tests in parallel across different machines.
19
+
20
+ Use the `--shard` option with the `run` command to execute only a portion of your tests:
21
+
22
+ ```bash
23
+ # Run the first quarter of tests
24
+ npx codeceptjs run --shard 1/4
25
+
26
+ # Run the second quarter of tests
27
+ npx codeceptjs run --shard 2/4
28
+
29
+ # Run the third quarter of tests
30
+ npx codeceptjs run --shard 3/4
31
+
32
+ # Run the fourth quarter of tests
33
+ npx codeceptjs run --shard 4/4
34
+ ```
35
+
36
+ ### CI Matrix Example
37
+
38
+ Here's how you can use test sharding with GitHub Actions matrix strategy:
39
+
40
+ ```yaml
41
+ name: Tests
42
+ on: [push, pull_request]
43
+
44
+ jobs:
45
+ test:
46
+ runs-on: ubuntu-latest
47
+ strategy:
48
+ matrix:
49
+ shard: [1/4, 2/4, 3/4, 4/4]
50
+
51
+ steps:
52
+ - uses: actions/checkout@v2
53
+ - uses: actions/setup-node@v2
54
+ - run: npm install
55
+ - run: npx codeceptjs run --shard ${{ matrix.shard }}
56
+ ```
57
+
58
+ This approach ensures:
59
+
60
+ - Each CI job runs only its assigned portion of tests
61
+ - Tests are distributed evenly across shards
62
+ - No manual configuration or maintenance of test lists
63
+ - Automatic load balancing as you add or remove tests
64
+
65
+ ### Shard Distribution
66
+
67
+ Tests are distributed evenly across shards using a round-robin approach:
68
+
69
+ - If you have 100 tests and 4 shards, each shard runs approximately 25 tests
70
+ - The first shard gets tests 1-25, second gets 26-50, third gets 51-75, fourth gets 76-100
71
+ - If tests don't divide evenly, earlier shards may get one extra test
72
+
73
+ ## Parallel Execution by Workers
74
+
75
+ It is easy to run tests in parallel if you have a lots of tests and free CPU cores. Just execute your tests using `run-workers` command specifying the number of workers to spawn:
76
+
77
+ ```
78
+ npx codeceptjs run-workers 2
79
+ ```
80
+
81
+ > ℹ Workers require NodeJS >= 11.7
82
+
83
+ This command is similar to `run`, however, steps output can't be shown in workers mode, as it is impossible to synchronize steps output from different processes.
84
+
85
+ Each worker spins an instance of CodeceptJS, executes a group of tests, and sends back report to the main process.
86
+
87
+ By default, the tests are assigned one by one to the available workers this may lead to multiple execution of `BeforeSuite()`. Use the option `--suites` to assign the suites one by one to the workers.
88
+
89
+ ```sh
90
+ npx codeceptjs run-workers --suites 2
91
+ ```
92
+
93
+ ### Test Distribution Strategies
94
+
95
+ CodeceptJS supports three different strategies for distributing tests across workers:
96
+
97
+ #### Default Strategy (`--by test`)
98
+
99
+ Tests are pre-assigned to workers at startup, distributing them evenly across all workers. Each worker gets a predetermined set of tests to run.
100
+
101
+ ```sh
102
+ npx codeceptjs run-workers 3 --by test
103
+ ```
104
+
105
+ #### Suite Strategy (`--by suite`)
106
+
107
+ Test suites are pre-assigned to workers, with all tests in a suite running on the same worker. This ensures better test isolation but may lead to uneven load distribution.
108
+
109
+ ```sh
110
+ npx codeceptjs run-workers 3 --by suite
111
+ ```
112
+
113
+ #### Pool Strategy (`--by pool`) - **Recommended for optimal performance**
114
+
115
+ Tests are maintained in a shared pool and distributed dynamically to workers as they become available. This provides the best load balancing and resource utilization.
116
+
117
+ ```sh
118
+ npx codeceptjs run-workers 3 --by pool
119
+ ```
120
+
121
+ ## Dynamic Test Pooling Mode
122
+
123
+ The pool mode enables dynamic test distribution for improved worker load balancing. Instead of pre-assigning tests to workers at startup, tests are stored in a shared pool and distributed on-demand as workers become available.
124
+
125
+ ### Benefits of Pool Mode
126
+
127
+ - **Better load balancing**: Workers never sit idle while others are still running long tests
128
+ - **Improved performance**: Especially beneficial when tests have varying execution times
129
+ - **Optimal resource utilization**: All CPU cores stay busy until the entire test suite is complete
130
+ - **Automatic scaling**: Workers continuously process tests until the pool is empty
131
+
132
+ ### When to Use Pool Mode
133
+
134
+ Pool mode is particularly effective in these scenarios:
135
+
136
+ - **Uneven test execution times**: When some tests take significantly longer than others
137
+ - **Large test suites**: With hundreds or thousands of tests where load balancing matters
138
+ - **Mixed test types**: When combining unit tests, integration tests, and end-to-end tests
139
+ - **CI/CD pipelines**: For consistent and predictable test execution times
140
+
141
+ ### Usage Examples
142
+
143
+ ```bash
144
+ # Basic pool mode with 4 workers
145
+ npx codeceptjs run-workers 4 --by pool
146
+
147
+ # Pool mode with grep filtering
148
+ npx codeceptjs run-workers 3 --by pool --grep "@smoke"
149
+
150
+ # Pool mode in debug mode
151
+ npx codeceptjs run-workers 2 --by pool --debug
152
+
153
+ # Pool mode with specific configuration
154
+ npx codeceptjs run-workers 3 --by pool -c codecept.conf.js
155
+ ```
156
+
157
+ ### How Pool Mode Works
158
+
159
+ 1. **Pool Creation**: All tests are collected into a shared pool of test identifiers
160
+ 2. **Worker Initialization**: The specified number of workers are spawned
161
+ 3. **Dynamic Assignment**: Workers request tests from the pool when they're ready
162
+ 4. **Continuous Processing**: Each worker runs one test, then immediately requests the next
163
+ 5. **Automatic Completion**: Workers exit when the pool is empty and no more tests remain
164
+
165
+ ### Performance Comparison
166
+
167
+ ```bash
168
+ # Traditional mode - tests pre-assigned, some workers may finish early
169
+ npx codeceptjs run-workers 3 --by test # ✓ Good for uniform test times
170
+
171
+ # Suite mode - entire suites assigned to workers
172
+ npx codeceptjs run-workers 3 --by suite # ✓ Good for test isolation
173
+
174
+ # Pool mode - tests distributed dynamically
175
+ npx codeceptjs run-workers 3 --by pool # ✓ Best for mixed test execution times
176
+ ```
177
+
178
+ ## Test stats with Parallel Execution by Workers
179
+
180
+ ```js
181
+ import { event } from 'codeceptjs';
182
+
183
+ export default function() {
184
+
185
+ event.dispatcher.on(event.workers.result, function (result) {
186
+
187
+ console.log(result);
188
+
189
+ });
190
+ }
191
+
192
+ // in console log
193
+ FAIL | 7 passed, 1 failed, 1 skipped // 2s
194
+ {
195
+ "tests": {
196
+ "passed": [
197
+ {
198
+ "type": "test",
199
+ "title": "Assert @C3",
200
+ "body": "() => { }",
201
+ "async": 0,
202
+ "sync": true,
203
+ "_timeout": 2000,
204
+ "_slow": 75,
205
+ "_retries": -1,
206
+ "timedOut": false,
207
+ "_currentRetry": 0,
208
+ "pending": false,
209
+ "opts": {},
210
+ "tags": [
211
+ "@C3"
212
+ ],
213
+ "uid": "xe4q1HdqpRrZG5dPe0JG+A",
214
+ "workerIndex": 3,
215
+ "retries": -1,
216
+ "duration": 493,
217
+ "err": null,
218
+ "parent": {
219
+ "title": "My",
220
+ "ctx": {},
221
+ "suites": [],
222
+ "tests": [],
223
+ "root": false,
224
+ "pending": false,
225
+ "_retries": -1,
226
+ "_beforeEach": [],
227
+ "_beforeAll": [],
228
+ "_afterEach": [],
229
+ "_afterAll": [],
230
+ "_timeout": 2000,
231
+ "_slow": 75,
232
+ "_bail": false,
233
+ "_onlyTests": [],
234
+ "_onlySuites": [],
235
+ "delayed": false
236
+ },
237
+ "steps": [
238
+ {
239
+ "actor": "I",
240
+ "name": "amOnPage",
241
+ "status": "success",
242
+ "args": [
243
+ "https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST"
244
+ ],
245
+ "startedAt": 1698760652610,
246
+ "startTime": 1698760652611,
247
+ "endTime": 1698760653098,
248
+ "finishedAt": 1698760653098,
249
+ "duration": 488
250
+ },
251
+ {
252
+ "actor": "I",
253
+ "name": "grabCurrentUrl",
254
+ "status": "success",
255
+ "args": [],
256
+ "startedAt": 1698760653098,
257
+ "startTime": 1698760653098,
258
+ "endTime": 1698760653099,
259
+ "finishedAt": 1698760653099,
260
+ "duration": 1
261
+ }
262
+ ]
263
+ }
264
+ ],
265
+ "failed": [],
266
+ "skipped": []
267
+ }
268
+ }
269
+ ```
270
+
271
+ CodeceptJS also exposes the env var `process.env.RUNS_WITH_WORKERS` when running tests with `run-workers` command so that you could handle the events better in your plugins/helpers
272
+
273
+ ```js
274
+ import { event } from 'codeceptjs'
275
+
276
+ export default function () {
277
+ // this event would trigger the `_publishResultsToTestrail` when running `run-workers` command
278
+ event.dispatcher.on(event.workers.result, async () => {
279
+ await _publishResultsToTestrail()
280
+ })
281
+
282
+ // this event would not trigger the `_publishResultsToTestrail` multiple times when running `run-workers` command
283
+ event.dispatcher.on(event.all.result, async () => {
284
+ // when running `run` command, this env var is undefined
285
+ if (!process.env.RUNS_WITH_WORKERS) await _publishResultsToTestrail()
286
+ })
287
+ }
288
+ ```
289
+
290
+ ## Parallel Execution by Workers on Multiple Browsers
291
+
292
+ To run tests in parallel across multiple browsers, modify your `codecept.conf.js` file to configure multiple browsers on which you want to run your tests and your tests will run across multiple browsers.
293
+
294
+ Start with modifying the `codecept.conf.js` file. Add multiple key inside the config which will be used to configure multiple profiles.
295
+
296
+ ```
297
+ export const config = {
298
+ helpers: {
299
+ WebDriver: {
300
+ url: 'http://localhost:3000',
301
+ desiredCapabilties: {}
302
+ }
303
+ },
304
+ multiple: {
305
+ profile1: {
306
+ browsers: [
307
+ {
308
+ browser: "firefox",
309
+ desiredCapabilties: {
310
+ // override capabilties related to firefox
311
+ }
312
+ },
313
+ {
314
+ browser: "chrome",
315
+ desiredCapabilties: {
316
+ // override capabilties related to chrome
317
+ }
318
+ }
319
+ ]
320
+ },
321
+ profile2: {
322
+ browsers: [
323
+ {
324
+ browser: "safari",
325
+ desiredCapabilties: {
326
+ // override capabilties related to safari
327
+ }
328
+ }
329
+ ]
330
+ }
331
+ }
332
+ };
333
+ ```
334
+
335
+ To trigger tests on all the profiles configured, you can use the following command:
336
+
337
+ ```
338
+ npx codeceptjs run-workers 3 all -c codecept.conf.js
339
+ ```
340
+
341
+ This will run your tests across all browsers configured from profile1 & profile2 on 3 workers.
342
+
343
+ To trigger tests on specific profile, you can use the following command:
344
+
345
+ ```
346
+ npx codeceptjs run-workers 2 profile1 -c codecept.conf.js
347
+ ```
348
+
349
+ This will run your tests across 2 browsers from profile1 on 2 workers.
350
+
351
+ ## Custom Parallel Execution
352
+
353
+ To get a full control of parallelization create a custom execution script to match your needs.
354
+ This way you can configure which tests are matched, how the groups are formed, and with which configuration each worker is executed.
355
+
356
+ Start with creating file `bin/parallel.js`.
357
+
358
+ On MacOS/Linux run following commands:
359
+
360
+ ```
361
+ mkdir bin
362
+ touch bin/parallel.js
363
+ chmod +x bin/parallel.js
364
+ ```
365
+
366
+ > Filename or directory can be customized. You are creating your own custom runner so take this paragraph as an example.
367
+
368
+ Create a placeholder in file:
369
+
370
+ ```js
371
+ #!/usr/bin/env node
372
+ import { Workers, event } from 'codeceptjs'
373
+ // here will go magic
374
+ ```
375
+
376
+ Now let's see how to update this file for different parallelization modes:
377
+
378
+ ### Example: Running tests in 2 browsers in 4 threads
379
+
380
+ ```js
381
+ const workerConfig = {
382
+ testConfig: './test/data/sandbox/codecept.customworker.js',
383
+ }
384
+
385
+ // don't initialize workers in constructor
386
+ const workers = new Workers(null, workerConfig)
387
+ // split tests by suites in 2 groups
388
+ const testGroups = workers.createGroupsOfSuites(2)
389
+
390
+ const browsers = ['firefox', 'chrome']
391
+
392
+ const configs = browsers.map(browser => {
393
+ return {
394
+ helpers: {
395
+ WebDriver: { browser },
396
+ },
397
+ }
398
+ })
399
+
400
+ for (const config of configs) {
401
+ for (group of testGroups) {
402
+ const worker = workers.spawn()
403
+ worker.addTests(group)
404
+ worker.addConfig(config)
405
+ }
406
+ }
407
+
408
+ // Listen events for failed test
409
+ workers.on(event.test.failed, failedTest => {
410
+ console.log('Failed : ', failedTest.title)
411
+ })
412
+
413
+ // Listen events for passed test
414
+ workers.on(event.test.passed, successTest => {
415
+ console.log('Passed : ', successTest.title)
416
+ })
417
+
418
+ // test run status will also be available in event
419
+ workers.on(event.all.result, () => {
420
+ // Use printResults() to display result with standard style
421
+ workers.printResults()
422
+ })
423
+
424
+ // run workers as async function
425
+ runWorkers()
426
+
427
+ async function runWorkers() {
428
+ try {
429
+ // run bootstrapAll
430
+ await workers.bootstrapAll()
431
+ // run tests
432
+ await workers.run()
433
+ } finally {
434
+ // run teardown All
435
+ await workers.teardownAll()
436
+ }
437
+ }
438
+ ```
439
+
440
+ Inside `event.all.result` you can obtain test results from all workers, so you can customize the report:
441
+
442
+ ```js
443
+ workers.on(event.all.result, result => {
444
+ // print output
445
+ console.log('Test status : ', result.hasFailed() ? 'Failed' : 'Passed');
446
+
447
+ // print stats
448
+ console.log(`Total tests : ${result.stats.tests}`);
449
+ console.log(`Passed tests : ${result.stats.passes}`);
450
+ console.log(`Failed test tests : ${result.stats.failures}`);
451
+
452
+ // If you don't want to listen for failed and passed test separately, use `tests` array
453
+ for (const test of result.tests) {
454
+ console.log(`Test status: ${test.err===null}, `, `Test : ${test.title}`);
455
+ }
456
+ }
457
+ ```
458
+
459
+ ### Example: Running Tests Split By A Custom Function
460
+
461
+ If you want your tests to split according to your need this method is suited for you. For example: If you have 4 long running test files and 4 normal test files there chance all 4 tests end up in same worker thread. For these cases custom function will be helpful.
462
+
463
+ ```js
464
+ /*
465
+ Define a function to split your tests.
466
+
467
+ function should return an array with this format [[file1, file2], [file3], ...]
468
+
469
+ where file1 and file2 will run in a worker thread and file3 will run in a worker thread
470
+ */
471
+ const splitTests = () => {
472
+ const files = [['./test/data/sandbox/guthub_test.js', './test/data/sandbox/devto_test.js'], ['./test/data/sandbox/longrunnig_test.js']]
473
+
474
+ return files
475
+ }
476
+
477
+ const workerConfig = {
478
+ testConfig: './test/data/sandbox/codecept.customworker.js',
479
+ by: splitTests,
480
+ }
481
+
482
+ // don't initialize workers in constructor
483
+ const customWorkers = new Workers(null, workerConfig)
484
+
485
+ customWorkers.run()
486
+
487
+ // You can use event listeners similar to above example.
488
+ customWorkers.on(event.all.result, () => {
489
+ workers.printResults()
490
+ })
491
+ ```
492
+
493
+ ### Emitting messages to the parent worker
494
+
495
+ Child workers can send non-test events to the main process. This is useful if you want to pass along information not related to the tests event cycles itself such as `event.test.success`.
496
+
497
+ ```js
498
+ // inside main process
499
+ // listen for any non test related events
500
+ workers.on('message', data => {
501
+ console.log(data)
502
+ })
503
+
504
+ workers.on(event.all.result, result => {
505
+ // logic
506
+ })
507
+ ```
508
+
509
+ ## Sharing Data Between Workers
510
+
511
+ NodeJS Workers can communicate between each other via messaging system. CodeceptJS allows you to share data between different worker processes using the `share()` and `inject()` functions.
512
+
513
+ ### Basic Usage
514
+
515
+ You can share data directly using the `share()` function and access it using `inject()`:
516
+
517
+ ```js
518
+ // In one test or worker
519
+ share({ userData: { name: 'user', password: '123456' } })
520
+
521
+ // In another test or worker
522
+ const testData = inject()
523
+ console.log(testData.userData.name) // 'user'
524
+ console.log(testData.userData.password) // '123456'
525
+ ```
526
+
527
+ ### Initializing Data in Bootstrap
528
+
529
+ For complex scenarios where you need to initialize shared data before tests run, you can use the bootstrap function:
530
+
531
+ ```js
532
+ // inside codecept.conf.js
533
+ export const config = {
534
+ bootstrap() {
535
+ // Initialize shared data container
536
+ share({ userData: null, config: { retries: 3 } })
537
+ },
538
+ }
539
+ ```
540
+
541
+ Then in your tests, you can check and update the shared data:
542
+
543
+ ```js
544
+ const testData = inject()
545
+ if (!testData.userData) {
546
+ // Update shared data - both approaches work:
547
+ share({ userData: { name: 'user', password: '123456' } })
548
+ // or mutate the injected object:
549
+ testData.userData = { name: 'user', password: '123456' }
550
+ }
551
+ ```
552
+
553
+ ### Working with Proxy Objects
554
+
555
+ Since CodeceptJS 3.7.0+, shared data uses Proxy objects for synchronization between workers. The proxy system works seamlessly for most use cases:
556
+
557
+ ```js
558
+ // ✅ All of these work correctly:
559
+ const data = inject()
560
+ console.log(data.userData.name) // Access nested properties
561
+ console.log(Object.keys(data)) // Enumerate shared keys
562
+ data.newProperty = 'value' // Add new properties
563
+ Object.assign(data, { more: 'data' }) // Merge objects
564
+ ```
565
+
566
+ **Important Note:** Avoid reassigning the entire injected object:
567
+
568
+ ```js
569
+ // ❌ AVOID: This breaks the proxy reference
570
+ let testData = inject()
571
+ testData = someOtherObject // This will NOT work as expected!
572
+
573
+ // ✅ PREFERRED: Use share() to replace data or mutate properties
574
+ share({ userData: someOtherObject }) // This works!
575
+ // or
576
+ Object.assign(inject(), someOtherObject) // This works!
577
+ ```
578
+
579
+ ### Local Data (Worker-Specific)
580
+
581
+ If you want to share data only within the same worker (not across all workers), use the `local` option:
582
+
583
+ ```js
584
+ share({ localData: 'worker-specific' }, { local: true })
585
+ ```