codeceptjs 3.6.0-beta.1.ai-healers → 3.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -2
- package/bin/codecept.js +2 -1
- package/docs/webapi/dontSeeTraffic.mustache +13 -0
- package/docs/webapi/flushNetworkTraffics.mustache +5 -0
- package/docs/webapi/grabRecordedNetworkTraffics.mustache +10 -0
- package/docs/webapi/seeTraffic.mustache +36 -0
- package/docs/webapi/startRecordingTraffic.mustache +8 -0
- package/docs/webapi/startRecordingWebSocketMessages.mustache +8 -0
- package/docs/webapi/stopRecordingTraffic.mustache +5 -0
- package/docs/webapi/stopRecordingWebSocketMessages.mustache +7 -0
- package/docs/webapi/waitForCookie.mustache +9 -0
- package/lib/actor.js +6 -3
- package/lib/command/dryRun.js +44 -13
- package/lib/helper/Appium.js +36 -12
- package/lib/helper/Expect.js +11 -8
- package/lib/helper/JSONResponse.js +8 -8
- package/lib/helper/MockServer.js +221 -0
- package/lib/helper/Playwright.js +107 -371
- package/lib/helper/Puppeteer.js +404 -71
- package/lib/helper/REST.js +4 -1
- package/lib/helper/WebDriver.js +189 -13
- package/lib/helper/errors/ElementAssertion.js +38 -0
- package/lib/helper/extras/PlaywrightReactVueLocator.js +6 -1
- package/lib/helper/network/actions.js +123 -0
- package/lib/helper/network/utils.js +187 -0
- package/lib/locator.js +36 -5
- package/lib/pause.js +4 -9
- package/lib/plugin/coverage.js +112 -99
- package/lib/step.js +3 -1
- package/package.json +49 -38
- package/typings/index.d.ts +19 -2
- package/typings/promiseBasedTypes.d.ts +505 -41
- package/typings/types.d.ts +531 -43
- package/docs/advanced.md +0 -351
- package/docs/ai.md +0 -365
- package/docs/api.md +0 -323
- package/docs/basics.md +0 -979
- package/docs/bdd.md +0 -539
- package/docs/best.md +0 -237
- package/docs/books.md +0 -37
- package/docs/bootstrap.md +0 -135
- package/docs/build/AI.js +0 -124
- package/docs/build/ApiDataFactory.js +0 -410
- package/docs/build/Appium.js +0 -2027
- package/docs/build/Expect.js +0 -422
- package/docs/build/FileSystem.js +0 -228
- package/docs/build/GraphQL.js +0 -229
- package/docs/build/GraphQLDataFactory.js +0 -309
- package/docs/build/JSONResponse.js +0 -338
- package/docs/build/Mochawesome.js +0 -71
- package/docs/build/Nightmare.js +0 -2152
- package/docs/build/OpenAI.js +0 -126
- package/docs/build/Playwright.js +0 -5110
- package/docs/build/Protractor.js +0 -2706
- package/docs/build/Puppeteer.js +0 -3905
- package/docs/build/REST.js +0 -344
- package/docs/build/TestCafe.js +0 -2125
- package/docs/build/WebDriver.js +0 -4240
- package/docs/changelog.md +0 -2572
- package/docs/commands.md +0 -266
- package/docs/community-helpers.md +0 -58
- package/docs/configuration.md +0 -157
- package/docs/continuous-integration.md +0 -22
- package/docs/custom-helpers.md +0 -306
- package/docs/data.md +0 -379
- package/docs/detox.md +0 -235
- package/docs/docker.md +0 -136
- package/docs/email.md +0 -183
- package/docs/examples.md +0 -149
- package/docs/heal.md +0 -186
- package/docs/helpers/ApiDataFactory.md +0 -266
- package/docs/helpers/Appium.md +0 -1374
- package/docs/helpers/Detox.md +0 -586
- package/docs/helpers/Expect.md +0 -275
- package/docs/helpers/FileSystem.md +0 -152
- package/docs/helpers/GraphQL.md +0 -151
- package/docs/helpers/GraphQLDataFactory.md +0 -226
- package/docs/helpers/JSONResponse.md +0 -254
- package/docs/helpers/Mochawesome.md +0 -8
- package/docs/helpers/MockRequest.md +0 -377
- package/docs/helpers/Nightmare.md +0 -1305
- package/docs/helpers/OpenAI.md +0 -70
- package/docs/helpers/Playwright.md +0 -2759
- package/docs/helpers/Polly.md +0 -44
- package/docs/helpers/Protractor.md +0 -1769
- package/docs/helpers/Puppeteer-firefox.md +0 -86
- package/docs/helpers/Puppeteer.md +0 -2317
- package/docs/helpers/REST.md +0 -218
- package/docs/helpers/TestCafe.md +0 -1321
- package/docs/helpers/WebDriver.md +0 -2547
- package/docs/hooks.md +0 -340
- package/docs/index.md +0 -111
- package/docs/installation.md +0 -75
- package/docs/internal-api.md +0 -266
- package/docs/locators.md +0 -339
- package/docs/mobile-react-native-locators.md +0 -67
- package/docs/mobile.md +0 -338
- package/docs/pageobjects.md +0 -291
- package/docs/parallel.md +0 -400
- package/docs/playwright.md +0 -632
- package/docs/plugins.md +0 -1247
- package/docs/puppeteer.md +0 -316
- package/docs/quickstart.md +0 -162
- package/docs/react.md +0 -70
- package/docs/reports.md +0 -392
- package/docs/secrets.md +0 -36
- package/docs/shadow.md +0 -68
- package/docs/shared/keys.mustache +0 -31
- package/docs/shared/react.mustache +0 -1
- package/docs/testcafe.md +0 -174
- package/docs/translation.md +0 -247
- package/docs/tutorial.md +0 -271
- package/docs/typescript.md +0 -180
- package/docs/ui.md +0 -59
- package/docs/videos.md +0 -28
- package/docs/visual.md +0 -202
- package/docs/vue.md +0 -143
- package/docs/webdriver.md +0 -701
- package/docs/wiki/Books-&-Posts.md +0 -27
- package/docs/wiki/Community-Helpers-&-Plugins.md +0 -53
- package/docs/wiki/Converting-Playwright-to-Istanbul-Coverage.md +0 -61
- package/docs/wiki/Examples.md +0 -145
- package/docs/wiki/Google-Summer-of-Code-(GSoC)-2020.md +0 -68
- package/docs/wiki/Home.md +0 -16
- package/docs/wiki/Migration-to-Appium-v2---CodeceptJS.md +0 -83
- package/docs/wiki/Release-Process.md +0 -24
- package/docs/wiki/Roadmap.md +0 -23
- package/docs/wiki/Tests.md +0 -1393
- package/docs/wiki/Upgrading-to-CodeceptJS-3.md +0 -153
- 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
|
-
```
|