codeceptjs 3.5.12-beta.5 → 3.5.12-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 (217) hide show
  1. package/lib/helper/MockServer.js +215 -0
  2. package/lib/helper/Playwright.js +43 -22
  3. package/lib/helper/Puppeteer.js +25 -6
  4. package/lib/helper/WebDriver.js +25 -4
  5. package/lib/helper/errors/ElementAssertion.js +38 -0
  6. package/package.json +6 -5
  7. package/typings/index.d.ts +16 -0
  8. package/typings/promiseBasedTypes.d.ts +182 -0
  9. package/typings/types.d.ts +182 -0
  10. package/docs/advanced.md +0 -351
  11. package/docs/ai.md +0 -248
  12. package/docs/api.md +0 -323
  13. package/docs/basics.md +0 -979
  14. package/docs/bdd.md +0 -539
  15. package/docs/best.md +0 -237
  16. package/docs/books.md +0 -37
  17. package/docs/bootstrap.md +0 -135
  18. package/docs/build/ApiDataFactory.js +0 -410
  19. package/docs/build/Appium.js +0 -2027
  20. package/docs/build/Expect.js +0 -422
  21. package/docs/build/FileSystem.js +0 -228
  22. package/docs/build/GraphQL.js +0 -229
  23. package/docs/build/GraphQLDataFactory.js +0 -309
  24. package/docs/build/JSONResponse.js +0 -338
  25. package/docs/build/Mochawesome.js +0 -71
  26. package/docs/build/Nightmare.js +0 -2152
  27. package/docs/build/OpenAI.js +0 -126
  28. package/docs/build/Playwright.js +0 -5110
  29. package/docs/build/Protractor.js +0 -2706
  30. package/docs/build/Puppeteer.js +0 -3905
  31. package/docs/build/REST.js +0 -344
  32. package/docs/build/TestCafe.js +0 -2125
  33. package/docs/build/WebDriver.js +0 -4240
  34. package/docs/changelog.md +0 -2572
  35. package/docs/commands.md +0 -266
  36. package/docs/community-helpers.md +0 -58
  37. package/docs/configuration.md +0 -157
  38. package/docs/continuous-integration.md +0 -22
  39. package/docs/custom-helpers.md +0 -306
  40. package/docs/data.md +0 -379
  41. package/docs/detox.md +0 -235
  42. package/docs/docker.md +0 -136
  43. package/docs/email.md +0 -183
  44. package/docs/examples.md +0 -149
  45. package/docs/helpers/ApiDataFactory.md +0 -266
  46. package/docs/helpers/Appium.md +0 -1374
  47. package/docs/helpers/Detox.md +0 -586
  48. package/docs/helpers/Expect.md +0 -275
  49. package/docs/helpers/FileSystem.md +0 -152
  50. package/docs/helpers/GraphQL.md +0 -151
  51. package/docs/helpers/GraphQLDataFactory.md +0 -226
  52. package/docs/helpers/JSONResponse.md +0 -254
  53. package/docs/helpers/Mochawesome.md +0 -8
  54. package/docs/helpers/MockRequest.md +0 -377
  55. package/docs/helpers/Nightmare.md +0 -1305
  56. package/docs/helpers/OpenAI.md +0 -70
  57. package/docs/helpers/Playwright.md +0 -2759
  58. package/docs/helpers/Polly.md +0 -44
  59. package/docs/helpers/Protractor.md +0 -1769
  60. package/docs/helpers/Puppeteer-firefox.md +0 -86
  61. package/docs/helpers/Puppeteer.md +0 -2317
  62. package/docs/helpers/REST.md +0 -218
  63. package/docs/helpers/TestCafe.md +0 -1321
  64. package/docs/helpers/WebDriver.md +0 -2547
  65. package/docs/hooks.md +0 -340
  66. package/docs/index.md +0 -111
  67. package/docs/installation.md +0 -75
  68. package/docs/internal-api.md +0 -266
  69. package/docs/locators.md +0 -339
  70. package/docs/mobile-react-native-locators.md +0 -67
  71. package/docs/mobile.md +0 -338
  72. package/docs/pageobjects.md +0 -291
  73. package/docs/parallel.md +0 -400
  74. package/docs/playwright.md +0 -632
  75. package/docs/plugins.md +0 -1259
  76. package/docs/puppeteer.md +0 -316
  77. package/docs/quickstart.md +0 -162
  78. package/docs/react.md +0 -70
  79. package/docs/reports.md +0 -392
  80. package/docs/secrets.md +0 -36
  81. package/docs/shadow.md +0 -68
  82. package/docs/shared/keys.mustache +0 -31
  83. package/docs/shared/react.mustache +0 -1
  84. package/docs/testcafe.md +0 -174
  85. package/docs/translation.md +0 -247
  86. package/docs/tutorial.md +0 -271
  87. package/docs/typescript.md +0 -180
  88. package/docs/ui.md +0 -59
  89. package/docs/videos.md +0 -28
  90. package/docs/visual.md +0 -202
  91. package/docs/vue.md +0 -143
  92. package/docs/webapi/amOnPage.mustache +0 -11
  93. package/docs/webapi/appendField.mustache +0 -11
  94. package/docs/webapi/attachFile.mustache +0 -12
  95. package/docs/webapi/blur.mustache +0 -18
  96. package/docs/webapi/checkOption.mustache +0 -13
  97. package/docs/webapi/clearCookie.mustache +0 -9
  98. package/docs/webapi/clearField.mustache +0 -9
  99. package/docs/webapi/click.mustache +0 -25
  100. package/docs/webapi/clickLink.mustache +0 -8
  101. package/docs/webapi/closeCurrentTab.mustache +0 -7
  102. package/docs/webapi/closeOtherTabs.mustache +0 -8
  103. package/docs/webapi/dontSee.mustache +0 -11
  104. package/docs/webapi/dontSeeCheckboxIsChecked.mustache +0 -10
  105. package/docs/webapi/dontSeeCookie.mustache +0 -8
  106. package/docs/webapi/dontSeeCurrentUrlEquals.mustache +0 -10
  107. package/docs/webapi/dontSeeElement.mustache +0 -8
  108. package/docs/webapi/dontSeeElementInDOM.mustache +0 -8
  109. package/docs/webapi/dontSeeInCurrentUrl.mustache +0 -4
  110. package/docs/webapi/dontSeeInField.mustache +0 -11
  111. package/docs/webapi/dontSeeInSource.mustache +0 -8
  112. package/docs/webapi/dontSeeInTitle.mustache +0 -8
  113. package/docs/webapi/doubleClick.mustache +0 -13
  114. package/docs/webapi/downloadFile.mustache +0 -12
  115. package/docs/webapi/dragAndDrop.mustache +0 -9
  116. package/docs/webapi/dragSlider.mustache +0 -11
  117. package/docs/webapi/executeAsyncScript.mustache +0 -24
  118. package/docs/webapi/executeScript.mustache +0 -26
  119. package/docs/webapi/fillField.mustache +0 -16
  120. package/docs/webapi/focus.mustache +0 -13
  121. package/docs/webapi/forceClick.mustache +0 -28
  122. package/docs/webapi/forceRightClick.mustache +0 -18
  123. package/docs/webapi/grabAllWindowHandles.mustache +0 -7
  124. package/docs/webapi/grabAttributeFrom.mustache +0 -10
  125. package/docs/webapi/grabAttributeFromAll.mustache +0 -9
  126. package/docs/webapi/grabBrowserLogs.mustache +0 -9
  127. package/docs/webapi/grabCookie.mustache +0 -11
  128. package/docs/webapi/grabCssPropertyFrom.mustache +0 -11
  129. package/docs/webapi/grabCssPropertyFromAll.mustache +0 -10
  130. package/docs/webapi/grabCurrentUrl.mustache +0 -9
  131. package/docs/webapi/grabCurrentWindowHandle.mustache +0 -6
  132. package/docs/webapi/grabDataFromPerformanceTiming.mustache +0 -20
  133. package/docs/webapi/grabElementBoundingRect.mustache +0 -20
  134. package/docs/webapi/grabGeoLocation.mustache +0 -8
  135. package/docs/webapi/grabHTMLFrom.mustache +0 -10
  136. package/docs/webapi/grabHTMLFromAll.mustache +0 -9
  137. package/docs/webapi/grabNumberOfOpenTabs.mustache +0 -8
  138. package/docs/webapi/grabNumberOfVisibleElements.mustache +0 -9
  139. package/docs/webapi/grabPageScrollPosition.mustache +0 -8
  140. package/docs/webapi/grabPopupText.mustache +0 -5
  141. package/docs/webapi/grabSource.mustache +0 -8
  142. package/docs/webapi/grabTextFrom.mustache +0 -10
  143. package/docs/webapi/grabTextFromAll.mustache +0 -9
  144. package/docs/webapi/grabTitle.mustache +0 -8
  145. package/docs/webapi/grabValueFrom.mustache +0 -9
  146. package/docs/webapi/grabValueFromAll.mustache +0 -8
  147. package/docs/webapi/grabWebElement.mustache +0 -9
  148. package/docs/webapi/grabWebElements.mustache +0 -9
  149. package/docs/webapi/moveCursorTo.mustache +0 -12
  150. package/docs/webapi/openNewTab.mustache +0 -7
  151. package/docs/webapi/pressKey.mustache +0 -12
  152. package/docs/webapi/pressKeyDown.mustache +0 -12
  153. package/docs/webapi/pressKeyUp.mustache +0 -12
  154. package/docs/webapi/pressKeyWithKeyNormalization.mustache +0 -60
  155. package/docs/webapi/refreshPage.mustache +0 -6
  156. package/docs/webapi/resizeWindow.mustache +0 -6
  157. package/docs/webapi/rightClick.mustache +0 -14
  158. package/docs/webapi/saveElementScreenshot.mustache +0 -10
  159. package/docs/webapi/saveScreenshot.mustache +0 -12
  160. package/docs/webapi/say.mustache +0 -10
  161. package/docs/webapi/scrollIntoView.mustache +0 -11
  162. package/docs/webapi/scrollPageToBottom.mustache +0 -6
  163. package/docs/webapi/scrollPageToTop.mustache +0 -6
  164. package/docs/webapi/scrollTo.mustache +0 -12
  165. package/docs/webapi/see.mustache +0 -11
  166. package/docs/webapi/seeAttributesOnElements.mustache +0 -9
  167. package/docs/webapi/seeCheckboxIsChecked.mustache +0 -10
  168. package/docs/webapi/seeCookie.mustache +0 -8
  169. package/docs/webapi/seeCssPropertiesOnElements.mustache +0 -9
  170. package/docs/webapi/seeCurrentUrlEquals.mustache +0 -11
  171. package/docs/webapi/seeElement.mustache +0 -8
  172. package/docs/webapi/seeElementInDOM.mustache +0 -8
  173. package/docs/webapi/seeInCurrentUrl.mustache +0 -8
  174. package/docs/webapi/seeInField.mustache +0 -12
  175. package/docs/webapi/seeInPopup.mustache +0 -8
  176. package/docs/webapi/seeInSource.mustache +0 -7
  177. package/docs/webapi/seeInTitle.mustache +0 -8
  178. package/docs/webapi/seeNumberOfElements.mustache +0 -11
  179. package/docs/webapi/seeNumberOfVisibleElements.mustache +0 -10
  180. package/docs/webapi/seeTextEquals.mustache +0 -9
  181. package/docs/webapi/seeTitleEquals.mustache +0 -8
  182. package/docs/webapi/selectOption.mustache +0 -21
  183. package/docs/webapi/setCookie.mustache +0 -16
  184. package/docs/webapi/setGeoLocation.mustache +0 -12
  185. package/docs/webapi/switchTo.mustache +0 -9
  186. package/docs/webapi/switchToNextTab.mustache +0 -10
  187. package/docs/webapi/switchToPreviousTab.mustache +0 -10
  188. package/docs/webapi/type.mustache +0 -21
  189. package/docs/webapi/uncheckOption.mustache +0 -13
  190. package/docs/webapi/wait.mustache +0 -8
  191. package/docs/webapi/waitForClickable.mustache +0 -11
  192. package/docs/webapi/waitForDetached.mustache +0 -10
  193. package/docs/webapi/waitForElement.mustache +0 -11
  194. package/docs/webapi/waitForEnabled.mustache +0 -6
  195. package/docs/webapi/waitForFunction.mustache +0 -17
  196. package/docs/webapi/waitForInvisible.mustache +0 -10
  197. package/docs/webapi/waitForNumberOfTabs.mustache +0 -9
  198. package/docs/webapi/waitForText.mustache +0 -13
  199. package/docs/webapi/waitForValue.mustache +0 -10
  200. package/docs/webapi/waitForVisible.mustache +0 -10
  201. package/docs/webapi/waitInUrl.mustache +0 -9
  202. package/docs/webapi/waitNumberOfVisibleElements.mustache +0 -10
  203. package/docs/webapi/waitToHide.mustache +0 -10
  204. package/docs/webapi/waitUrlEquals.mustache +0 -10
  205. package/docs/webdriver.md +0 -701
  206. package/docs/wiki/Books-&-Posts.md +0 -27
  207. package/docs/wiki/Community-Helpers-&-Plugins.md +0 -53
  208. package/docs/wiki/Converting-Playwright-to-Istanbul-Coverage.md +0 -61
  209. package/docs/wiki/Examples.md +0 -145
  210. package/docs/wiki/Google-Summer-of-Code-(GSoC)-2020.md +0 -68
  211. package/docs/wiki/Home.md +0 -16
  212. package/docs/wiki/Migration-to-Appium-v2---CodeceptJS.md +0 -83
  213. package/docs/wiki/Release-Process.md +0 -24
  214. package/docs/wiki/Roadmap.md +0 -23
  215. package/docs/wiki/Tests.md +0 -1393
  216. package/docs/wiki/Upgrading-to-CodeceptJS-3.md +0 -153
  217. package/docs/wiki/Videos.md +0 -19
