codeceptjs 3.5.0 → 3.5.1-2.beta.7

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 (273) hide show
  1. package/README.md +24 -25
  2. package/lib/actor.js +6 -3
  3. package/lib/ai.js +12 -3
  4. package/lib/cli.js +12 -2
  5. package/lib/codecept.js +4 -0
  6. package/lib/colorUtils.js +10 -0
  7. package/lib/command/definitions.js +2 -7
  8. package/lib/command/dryRun.js +2 -1
  9. package/lib/command/info.js +24 -0
  10. package/lib/command/init.js +51 -5
  11. package/lib/command/run-multiple/collection.js +17 -5
  12. package/lib/command/run-multiple.js +4 -2
  13. package/lib/command/run-workers.js +66 -4
  14. package/lib/command/run.js +7 -0
  15. package/lib/command/workers/runTests.js +39 -0
  16. package/lib/data/context.js +14 -6
  17. package/lib/event.js +4 -0
  18. package/lib/helper/ApiDataFactory.js +2 -1
  19. package/lib/helper/Appium.js +73 -24
  20. package/lib/helper/Expect.js +422 -0
  21. package/lib/helper/FileSystem.js +1 -1
  22. package/lib/helper/GraphQL.js +25 -0
  23. package/lib/helper/Nightmare.js +9 -4
  24. package/lib/helper/OpenAI.js +14 -10
  25. package/lib/helper/Playwright.js +1205 -288
  26. package/lib/helper/Protractor.js +11 -6
  27. package/lib/helper/Puppeteer.js +173 -61
  28. package/lib/helper/TestCafe.js +44 -9
  29. package/lib/helper/WebDriver.js +231 -82
  30. package/lib/helper/errors/ElementNotFound.js +2 -1
  31. package/lib/helper/extras/PlaywrightReactVueLocator.js +38 -0
  32. package/lib/helper/scripts/blurElement.js +17 -0
  33. package/lib/helper/scripts/focusElement.js +17 -0
  34. package/lib/helper/scripts/highlightElement.js +2 -2
  35. package/lib/html.js +3 -3
  36. package/lib/interfaces/bdd.js +1 -1
  37. package/lib/interfaces/gherkin.js +37 -3
  38. package/lib/interfaces/scenarioConfig.js +1 -0
  39. package/lib/locator.js +17 -4
  40. package/lib/mochaFactory.js +2 -1
  41. package/lib/output.js +1 -1
  42. package/lib/pause.js +12 -9
  43. package/lib/plugin/autoLogin.js +45 -10
  44. package/lib/plugin/heal.js +47 -17
  45. package/lib/plugin/retryFailedStep.js +10 -1
  46. package/lib/plugin/retryTo.js +2 -4
  47. package/lib/plugin/selenoid.js +6 -1
  48. package/lib/plugin/standardActingHelpers.js +0 -2
  49. package/lib/plugin/stepByStepReport.js +2 -2
  50. package/lib/plugin/tryTo.js +5 -7
  51. package/lib/plugin/wdio.js +0 -1
  52. package/lib/recorder.js +20 -9
  53. package/lib/session.js +1 -1
  54. package/lib/step.js +30 -11
  55. package/lib/ui.js +1 -0
  56. package/lib/utils.js +18 -1
  57. package/lib/workers.js +28 -3
  58. package/package.json +108 -98
  59. package/translations/de-DE.js +5 -0
  60. package/translations/fr-FR.js +14 -1
  61. package/translations/it-IT.js +1 -0
  62. package/translations/ja-JP.js +5 -0
  63. package/translations/pl-PL.js +5 -0
  64. package/translations/pt-BR.js +1 -0
  65. package/translations/ru-RU.js +1 -0
  66. package/translations/zh-CN.js +5 -0
  67. package/translations/zh-TW.js +5 -0
  68. package/typings/index.d.ts +8 -6
  69. package/typings/promiseBasedTypes.d.ts +784 -822
  70. package/typings/types.d.ts +1214 -727
  71. package/CHANGELOG.md +0 -2492
  72. package/docs/advanced.md +0 -351
  73. package/docs/ai.md +0 -246
  74. package/docs/api.md +0 -323
  75. package/docs/basics.md +0 -980
  76. package/docs/bdd.md +0 -535
  77. package/docs/best.md +0 -237
  78. package/docs/books.md +0 -37
  79. package/docs/bootstrap.md +0 -135
  80. package/docs/build/ApiDataFactory.js +0 -409
  81. package/docs/build/Appium.js +0 -1978
  82. package/docs/build/FileSystem.js +0 -228
  83. package/docs/build/GraphQL.js +0 -204
  84. package/docs/build/GraphQLDataFactory.js +0 -309
  85. package/docs/build/JSONResponse.js +0 -338
  86. package/docs/build/Mochawesome.js +0 -71
  87. package/docs/build/Nightmare.js +0 -2147
  88. package/docs/build/OpenAI.js +0 -122
  89. package/docs/build/Playwright.js +0 -4134
  90. package/docs/build/Polly.js +0 -42
  91. package/docs/build/Protractor.js +0 -2701
  92. package/docs/build/Puppeteer.js +0 -3743
  93. package/docs/build/REST.js +0 -344
  94. package/docs/build/SeleniumWebdriver.js +0 -76
  95. package/docs/build/TestCafe.js +0 -2059
  96. package/docs/build/WebDriver.js +0 -4042
  97. package/docs/changelog.md +0 -2501
  98. package/docs/commands.md +0 -254
  99. package/docs/community-helpers.md +0 -58
  100. package/docs/configuration.md +0 -157
  101. package/docs/continuous-integration.md +0 -22
  102. package/docs/custom-helpers.md +0 -306
  103. package/docs/data.md +0 -375
  104. package/docs/detox.md +0 -235
  105. package/docs/docker.md +0 -137
  106. package/docs/email.md +0 -183
  107. package/docs/examples.md +0 -149
  108. package/docs/helpers/ApiDataFactory.md +0 -266
  109. package/docs/helpers/Appium.md +0 -1317
  110. package/docs/helpers/Detox.md +0 -586
  111. package/docs/helpers/FileSystem.md +0 -152
  112. package/docs/helpers/GraphQL.md +0 -130
  113. package/docs/helpers/GraphQLDataFactory.md +0 -226
  114. package/docs/helpers/JSONResponse.md +0 -254
  115. package/docs/helpers/Mochawesome.md +0 -8
  116. package/docs/helpers/MockRequest.md +0 -377
  117. package/docs/helpers/Nightmare.md +0 -1258
  118. package/docs/helpers/OpenAI.md +0 -70
  119. package/docs/helpers/Playwright.md +0 -2250
  120. package/docs/helpers/Polly.md +0 -44
  121. package/docs/helpers/Puppeteer-firefox.md +0 -86
  122. package/docs/helpers/Puppeteer.md +0 -2147
  123. package/docs/helpers/REST.md +0 -218
  124. package/docs/helpers/TestCafe.md +0 -1224
  125. package/docs/helpers/WebDriver.md +0 -2325
  126. package/docs/hooks.md +0 -340
  127. package/docs/index.md +0 -111
  128. package/docs/installation.md +0 -75
  129. package/docs/internal-api.md +0 -265
  130. package/docs/locators.md +0 -331
  131. package/docs/mobile-react-native-locators.md +0 -67
  132. package/docs/mobile.md +0 -344
  133. package/docs/nightmare.md +0 -223
  134. package/docs/pageobjects.md +0 -291
  135. package/docs/parallel.md +0 -288
  136. package/docs/playwright.md +0 -609
  137. package/docs/plugins.md +0 -1225
  138. package/docs/puppeteer.md +0 -316
  139. package/docs/quickstart.md +0 -163
  140. package/docs/react.md +0 -69
  141. package/docs/reports.md +0 -392
  142. package/docs/secrets.md +0 -36
  143. package/docs/shadow.md +0 -68
  144. package/docs/shared/keys.mustache +0 -31
  145. package/docs/shared/react.mustache +0 -1
  146. package/docs/testcafe.md +0 -174
  147. package/docs/translation.md +0 -247
  148. package/docs/tutorial.md +0 -271
  149. package/docs/typescript.md +0 -180
  150. package/docs/ui.md +0 -59
  151. package/docs/videos.md +0 -28
  152. package/docs/visual.md +0 -202
  153. package/docs/vue.md +0 -121
  154. package/docs/webapi/amOnPage.mustache +0 -11
  155. package/docs/webapi/appendField.mustache +0 -11
  156. package/docs/webapi/attachFile.mustache +0 -12
  157. package/docs/webapi/checkOption.mustache +0 -13
  158. package/docs/webapi/clearCookie.mustache +0 -10
  159. package/docs/webapi/clearField.mustache +0 -9
  160. package/docs/webapi/click.mustache +0 -25
  161. package/docs/webapi/clickLink.mustache +0 -8
  162. package/docs/webapi/closeCurrentTab.mustache +0 -7
  163. package/docs/webapi/closeOtherTabs.mustache +0 -8
  164. package/docs/webapi/dontSee.mustache +0 -11
  165. package/docs/webapi/dontSeeCheckboxIsChecked.mustache +0 -10
  166. package/docs/webapi/dontSeeCookie.mustache +0 -8
  167. package/docs/webapi/dontSeeCurrentUrlEquals.mustache +0 -10
  168. package/docs/webapi/dontSeeElement.mustache +0 -8
  169. package/docs/webapi/dontSeeElementInDOM.mustache +0 -8
  170. package/docs/webapi/dontSeeInCurrentUrl.mustache +0 -4
  171. package/docs/webapi/dontSeeInField.mustache +0 -11
  172. package/docs/webapi/dontSeeInSource.mustache +0 -8
  173. package/docs/webapi/dontSeeInTitle.mustache +0 -8
  174. package/docs/webapi/doubleClick.mustache +0 -13
  175. package/docs/webapi/downloadFile.mustache +0 -12
  176. package/docs/webapi/dragAndDrop.mustache +0 -9
  177. package/docs/webapi/dragSlider.mustache +0 -11
  178. package/docs/webapi/executeAsyncScript.mustache +0 -24
  179. package/docs/webapi/executeScript.mustache +0 -26
  180. package/docs/webapi/fillField.mustache +0 -16
  181. package/docs/webapi/forceClick.mustache +0 -28
  182. package/docs/webapi/forceRightClick.mustache +0 -18
  183. package/docs/webapi/grabAllWindowHandles.mustache +0 -7
  184. package/docs/webapi/grabAttributeFrom.mustache +0 -10
  185. package/docs/webapi/grabAttributeFromAll.mustache +0 -9
  186. package/docs/webapi/grabBrowserLogs.mustache +0 -9
  187. package/docs/webapi/grabCookie.mustache +0 -11
  188. package/docs/webapi/grabCssPropertyFrom.mustache +0 -11
  189. package/docs/webapi/grabCssPropertyFromAll.mustache +0 -10
  190. package/docs/webapi/grabCurrentUrl.mustache +0 -9
  191. package/docs/webapi/grabCurrentWindowHandle.mustache +0 -6
  192. package/docs/webapi/grabDataFromPerformanceTiming.mustache +0 -20
  193. package/docs/webapi/grabElementBoundingRect.mustache +0 -20
  194. package/docs/webapi/grabGeoLocation.mustache +0 -8
  195. package/docs/webapi/grabHTMLFrom.mustache +0 -10
  196. package/docs/webapi/grabHTMLFromAll.mustache +0 -9
  197. package/docs/webapi/grabNumberOfOpenTabs.mustache +0 -8
  198. package/docs/webapi/grabNumberOfVisibleElements.mustache +0 -9
  199. package/docs/webapi/grabPageScrollPosition.mustache +0 -8
  200. package/docs/webapi/grabPopupText.mustache +0 -5
  201. package/docs/webapi/grabSource.mustache +0 -8
  202. package/docs/webapi/grabTextFrom.mustache +0 -10
  203. package/docs/webapi/grabTextFromAll.mustache +0 -9
  204. package/docs/webapi/grabTitle.mustache +0 -8
  205. package/docs/webapi/grabValueFrom.mustache +0 -9
  206. package/docs/webapi/grabValueFromAll.mustache +0 -8
  207. package/docs/webapi/moveCursorTo.mustache +0 -12
  208. package/docs/webapi/openNewTab.mustache +0 -7
  209. package/docs/webapi/pressKey.mustache +0 -12
  210. package/docs/webapi/pressKeyDown.mustache +0 -12
  211. package/docs/webapi/pressKeyUp.mustache +0 -12
  212. package/docs/webapi/pressKeyWithKeyNormalization.mustache +0 -60
  213. package/docs/webapi/refreshPage.mustache +0 -6
  214. package/docs/webapi/resizeWindow.mustache +0 -6
  215. package/docs/webapi/rightClick.mustache +0 -14
  216. package/docs/webapi/saveElementScreenshot.mustache +0 -10
  217. package/docs/webapi/saveScreenshot.mustache +0 -12
  218. package/docs/webapi/say.mustache +0 -10
  219. package/docs/webapi/scrollIntoView.mustache +0 -11
  220. package/docs/webapi/scrollPageToBottom.mustache +0 -6
  221. package/docs/webapi/scrollPageToTop.mustache +0 -6
  222. package/docs/webapi/scrollTo.mustache +0 -12
  223. package/docs/webapi/see.mustache +0 -11
  224. package/docs/webapi/seeAttributesOnElements.mustache +0 -9
  225. package/docs/webapi/seeCheckboxIsChecked.mustache +0 -10
  226. package/docs/webapi/seeCookie.mustache +0 -8
  227. package/docs/webapi/seeCssPropertiesOnElements.mustache +0 -9
  228. package/docs/webapi/seeCurrentUrlEquals.mustache +0 -11
  229. package/docs/webapi/seeElement.mustache +0 -8
  230. package/docs/webapi/seeElementInDOM.mustache +0 -8
  231. package/docs/webapi/seeInCurrentUrl.mustache +0 -8
  232. package/docs/webapi/seeInField.mustache +0 -12
  233. package/docs/webapi/seeInPopup.mustache +0 -8
  234. package/docs/webapi/seeInSource.mustache +0 -7
  235. package/docs/webapi/seeInTitle.mustache +0 -8
  236. package/docs/webapi/seeNumberOfElements.mustache +0 -11
  237. package/docs/webapi/seeNumberOfVisibleElements.mustache +0 -10
  238. package/docs/webapi/seeTextEquals.mustache +0 -9
  239. package/docs/webapi/seeTitleEquals.mustache +0 -8
  240. package/docs/webapi/selectOption.mustache +0 -21
  241. package/docs/webapi/setCookie.mustache +0 -16
  242. package/docs/webapi/setGeoLocation.mustache +0 -12
  243. package/docs/webapi/switchTo.mustache +0 -9
  244. package/docs/webapi/switchToNextTab.mustache +0 -10
  245. package/docs/webapi/switchToPreviousTab.mustache +0 -10
  246. package/docs/webapi/type.mustache +0 -21
  247. package/docs/webapi/uncheckOption.mustache +0 -13
  248. package/docs/webapi/wait.mustache +0 -8
  249. package/docs/webapi/waitForClickable.mustache +0 -11
  250. package/docs/webapi/waitForDetached.mustache +0 -10
  251. package/docs/webapi/waitForElement.mustache +0 -11
  252. package/docs/webapi/waitForEnabled.mustache +0 -6
  253. package/docs/webapi/waitForFunction.mustache +0 -17
  254. package/docs/webapi/waitForInvisible.mustache +0 -10
  255. package/docs/webapi/waitForText.mustache +0 -13
  256. package/docs/webapi/waitForValue.mustache +0 -10
  257. package/docs/webapi/waitForVisible.mustache +0 -10
  258. package/docs/webapi/waitInUrl.mustache +0 -9
  259. package/docs/webapi/waitNumberOfVisibleElements.mustache +0 -10
  260. package/docs/webapi/waitToHide.mustache +0 -10
  261. package/docs/webapi/waitUrlEquals.mustache +0 -10
  262. package/docs/webdriver.md +0 -657
  263. package/docs/wiki/Books-&-Posts.md +0 -27
  264. package/docs/wiki/Community-Helpers-&-Plugins.md +0 -49
  265. package/docs/wiki/Converting-Playwright-to-Istanbul-Coverage.md +0 -29
  266. package/docs/wiki/Examples.md +0 -139
  267. package/docs/wiki/Google-Summer-of-Code-(GSoC)-2020.md +0 -68
  268. package/docs/wiki/Home.md +0 -16
  269. package/docs/wiki/Release-Process.md +0 -24
  270. package/docs/wiki/Roadmap.md +0 -23
  271. package/docs/wiki/Tests.md +0 -1393
  272. package/docs/wiki/Upgrading-to-CodeceptJS-3.md +0 -153
  273. package/docs/wiki/Videos.md +0 -19
@@ -1,609 +0,0 @@
1
- ---
2
- permalink: /playwright
3
- title: Testing with Playwright
4
- ---
5
-
6
- # Testing with Playwright
7
-
8
- Playwright is a Node library to automate the [Chromium](https://www.chromium.org/Home), [WebKit](https://webkit.org/) and [Firefox](https://www.mozilla.org/en-US/firefox/new/) browsers as well as [Electron](https://www.electronjs.org/) apps with a single API. It enables **cross-browser** web automation that is **ever-green**, **capable**, **reliable** and **fast**.
9
-
10
- Playwright was built similarly to [Puppeteer](https://github.com/puppeteer/puppeteer), using its API and so is very different in usage. However, Playwright has cross browser support with better design for test automation.
11
-
12
- Take a look at a sample test:
13
-
14
- ```js
15
- I.amOnPage('https://github.com');
16
- I.click('Sign in', '//html/body/div[1]/header');
17
- I.see('Sign in to GitHub', 'h1');
18
- I.fillField('Username or email address', 'something@totest.com');
19
- I.fillField('Password', '123456');
20
- I.click('Sign in');
21
- I.see('Incorrect username or password.', '.flash-error');
22
- ```
23
-
24
- It's readable and simple and working using Playwright API!
25
-
26
- ## Setup
27
-
28
- To start you need CodeceptJS with Playwright packages installed
29
-
30
- ```bash
31
- npm install codeceptjs playwright --save
32
- ```
33
-
34
- Or see [alternative installation options](https://codecept.io/installation/)
35
-
36
- > If you already have CodeceptJS project, just install `playwright` package and enable a helper it in config.
37
-
38
- And a basic project initialized
39
-
40
- ```sh
41
- npx codeceptjs init
42
- ```
43
-
44
- You will be asked for a Helper to use, you should select Playwright and provide url of a website you are testing.
45
-
46
- ## Configuring
47
-
48
- Make sure `Playwright` helper is enabled in `codecept.conf.js` config:
49
-
50
- ```js
51
- { // ..
52
- helpers: {
53
- Playwright: {
54
- url: "http://localhost",
55
- show: true,
56
- browser: 'chromium'
57
- }
58
- }
59
- // ..
60
- }
61
- ```
62
-
63
- > Turn off the `show` option if you want to run test in headless mode.
64
- > If you don't specify the browser here, `chromium` will be used. Possible browsers are: `chromium`, `firefox` and `webkit`
65
-
66
- Playwright uses different strategies to detect if a page is loaded. In configuration use `waitForNavigation` option for that:
67
-
68
- When to consider navigation succeeded, defaults to `load`. Given an array of event strings, navigation is considered to be successful after all events have been fired. Events can be either:
69
- - `load` - consider navigation to be finished when the load event is fired.
70
- - `domcontentloaded` - consider navigation to be finished when the DOMContentLoaded event is fired.
71
- - `networkidle` - consider navigation to be finished when there are no network connections for at least 500 ms.
72
-
73
- ```js
74
- helpers: {
75
- Playwright: {
76
- url: "http://localhost",
77
- show: true,
78
- browser: 'chromium',
79
- waitForNavigation: "networkidle0"
80
- }
81
- }
82
- ```
83
-
84
- When a test runs faster than application it is recommended to increase `waitForAction` config value.
85
- It will wait for a small amount of time (100ms) by default after each user action is taken.
86
-
87
- > ▶ More options are listed in [helper reference](https://codecept.io/helpers/Playwright/).
88
-
89
- ## Writing Tests
90
-
91
- Additional CodeceptJS tests should be created with `gt` command:
92
-
93
- ```sh
94
- npx codeceptjs gt
95
- ```
96
-
97
- As an example we will use `ToDoMvc` app for testing.
98
-
99
- ### Actions
100
-
101
- Tests consist with a scenario of user's action taken on a page. The most widely used ones are:
102
-
103
- * `amOnPage` - to open a webpage (accepts relative or absolute url)
104
- * `click` - to locate a button or link and click on it
105
- * `fillField` - to enter a text inside a field
106
- * `selectOption`, `checkOption` - to interact with a form
107
- * `wait*` to wait for some parts of page to be fully rendered (important for testing SPA)
108
- * `grab*` to get values from page sources
109
- * `see`, `dontSee` - to check for a text on a page
110
- * `seeElement`, `dontSeeElement` - to check for elements on a page
111
-
112
- > ℹ All actions are listed in [Playwright helper reference](https://codecept.io/helpers/Playwright/).*
113
-
114
- All actions which interact with elements can use **[CSS or XPath locators](https://codecept.io/locators/#css-and-xpath)**. Actions like `click` or `fillField` can locate elements by their name or value on a page:
115
-
116
- ```js
117
- // search for link or button
118
- I.click('Login');
119
- // locate field by its label
120
- I.fillField('Name', 'Miles');
121
- // we can use input name
122
- I.fillField('user[email]','miles@davis.com');
123
- ```
124
-
125
- You can also specify the exact locator type with strict locators:
126
-
127
- ```js
128
- I.click({css: 'button.red'});
129
- I.fillField({name: 'user[email]'},'miles@davis.com');
130
- I.seeElement({xpath: '//body/header'});
131
- ```
132
-
133
- ### Interactive Pause
134
-
135
- It's easy to start writing a test if you use [interactive pause](/basics#debug). Just open a web page and pause execution.
136
-
137
- ```js
138
- Feature('Sample Test');
139
-
140
- Scenario('open my website', ({ I }) => {
141
- I.amOnPage('http://todomvc.com/examples/react/');
142
- pause();
143
- });
144
- ```
145
-
146
- This is just enough to run a test, open a browser, and think what to do next to write a test case.
147
-
148
- When you execute such test with `codeceptjs run` command you may see the browser is started
149
-
150
- ```
151
- npx codeceptjs run --steps
152
- ```
153
-
154
- After a page is opened a full control of a browser is given to a terminal. Type in different commands such as `click`, `see`, `fillField` to write the test. A successful commands will be saved to `./output/cli-history` file and can be copied into a test.
155
-
156
- A complete ToDo-MVC test may look like:
157
-
158
- ```js
159
- Feature('ToDo');
160
-
161
- Scenario('create todo item', ({ I }) => {
162
- I.amOnPage('http://todomvc.com/examples/react/');
163
- I.dontSeeElement('.todo-count');
164
- I.fillField('What needs to be done?', 'Write a guide');
165
- I.pressKey('Enter');
166
- I.see('Write a guide', '.todo-list');
167
- I.see('1 item left', '.todo-count');
168
- });
169
- ```
170
-
171
- ### Grabbers
172
-
173
- If you need to get element's value inside a test you can use `grab*` methods. They should be used with `await` operator inside `async` function:
174
-
175
- ```js
176
- const assert = require('assert');
177
- Scenario('get value of current tasks', async ({ I }) => {
178
- I.fillField('.todo', 'my first item');
179
- I.pressKey('Enter')
180
- I.fillField('.todo', 'my second item');
181
- I.pressKey('Enter')
182
- let numTodos = await I.grabTextFrom('.todo-count strong');
183
- assert.equal(2, numTodos);
184
- });
185
- ```
186
-
187
- ### Within
188
-
189
- In case some actions should be taken inside one element (a container or modal window or iframe) you can use `within` block to narrow the scope.
190
- Please take a note that you can't use within inside another within in Playwright helper:
191
-
192
- ```js
193
- await within('.todoapp', () => {
194
- I.fillField('.todo', 'my new item');
195
- I.pressKey('Enter')
196
- I.see('1 item left', '.todo-count');
197
- I.click('.todo-list input.toggle');
198
- });
199
- I.see('0 items left', '.todo-count');
200
- ```
201
-
202
- ### Each Element <Badge text="Since 3.3" type="warning"/>
203
-
204
- Usually, CodeceptJS performs an action on the first matched element.
205
- In case you want to do an action on each element found, use the special function `eachElement` which comes from [eachElement](https://codecept.io/plugins/#eachelement) plugin.
206
-
207
- `eachElement` function matches all elements by locator and performs a callback on each of those element. A callback function receives [ElementHandle instance](https://playwright.dev/docs/api/class-elementhandle) from Playwright API. `eachElement` may perform arbitrary actions on a page, so the first argument should by a description of the actions performed. This description will be used for logging purposes.
208
-
209
- Usage example
210
-
211
- ```js
212
- await eachElement(
213
- 'tick all checkboxes',
214
- 'input.custom-checkbox',
215
- async (el, index) => {
216
- await el.check();
217
- });
218
- );
219
- ```
220
-
221
- > ℹ Learn more about [eachElement plugin](/plugins/#eachelement)
222
-
223
- ## Multi Session Testing
224
-
225
- To launch additional browser context (or incognito window) use `session` command.
226
-
227
- ```js
228
- Scenario('I try to open this site as anonymous user', ({ I }) => {
229
- I.amOnPage('/');
230
- I.dontSee('Agree to cookies');
231
- session('anonymous user', () => {
232
- I.amOnPage('/');
233
- I.see('Agree to cookies');
234
- });
235
- })
236
- ```
237
-
238
- > ℹ Learn more about [multi-session testing](/basics/#multiple-sessions)
239
-
240
- ## Electron Testing
241
-
242
- CodeceptJS allows you to make use of [Playwright's Electron flavor](https://github.com/microsoft/playwright/blob/master/packages/playwright-electron/README.md).
243
- To use this functionality, all you need to do is set the browser to `electron` in the CodeceptJS configuration file and, according to the [Playwright BrowserType API](https://playwright.dev/docs/api/class-browsertype/#browsertypelaunchoptions), set the launch options to point to your Electron application.
244
-
245
- `main.js` - main Electron application file
246
- ```js
247
- const { app, BrowserWindow } = require("electron");
248
-
249
- function createWindow() {
250
- const window = new BrowserWindow({ width: 800, height: 600 });
251
- window.loadURL("https://example.com");
252
- }
253
-
254
- app.whenReady().then(createWindow);
255
- ```
256
-
257
- `codecept.conf.js` - CodeceptJS configuration file
258
- ```js
259
- const path = require("path");
260
-
261
- exports.config = {
262
- helpers: {
263
- Playwright: {
264
- browser: "electron",
265
- electron: {
266
- executablePath: require("electron"),
267
- args: [path.join(__dirname, "main.js")],
268
- },
269
- },
270
- },
271
- // rest of config
272
- }
273
- ```
274
-
275
- ### Headless Mode
276
-
277
- With Electron, headless mode must be set when creating the window. Therefore, CodeceptJS's `show` configuration parameter will not work. However, you can set it in the `main.js` file as shown below:
278
-
279
- ```js
280
- function createWindow() {
281
- const window = new BrowserWindow({ width: 800, height: 600, show: false });
282
- window.loadURL("https://example.com");
283
- }
284
- ```
285
-
286
-
287
- ## Device Emulation
288
-
289
- Playwright can emulate browsers of mobile devices. Instead of paying for expensive devices for mobile tests you can adjust Playwright settings so it could emulate mobile browsers on iPhone, Samsung Galaxy, etc.
290
-
291
- Device emulation can be enabled in CodeceptJS globally in a config or per session.
292
-
293
- Playwright contains a [list of predefined devices](https://github.com/microsoft/playwright/blob/master/src/server/deviceDescriptors.js) to emulate, for instance this is how you can enable iPhone 6 emulation for all tests:
294
-
295
- ```js
296
- const { devices } = require('playwright');
297
-
298
- helpers: {
299
- Playwright: {
300
- // regular config goes here
301
- emulate: devices['iPhone 6'],
302
- }
303
- }
304
- ```
305
- To adjust browser settings you can pass [custom options](https://github.com/microsoft/playwright/blob/master/docs/src/api/class-browsercontext.md)
306
-
307
- ```js
308
- helpers: {
309
- Playwright: {
310
- // regular config goes here
311
- // put on mobile device
312
- emulate: { isMobile: true, deviceScaleFactor: 2 }
313
- }
314
- }
315
- ```
316
-
317
- To enable device emulation for a specific test, create an additional browser session and pass in config as a second parameter:
318
-
319
- ```js
320
- const { devices } = require('playwright');
321
-
322
- Scenario('website looks nice on iPhone', () => {
323
- session('mobile user', devices['iPhone 6'], () => {
324
- I.amOnPage('/');
325
- I.see('Hello, iPhone user!')
326
- })
327
- });
328
- ```
329
-
330
- ## API Requests
331
-
332
- CodeceptJS has [REST](/helpers/REST) and [GraphQL]((/helpers/GraphQL)) helpers to perform requests to external APIs. This may be helpful to implement [data management](https://codecept.io/data/) strategy.
333
-
334
- However, Playwright since 1.18 has its own [API for making request](https://playwright.dev/docs/api/class-apirequestcontext#api-request-context-get). It uses cookies from browser session to authenticate requests. So you can use it via [`makeApiRequest`](/helpers/Playwright#makeApiRequest) method:
335
-
336
- ```js
337
- I.makeApiRequest('GET', '/users')
338
- ```
339
-
340
- It is also possible to test JSON responses by adding [`JSONResponse`](/helpers/JSONResponse) and connecting it to Playwright:
341
-
342
- ```js
343
- // inside codecept.conf.js
344
- {
345
- helpers: {
346
- Playwright: {
347
- // current config
348
- },
349
- JSONResponse: {
350
- requestHelper: 'Playwright',
351
- }
352
- }
353
- }
354
- ```
355
- This helper provides you methods for [API testing](/api). For instance, you can check for status code, data inclusion and structure:
356
-
357
- ```js
358
- I.makeApiRequest('GET', '/users/1');
359
- I.seeResponseCodeIs(200);
360
- I.seeResponseContainsKeys(['user']);
361
- ```
362
- This way you can do full fledged API testing via Playwright.
363
-
364
- ## Accessing Playwright API
365
-
366
- To get [Playwright API](https://playwright.dev/docs/api/class-playwright) inside a test use `I.usePlaywrightTo` method with a callback.
367
-
368
- `usePlaywrightTo` passes in an instance of Playwright helper from which you can obtain access to main Playwright classes:
369
-
370
- * [`browser`](https://playwright.dev/docs/api/class-browser)
371
- * [`browserContext`](https://playwright.dev/docs/api/class-browsercontext)
372
- * [`page`](https://playwright.dev/docs/api/class-page)
373
-
374
- To keep test readable provide a description of a callback inside the first parameter.
375
-
376
- ```js
377
- I.usePlaywrightTo('emulate offline mode', async ({ browser, browserContext, page }) => {
378
- // use browser, page, context objects inside this function
379
- await browserContext.setOffline(true);
380
- });
381
- ```
382
-
383
- Playwright commands are asynchronous so a callback function must be async.
384
-
385
- A Playwright helper is passed as argument for callback, so you can combine Playwright API with CodeceptJS API:
386
-
387
- ```js
388
- I.usePlaywrightTo('emulate offline mode', async (Playwright) => {
389
- // access internal objects browser, page, context of helper
390
- await Playwright.browserContext.setOffline(true);
391
- // call a method of helper, await is required here
392
- await Playwright.click('Reload');
393
- });
394
- ```
395
-
396
- ## Mocking Network Requests <Badge text="Since 3.1" type="warning"/>
397
-
398
- Network requests & responses can be mocked and modified. Use `mockRequest` which strictly follows [Playwright's `route` API](https://playwright.dev/docs/api/class-browsercontext#browser-context-route).
399
-
400
- ```js
401
- I.mockRoute('/api/**', route => {
402
- if (route.request().postData().includes('my-string'))
403
- route.fulfill({ body: 'mocked-data' });
404
- else
405
- route.continue();
406
- });
407
-
408
- I.mockRoute('**/*.{png,jpg,jpeg}', route => route.abort());
409
-
410
- // To disable mocking for a route call `stopMockingRoute`
411
- // for previously mocked URL
412
- I.stopMockingRoute('**/*.{png,jpg,jpeg}'
413
- ```
414
-
415
- To master request intercepting [use `route` object](https://playwright.dev/docs/api/class-route) object passed into mock request handler.
416
-
417
- ## Video <Badge text="Since 3.1" type="warning"/>
418
-
419
- Playwright may record videos for failed tests. This can be enabled in a config with `video: true` option:
420
-
421
- ```js
422
- exports.config = {
423
- helpers: {
424
- Playwright: {
425
- // ...
426
- video: true
427
- }
428
- }
429
- }
430
- ```
431
- When a test fails and video was enabled a video file is shown under the `artifacts` section in the error message:
432
-
433
- ```
434
- -- FAILURES:
435
-
436
- 1) GitHub
437
- open:
438
-
439
- Scenario Steps:
440
- - I.amOnPage("https://gothub11.com/search") at Test.<anonymous> (./github_test.js:16:5)
441
-
442
- Artifacts:
443
- - screenshot: /home/davert/projects/codeceptjs/examples/output/open.failed.png
444
- - video: /home/davert/projects/codeceptjs/examples/output/videos/5ecf6aaa78865bce14d271b55de964fd.webm
445
- ```
446
-
447
- Open video and use it to debug a failed test case. Video helps when running tests on CI. Configure your CI system to enable artifacts storage for `output/video` and review videos of failed test case to understand failures.
448
-
449
- It is recommended to enable [subtitles](https://codecept.io/plugins/#subtitles) plugin which will generate subtitles from steps in `.srt` format. Subtitles file will be saved into after a video file so video player (like VLC) would load them automatically:
450
-
451
- ![](https://user-images.githubusercontent.com/220264/131644090-38d1ca55-1ba1-41fa-8fd1-7dea2b7ae995.png)
452
-
453
-
454
- ## Trace <Badge text="Since 3.1" type="warning"/>
455
-
456
- If video is not enough to descover why a test failed a [trace](https://playwright.dev/docs/trace-viewer/) can be recorded.
457
-
458
- ![](https://user-images.githubusercontent.com/220264/128403246-7e1b9b33-9ce2-42d5-b87b-b8749d5d7a78.png)
459
-
460
- Inside a trace you get screenshots, DOM snapshots, console logs, network requests and playwright commands logged and showed on a timeline. This may help for a deep debug of a failed test cases. Trace file is saved into ZIP archive and can be viewed with Trace Viewer built into Playwright.
461
-
462
-
463
- Enable trace with `trace: true` option in a config:
464
-
465
- ```js
466
- exports.config = {
467
- helpers: {
468
- Playwright: {
469
- // ...
470
- trace: true
471
- }
472
- }
473
- }
474
- ```
475
-
476
- When a test fails and trace was enabled, a trace file is shown under the `artifacts` section in the error message:
477
-
478
- ```
479
- -- FAILURES:
480
-
481
- 1) GitHub
482
- open:
483
-
484
- Scenario Steps:
485
- - I.amOnPage("https://gothub11.com/search") at Test.<anonymous> (./github_test.js:16:5)
486
-
487
- Artifacts:
488
- - screenshot: /home/davert/projects/codeceptjs/examples/output/open.failed.png
489
- - trace: /home/davert/projects/codeceptjs/examples/output/trace/open.zip
490
- ```
491
-
492
- Use Playwright's trace viewer to analyze the trace:
493
-
494
- ```
495
- npx playwright show-trace {path-to-trace-file}
496
- ```
497
-
498
- For instance, this is how you can read a trace for a failed test from an example:
499
-
500
- ```
501
- npx playwright show-trace /home/davert/projects/codeceptjs/examples/output/trace/open.zip
502
- ```
503
-
504
- ## Capturing Code Coverage
505
-
506
- Code coverage can be captured, by enabling the `coverage` plugin in `codecept.config.js`.
507
-
508
- ```js
509
- {
510
- plugins: {
511
- coverage: {
512
- enabled: true
513
- }
514
- }
515
- }
516
- ```
517
-
518
- Once all the tests are completed, `codecept` will create and store coverage in `output/coverage` folder, as shown below.
519
-
520
- ![](https://user-images.githubusercontent.com/16587779/131362352-30ee9c51-705f-4098-b665-53035ea9275f.png)
521
-
522
- Then you need to [convert code coverage from Playwright's format into Istanbul format](https://github.com/codeceptjs/CodeceptJS/wiki/Converting-Playwright-to-Istanbul-Coverage).
523
-
524
- Once the istanbul compatible coverage is generated, use [`nyc`](https://www.npmjs.com/package/nyc) to generate your coverage report in your desired format.
525
-
526
- ```
527
- npx nyc report --reporter html -t coverage
528
- ```
529
-
530
- The above command will generate will generate coverage in an interactive html format. It should generate `html` files in the directory where your code coverage is present, something like shown below.
531
-
532
- ![](https://user-images.githubusercontent.com/16587779/131858419-cbc7df7d-0851-47b9-b086-b5e3b9165674.png)
533
-
534
- Open `index.html` in your browser to view the full interactive coverage report.
535
-
536
- ![](https://user-images.githubusercontent.com/16587779/131858993-87d1aafc-8ef1-4a82-867d-e64a13e36106.png)
537
-
538
- ![](https://user-images.githubusercontent.com/16587779/131859006-c6f17d18-c603-44a5-9d59-0670177276cf.png)
539
-
540
- ## Extending Helper
541
-
542
- To create custom `I.*` commands using Playwright API you need to create a custom helper.
543
-
544
- Start with creating an `MyPlaywright` helper using `generate:helper` or `gh` command:
545
-
546
- ```sh
547
- npx codeceptjs gh
548
- ```
549
-
550
- Then inside a Helper you can access `Playwright` helper of CodeceptJS.
551
- Let's say you want to create `I.grabDimensionsOfCurrentPage` action. In this case you need to call `evaluate` method of `page` object
552
-
553
- ```js
554
- // inside a MyPlaywright helper
555
- async grabDimensionsOfCurrentPage() {
556
- const { page } = this.helpers.Playwright;
557
- await page.goto('https://www.example.com/');
558
- return page.evaluate(() => {
559
- return {
560
- width: document.documentElement.clientWidth,
561
- height: document.documentElement.clientHeight,
562
- deviceScaleFactor: window.devicePixelRatio
563
- }
564
- })
565
- }
566
- ```
567
-
568
- The same way you can also access `browser` object to implement more actions or handle events. For instance, you want to set the permissions, you can approach it with:
569
-
570
- ```js
571
- // inside a MyPlaywright helper
572
- async setPermissions() {
573
- const { browser } = this.helpers.Playwright;
574
- const context = browser.defaultContext()
575
- return context.setPermissions('https://html5demos.com', ['geolocation']);
576
- }
577
- ```
578
-
579
- > [▶ Learn more about BrowserContext](https://github.com/microsoft/playwright/blob/master/docs/src/api/class-browsercontext.md)
580
-
581
- > [▶ Learn more about Helpers](https://codecept.io/helpers/)
582
-
583
- ## Timezone change
584
-
585
- Sometimes it's useful to test browser in different timezones. You can change timezone this way:
586
-
587
- ```js
588
- Scenario("Test in a different timezone", ({ I }) => {
589
- I.restartBrowser({ timezoneId: 'America/Phoenix' });
590
- I.amOnPage("/");
591
- // ...
592
- // Reset timezone to default one (taken from OS)
593
- I.restartBrowser();
594
- });
595
- ```
596
- Other context options: https://playwright.dev/docs/api/class-browser#browser-new-context
597
-
598
- ## Configuring CI
599
-
600
- ### GitHub Actions
601
-
602
- Playwright can be added to GitHub Actions using [official action](https://github.com/microsoft/playwright-github-action). Use it before starting CodeceptJS tests to install all dependencies. It is important to run tests in headless mode ([otherwise you will need to enable xvfb to emulate desktop](https://github.com/microsoft/playwright-github-action#run-in-headful-mode)).
603
-
604
- ```yml
605
- # from workflows/tests.yml
606
- - uses: microsoft/playwright-github-action@v1
607
- - name: run CodeceptJS tests
608
- run: npx codeceptjs run
609
- ```