package/docs/bdd.md DELETED
@@ -1,539 +0,0 @@
1
- ---
2
- permalink: /bdd
3
- title: Behavior Driven Development
4
- ---
5
-
6
- # Behavior Driven Development
7
-
8
- Behavior Driven Development (BDD) is a popular software development methodology. BDD is considered an extension of TDD, and is greatly inspired by [Agile](https://agilemanifesto.org/) practices. The primary reason to choose BDD as your development process is to break down communication barriers between business and technical teams. BDD encourages the use of automated testing to verify all documented features of a project from the very beginning. This is why it is common to talk about BDD in the context of test frameworks (like CodeceptJS). The BDD approach, however, is about much more than testing - it is a common language for all team members to use during the development process.
9
-
10
- ## What is Behavior Driven Development
11
-
12
- BDD was introduced by [Dan North](https://dannorth.net/introducing-bdd/). He described it as:
13
-
14
- > outside-in, pull-based, multiple-stakeholder, multiple-scale, high-automation, agile methodology. It describes a cycle of interactions with well-defined outputs, resulting in the delivery of working, tested software that matters.
15
-
16
- BDD has its own evolution from the days it was born, started by replacing "test" to "should" in unit tests, and moving towards powerful tools like Cucumber and Behat, which made user stories (human-readable text) to be executed as an acceptance test.
17
-
18
- The idea of story BDD can be narrowed to:
19
-
20
- * describe features in a scenario with a formal text
21
- * use examples to make abstract things concrete
22
- * implement each step of a scenario for testing
23
- * write actual code implementing the feature
24
-
25
- By writing every feature in User Story format that is automatically executable as a test we ensure that: business, developers, QAs and managers are in the same boat.
26
-
27
- BDD encourages exploration and debate in order to formalize the requirements and the features that needs to be implemented by requesting to write the User Stories in a way that everyone can understand.
28
-
29
- By making tests to be a part of User Story, BDD allows non-technical personnel to write (or edit) Acceptance tests.
30
-
31
- With this procedure we also ensure that everyone in a team knows what has been developed, what has not, what has been tested and what has not.
32
-
33
- ### Ubiquitous Language
34
-
35
- The ubiquitous language is always referred as *common* language. That it is the main benefit. It is not a couple of our business specification's words, and not a couple of developer's technical terms. It is a common words and terms that can be understood by people for whom we are building the software and should be understood by developers. Establishing correct communication between this two groups people is vital for building successful project that will fit the domain and fulfill all business needs.
36
-
37
- Each feature of a product should be born from a talk between
38
-
39
- * business (analysts, product owner)
40
- * developers
41
- * QAs
42
-
43
- which are known in BDD as "three amigos".
44
-
45
- Such talks should produce written stories. There should be an actor that doing some things, the feature that should be fulfilled within the story and the result achieved.
46
-
47
- We can try to write such simple story:
48
-
49
- ```
50
- As a customer I want to buy several products
51
- I put first product with $600 price to my cart
52
- And then another one with $1000 price
53
- When I go to checkout process
54
- I should see that total number of products I want to buy is 2
55
- And my order amount is $1600
56
- ```
57
-
58
- As we can see this simple story highlights core concepts that are called *contracts*. We should fulfill those contracts to model software correctly. But how we can verify that those contracts are being satisfied? [Cucumber](https://cucumber.io) introduced a special language for such stories called **Gherkin**. Same story transformed to Gherkin will look like this:
59
-
60
- ```gherkin
61
- Feature: checkout process
62
- In order to buy products
63
- As a customer
64
- I want to be able to buy several products
65
-
66
- Scenario:
67
- Given I have product with $600 price in my cart
68
- And I have product with $1000 price
69
- When I go to checkout process
70
- Then I should see that total number of products is 2
71
- And my order amount is $1600
72
- ```
73
-
74
- **CodeceptJS can execute this scenario step by step as an automated test**.
75
- Every step in this scenario requires a code which defines it.
76
-
77
- ## Gherkin
78
-
79
- Let's learn some more about Gherkin format and then we will see how to execute it with CodeceptJS. We can enable Gherkin for current project by running `gherkin:init` command on **already initialized project**:
80
-
81
- ```
82
- npx codeceptjs gherkin:init
83
- ```
84
-
85
- It will add `gherkin` section to the current config. It will also prepare directories for features and step definition. And it will create the first feature file for you.
86
-
87
- ### Features
88
-
89
- Whenever you start writing a story you are describing a specific feature of an application, with a set of scenarios and examples describing this feature. Let's open a feature file created by `gherkin:init` command, which is `feature/basic.feature`.
90
-
91
- ```gherkin
92
- Feature: Business rules
93
- In order to achieve my goals
94
- As a persona
95
- I want to be able to interact with a system
96
-
97
- Scenario: do something
98
- Given I have a defined step
99
- ```
100
-
101
- This text should be rewritten to follow your buisness rules. Don't think about a web interface for a while.
102
- Think about how user interacts with your system and what goals they want to achieve. Then write interaction scenarios.
103
-
104
- #### Scenarios
105
-
106
- Scenarios are live examples of feature usage. Inside a feature file it should be written inside a *Feature* block. Each scenario should contain its title:
107
-
108
- ```gherkin
109
- Feature: checkout
110
- In order to buy product
111
- As a customer
112
- I need to be able to checkout the selected products
113
-
114
- Scenario: order several products
115
- ```
116
-
117
- Scenarios are written in step-by-step manner using Given-When-Then approach. At start, scenario should describe its context with **Given** keyword:
118
-
119
- ```gherkin
120
- Given I have product with $600 price in my cart
121
- And I have product with $1000 price in my cart
122
- ```
123
-
124
- Here we also use word **And** to extend the Given and not to repeat it in each line.
125
-
126
- This is how we described the initial conditions. Next, we perform some action. We use **When** keyword for it:
127
-
128
- ```gherkin
129
- When I go to checkout process
130
- ```
131
-
132
- And in the end we are verifying our expectation using **Then** keyword. The action changed the initial given state, and produced some results. Let's check that those results are what we actually expect.
133
-
134
- ```gherkin
135
- Then I should see that total number of products is 2
136
- And my order amount is $1600
137
- ```
138
-
139
- This scenarios are nice as live documentation but they do not test anything yet. What we need next is to define how to run those steps.
140
- Steps can be defined by executing `gherkin:snippets` command:
141
-
142
- ```bash
143
- npx codeceptjs gherkin:snippets [--path=PATH] [--feature=PATH]
144
- ```
145
-
146
- This will produce code templates for all undefined steps in the .feature files.
147
- By default, it will scan all of the `.feature` files specified in the `gherkin.features` section of the config and produce code templates for all undefined steps. If the `--feature` option is specified, it will scan the specified .feature file(s).
148
- The stub definitions by default will be placed into the first file specified in the `gherkin.steps` section of the config. However, you may also use `--path` to specify a specific file in which to place all undefined steps. This file must exist and be in the `gherkin.steps array of the config.
149
- Our next step will be to define those steps and transforming feature-file into a valid test.
150
-
151
- ### Step Definitions
152
-
153
- Step definitions are placed in JavaScript file with Given/When/Then functions that map strings from feature file to functions:
154
-
155
- ```js
156
- // use I and productPage via inject() function
157
- const { I, productPage } = inject();
158
-
159
- // you can provide RegEx to match corresponding steps
160
- Given(/I have product with \$(\d+) price/, (price) => {
161
- I.amOnPage('/products');
162
- productPage.create({ price });
163
- I.click('Add to cart');
164
- });
165
-
166
- // or a simple string
167
- When('I go to checkout process', () => {
168
- I.click('Checkout');
169
- });
170
-
171
- // parameters are passed in via Cucumber expressions
172
- Then('I should see that total number of products is {int}', (num) => {
173
- I.see(num, '.cart');
174
- });
175
- Then('my order amount is ${int}', (sum) => { // eslint-disable-line
176
- I.see('Total: ' + sum);
177
- });
178
- ```
179
-
180
- Steps can be either strings or regular expressions. Parameters from string are passed as function arguments. To define parameters in a string we use [Cucumber expressions](https://github.com/cucumber/cucumber-expressions#readme)
181
-
182
- To list all defined steps run `gherkin:steps` command:
183
-
184
- ```bash
185
- npx codeceptjs gherkin:steps
186
- ```
187
-
188
- Use `grep` to find steps in a list (grep works on Linux & MacOS):
189
-
190
- ```
191
- npx codeceptjs gherkin:steps | grep user
192
- ```
193
-
194
- To run tests and see step-by step output use `--steps` optoin:
195
-
196
- ```
197
- npx codeceptjs run --steps
198
- ```
199
-
200
- To see not only business steps but an actual performed steps use `--debug` flag:
201
-
202
- ```
203
- npx codeceptjs run --debug
204
- ```
205
-
206
- ## Advanced Gherkin
207
-
208
- Let's improve our BDD suite by using the advanced features of Gherkin language.
209
-
210
- ### Background
211
-
212
- If a group of scenarios have the same initial steps, let's that for dashboard we need always need to be logged in as administrator. We can use *Background* section to do the required preparations and not to repeat same steps across scenarios.
213
-
214
- ```gherkin
215
- Feature: Dashboard
216
- In order to view current state of business
217
- As an owner
218
- I need to be able to see reports on dashboard
219
-
220
- Background:
221
- Given I am logged in as administrator
222
- And I open dashboard page
223
- ```
224
-
225
- Steps in background are defined the same way as in scenarios.
226
-
227
- ### Tables
228
-
229
- Scenarios can become more descriptive when you represent repeating data as tables. Instead of writing several steps "I have product with :num1 $ price in my cart" we can have one step with multiple values in it.
230
-
231
- ```gherkin
232
- Given I have products in my cart
233
- | name | category | price |
234
- | Harry Potter | Books | 5 |
235
- | iPhone 5 | Smartphones | 1200 |
236
- | Nuclear Bomb | Weapons | 100000 |
237
- ```
238
-
239
- Tables are the recommended way to pass arrays into test scenarios.
240
- Inside a step definition data is stored in argument passed as `DataTable` JavaScript object.
241
- You can iterate on it like this:
242
-
243
- ```js
244
- Given('I have products in my cart', (table) => { // eslint-disable-line
245
- for (const id in table.rows) {
246
- if (id < 1) {
247
- continue; // skip a header of a table
248
- }
249
-
250
- // go by row cells
251
- const cells = table.rows[id].cells;
252
-
253
- // take values
254
- const name = cells[0].value;
255
- const category = cells[1].value;
256
- const price = cells[2].value;
257
- // ...
258
- }
259
- });
260
- ```
261
-
262
- You can also use the `parse()` method to obtain an object that allow you to get a simple version of the table parsed by column or row, with header (or not):
263
-
264
- - `raw()` - returns the table as a 2-D array
265
- - `rows()` - returns the table as a 2-D array, without the first row
266
- - `hashes()` - returns an array of objects where each row is converted to an object (column header is the key)
267
- - `rowsHash()` - returns an object where each row corresponds to an entry(first column is the key, second column is the value)
268
- - `transpose()` - transpose the data, returns nothing. To work with the transposed table use the methods above.
269
-
270
- If we use hashes() with the previous example :
271
-
272
- ```js
273
- Given('I have products in my cart', (table) => { // eslint-disable-line
274
- //parse the table by header
275
- const tableByHeader = table.parse().hashes();
276
- for (const row of tableByHeader) {
277
-
278
- // take values
279
- const name = row.name;
280
- const category = row.category;
281
- const price = row.price;
282
- // ...
283
- }
284
- });
285
- ```
286
- Examples of tables using:
287
-
288
- ```gherkin
289
- Given I have a short employees card
290
- | Harry | Potter |
291
- | Chuck | Norris |
292
- ```
293
- ```js
294
- const { DataTableArgument } = require('codeceptjs');
295
- //...
296
- Given('I have a short employees card', (table) => {
297
- const dataTableArgument = new DataTableArgument(table);
298
- const raw = dataTableArgument.raw();
299
- // row = [['Harry', 'Potter'], ['Chuck', 'Norris']]
300
- dataTableArgument.transpose();
301
- const transposedRaw = dataTableArgument.raw();
302
- // transposedRaw = [['Harry', 'Chuck'], ['Potter', 'Norris']];
303
- }
304
- );
305
- ```
306
- ```gherkin
307
- Given I have an employee card
308
- | name | surname | position |
309
- | Harry | Potter | Seeker |
310
- ```
311
- ```js
312
- const { DataTableArgument } = require('codeceptjs');
313
- //...
314
- Given('I have an employee card', (table) => {
315
- const dataTableArgument = new DataTableArgument(table);
316
- const hashes = dataTableArgument.hashes();
317
- // hashes = [{ name: 'Harry', surname: 'Potter', position: 'Seeker' }];
318
- const rows = dataTableArgument.rows();
319
- // rows = [['Harry', 'Potter', Seeker]];
320
- }
321
- );
322
- ```
323
- ```gherkin
324
- Given I have a formatted employee card
325
- | name | Harry |
326
- | surname | Potter |
327
- | position | Seeker |
328
- ```
329
- ```js
330
- const { DataTableArgument } = require('codeceptjs');
331
- //...
332
- Given('I have a formatted employee card', (table) => {
333
- const dataTableArgument = new DataTableArgument(table);
334
- const rawHash = dataTableArgument.rowsHash();
335
- // rawHash = { name: 'Harry', surname: 'Potter', position: 'Seeker' };
336
- }
337
- );
338
- ```
339
- ### Examples
340
-
341
- In case scenarios represent the same logic but differ on data, we can use *Scenario Outline* to provide different examples for the same behavior. Scenario outline is just like a basic scenario with some values replaced with placeholders, which are filled from a table. Each set of values is executed as a different test.
342
-
343
- ```gherkin
344
- Scenario Outline: order discount
345
- Given I have product with price <price>$ in my cart
346
- And discount for orders greater than $20 is 10 %
347
- When I go to checkout
348
- Then I should see overall price is "<total>" $
349
-
350
- Examples:
351
- | price | total |
352
- | 10 | 10 |
353
- | 20 | 20 |
354
- | 21 | 18.9 |
355
- | 30 | 27 |
356
- | 50 | 45 |
357
- ```
358
-
359
- It might be the case that the same column value needs to be utilized multiple times in the same step, that also can be possible with scenario outline.
360
-
361
- ```gherkin
362
- Scenario Outline: check parameter substitution
363
- Given I have a defined step
364
- When I see "<text>" text and "<text>" is not "xyz"
365
- Examples:
366
- | text |
367
- | Google |
368
-
369
- ```
370
-
371
- ### Long Strings
372
-
373
- Text values inside a scenarios can be set inside a `"""` block:
374
-
375
- ```gherkin
376
- Then i see in file "codecept.json"
377
- """
378
- {
379
- "output": "./output",
380
- "helpers": {
381
- "Puppeteer": {
382
- "url": "http://localhost",
383
- "restart": true,
384
- "windowSize": "1600x1200"
385
- }
386
- """
387
- ```
388
-
389
- This string can be accessed inside a `content` property of a last argument:
390
-
391
- ```js
392
- Then('Then i see in file {string}', (file, text) => {
393
- // file is a value of {string} from a title
394
- const fileContent = fs.readFileSync(file).toString();
395
- fileContent.should.include(text.content); // text.content is a value
396
- });
397
- ```
398
-
399
- ### Tags
400
-
401
- Gherkin scenarios and features can contain tags marked with `@`. Tags are appended to feature titles so you can easily filter by them when running tests:
402
-
403
- ```bash
404
- npx codeceptjs run --grep "@important"
405
- ```
406
-
407
- Tag should be placed before *Scenario:* or before *Feature:* keyword. In the last case all scenarios of that feature will be added to corresponding group.
408
-
409
- ### Custom types
410
-
411
- If you need parameter text in more advanced way, and you like using [Cucumber expressions](https://github.com/cucumber/cucumber-expressions#readme) better that regular expressions, use `DefineParameterType` function. You can extend Cucumber Expressions, so they automatically convert output parameters to your own types or transforms the match from the regexp.
412
-
413
- ```js
414
- DefineParameterType({
415
- name: 'popup_type',
416
- regexp: /critical|non-critical/,
417
- transformer: (match) => {
418
- return match === 'critical' ? '[class$="error"]'
419
- : '[class$="warning"]';
420
- },
421
- };);
422
-
423
- Given('I see {popup_type} popup', (popup) => {
424
- I.seeElement(popup);
425
- });
426
- ```
427
-
428
- ```gherkin
429
- Scenario: Display error message if user doesn't have permissions
430
- Given I on "Main" page without permissons
431
- Then I see error popup
432
- ```
433
-
434
- #### Parameters
435
-
436
- * `name` **[string]** The name the parameter type will be recognised by in output parameters.
437
- * `regexp` **([string] | [RegExp])** A regexp that will match the parameter. May include capture groups.
438
- * `transformer` **[function]** A function or method that transforms the match from the regexp.
439
- * `useForSnippets` **[boolean]** Defaults to `true`. That means this parameter type will be used to generate snippets for undefined steps.
440
- * `preferForRegexpMatch` **[boolean]** Defaults to `false`. Set to true if you have step definitions that use regular expressions, and you want this parameter type to take precedence over others during a match.
441
-
442
- ## Configuration
443
-
444
- * `gherkin`
445
- * `features` - path to feature files, or an array of feature file paths
446
- * `steps` - array of files with step definitions
447
- * `avoidDuplicateSteps` - attempts to avoid duplicate step definitions by shallow compare
448
-
449
- ```js
450
- ...
451
- "gherkin": {
452
- "features": "./features/*.feature",
453
- "steps": [
454
- "./step_definitions/steps.js"
455
- ]
456
- }
457
- ...
458
- ```
459
- ```js
460
- ...
461
- "gherkin": {
462
- "features": [
463
- "./features/*.feature",
464
- "./features/api_features/*.feature"
465
- ],
466
- "steps": [
467
- "./step_definitions/steps.js"
468
- ]
469
- }
470
- ...
471
- ```
472
-
473
- ## Before
474
-
475
- You can set up some before hooks inside step definition files. Use `Before` function to do that.
476
- This function receives current test as a parameter, so you can apply additional configuration to it.
477
-
478
- ```js
479
- // inside step_definitions
480
- Before((test) => {
481
- // perform your code
482
- test.retries(3); // retry test 3 times
483
- });
484
- ```
485
-
486
- This can be used to keep state between steps:
487
-
488
- ```js
489
- let state = {};
490
-
491
- // inside step_definitions
492
- Before(() => {
493
- state = {};
494
- });
495
-
496
- Given('have a user', async () => {
497
- state.user = await I.have('user');
498
- });
499
-
500
- When('I open account page', () => {
501
- I.amOnPage(`/user/${state.user.slug}`);
502
- })
503
- ```
504
-
505
- ## After
506
-
507
- Similarly to `Before` you can use `After` and `Fail` inside a scenario. `Fail` hook is activated on failure and receive two parameters: `test` and current `error`.
508
-
509
- ```js
510
- After(async () => {
511
- await someService.cleanup();
512
- });
513
-
514
- Fail((test, err) => {
515
- // test didn't
516
- console.log('Failed with', err);
517
- pause();
518
- });
519
- ```
520
-
521
- ## Tests vs Features
522
-
523
- It is common to think that BDD scenario is equal to test. But it's actually not. Not every test should be described as a feature. Not every test is written to test real business value. For instance, regression tests or negative scenario tests are not bringing any value to business. Business analysts don't care about scenario reproducing bug #13, or what error message is displayed when user tries to enter wrong password on login screen. Writing all the tests inside a feature files creates informational overflow.
524
-
525
- In CodeceptJS, you can combine tests written in Gherkin format with classical acceptance tests. This way you can keep your feature files compact with minimal set of scenarios, and write regular tests to cover all cases. Please note, feature files will be executed before tests.
526
-
527
- To run only features use `--features` option:
528
-
529
- ```
530
- npx codeceptjs run --features
531
- ```
532
-
533
- You can run a specific feature file by its filename or by grepping by name or tag.
534
-
535
- To run only tests without features use `--tests` option:
536
-
537
- ```
538
- npx codeceptjs run --tests
539
- ```