@@ -28,9 +28,9 @@ Most of web application have API, and it can be used to create and delete test r
28
28
By combining REST API with Factories you can easily create records for tests:
29
29
30
30
``` js
31
- I .have (' user' , { login: ' davert' , email: ' davert@mail.com' })
32
- let id = await I .have (' post' , { title: ' My first post' })
33
- I .haveMultiple (' comment' , 3 , { post_id: id })
31
+ I .have (' user' , { login: ' davert' , email: ' davert@mail.com' });
32
+ let id = await I .have (' post' , { title: ' My first post' });
33
+ I .haveMultiple (' comment' , 3 , {post_id: id});
34
34
```
35
35
36
36
To make this work you need
@@ -53,14 +53,14 @@ See the example for Posts factories:
53
53
``` js
54
54
// tests/factories/posts.js
55
55
56
- const { Factory } = require (' rosie' )
57
- const { faker } = require (' @faker-js/faker' )
56
+ const { Factory } = require (' rosie' );
57
+ const { faker } = require (' @faker-js/faker' );
58
58
59
59
module .exports = new Factory ()
60
- // no need to set id, it will be set by REST API
61
- .attr (' author' , () => faker .person .findName ())
62
- .attr (' title' , () => faker .lorem .sentence ())
63
- .attr (' body' , () => faker .lorem .paragraph ())
60
+ // no need to set id, it will be set by REST API
61
+ .attr (' author' , () => faker .person .findName ())
62
+ .attr (' title' , () => faker .lorem .sentence ())
63
+ .attr (' body' , () => faker .lorem .paragraph ());
64
64
```
65
65
66
66
For more options see [ rosie documentation] [ 1 ] .
@@ -71,12 +71,12 @@ Then configure ApiDataHelper to match factories and REST API:
71
71
72
72
ApiDataFactory has following config options:
73
73
74
- - ` endpoint ` : base URL for the API to send requests to.
75
- - ` cleanup ` (default: true): should inserted records be deleted up after tests
76
- - ` factories ` : list of defined factories
77
- - ` returnId ` (default: false): return id instead of a complete response when creating items.
78
- - ` headers ` : list of headers
79
- - ` REST ` : configuration for REST requests
74
+ * ` endpoint ` : base URL for the API to send requests to.
75
+ * ` cleanup ` (default: true): should inserted records be deleted up after tests
76
+ * ` factories ` : list of defined factories
77
+ * ` returnId ` (default: false): return id instead of a complete response when creating items.
78
+ * ` headers ` : list of headers
79
+ * ` REST ` : configuration for REST requests
80
80
81
81
See the example:
82
82
@@ -121,19 +121,19 @@ For instance, to set timeout you should add:
121
121
122
122
By default to create a record ApiDataFactory will use endpoint and plural factory name:
123
123
124
- - create: ` POST {endpoint}/{resource} data `
125
- - delete: ` DELETE {endpoint}/{resource}/id `
124
+ * create: ` POST {endpoint}/{resource} data `
125
+ * delete: ` DELETE {endpoint}/{resource}/id `
126
126
127
127
Example (` endpoint ` : ` http://app.com/api ` ):
128
128
129
- - create: POST request to ` http://app.com/api/users `
130
- - delete: DELETE request to ` http://app.com/api/users/1 `
129
+ * create: POST request to ` http://app.com/api/users `
130
+ * delete: DELETE request to ` http://app.com/api/users/1 `
131
131
132
132
This behavior can be configured with following options:
133
133
134
- - ` uri ` : set different resource uri. Example: ` uri: account ` => ` http://app.com/api/account ` .
135
- - ` create ` : override create options. Expected format: ` { method: uri } ` . Example: ` { "post": "/users/create" } `
136
- - ` delete ` : override delete options. Expected format: ` { method: uri } ` . Example: ` { "post": "/users/delete/{id}" } `
134
+ * ` uri ` : set different resource uri. Example: ` uri: account ` => ` http://app.com/api/account ` .
135
+ * ` create ` : override create options. Expected format: ` { method: uri } ` . Example: ` { "post": "/users/create" } `
136
+ * ` delete ` : override delete options. Expected format: ` { method: uri } ` . Example: ` { "post": "/users/delete/{id}" } `
137
137
138
138
Requests can also be overridden with a function which returns [ axois request config] [ 4 ] .
139
139
@@ -146,31 +146,31 @@ delete: (id) => ({ method: 'delete', url: '/posts', data: { id } })
146
146
Requests can be updated on the fly by using ` onRequest ` function. For instance, you can pass in current session from a cookie.
147
147
148
148
``` js
149
- onRequest: async request => {
150
- // using global codeceptjs instance
151
- let cookie = await codeceptjs .container .helpers (' WebDriver' ).grabCookie (' session' )
152
- request .headers = { Cookie: ` session=${ cookie .value } ` }
153
- }
149
+ onRequest: async ( request ) => {
150
+ // using global codeceptjs instance
151
+ let cookie = await codeceptjs .container .helpers (' WebDriver' ).grabCookie (' session' );
152
+ request .headers = { Cookie: ` session=${ cookie .value } ` };
153
+ }
154
154
```
155
155
156
156
### Responses
157
157
158
158
By default ` I.have() ` returns a promise with a created data:
159
159
160
160
``` js
161
- let client = await I .have (' client' )
161
+ let client = await I .have (' client' );
162
162
```
163
163
164
164
Ids of created records are collected and used in the end of a test for the cleanup.
165
165
If you need to receive ` id ` instead of full response enable ` returnId ` in a helper config:
166
166
167
167
``` js
168
168
// returnId: false
169
- let clientId = await I .have (' client' )
169
+ let clientId = await I .have (' client' );
170
170
// clientId == 1
171
171
172
172
// returnId: true
173
- let clientId = await I .have (' client' )
173
+ let clientId = await I .have (' client' );
174
174
// client == { name: 'John', email: 'john@snow.com' }
175
175
```
176
176
@@ -190,47 +190,47 @@ By default `id` property of response is taken. This behavior can be changed by s
190
190
191
191
### Parameters
192
192
193
- - ` config `   ;
193
+ * ` config `   ;
194
194
195
- ### \ _ requestCreate
195
+ ### _ requestCreate
196
196
197
197
Executes request to create a record in API.
198
198
Can be replaced from a in custom helper.
199
199
200
200
#### Parameters
201
201
202
- - ` factory ` ** any**   ;
203
- - ` data ` ** any**   ;
202
+ * ` factory ` ** any**   ;
203
+ * ` data ` ** any**   ;
204
204
205
- ### \ _ requestDelete
205
+ ### _ requestDelete
206
206
207
207
Executes request to delete a record in API
208
208
Can be replaced from a custom helper.
209
209
210
210
#### Parameters
211
211
212
- - ` factory ` ** any**   ;
213
- - ` id ` ** any**   ;
212
+ * ` factory ` ** any**   ;
213
+ * ` id ` ** any**   ;
214
214
215
215
### have
216
216
217
217
Generates a new record using factory and saves API request to store it.
218
218
219
219
``` js
220
220
// create a user
221
- I .have (' user' )
221
+ I .have (' user' );
222
222
// create user with defined email
223
223
// and receive it when inside async function
224
- const user = await I .have (' user' , { email: ' user@user.com' })
224
+ const user = await I .have (' user' , { email: ' user@user.com' });
225
225
// create a user with options that will not be included in the final request
226
- I .have (' user' , {}, { age: 33 , height: 55 })
226
+ I .have (' user' , { }, { age: 33 , height: 55 })
227
227
```
228
228
229
229
#### Parameters
230
230
231
- - ` factory ` ** any** factory to use
232
- - ` params ` ** any?** predefined parameters
233
- - ` options ` ** any?** options for programmatically generate the attributes
231
+ * ` factory ` ** any** factory to use
232
+ * ` params ` ** any?** predefined parameters
233
+ * ` options ` ** any?** options for programmatically generate the attributes
234
234
235
235
Returns ** [ Promise] [ 5 ] <any >**   ;
236
236
@@ -240,24 +240,28 @@ Generates bunch of records and saves multiple API requests to store them.
240
240
241
241
``` js
242
242
// create 3 posts
243
- I .haveMultiple (' post' , 3 )
243
+ I .haveMultiple (' post' , 3 );
244
244
245
245
// create 3 posts by one author
246
- I .haveMultiple (' post' , 3 , { author: ' davert' })
246
+ I .haveMultiple (' post' , 3 , { author: ' davert' });
247
247
248
248
// create 3 posts by one author with options
249
- I .haveMultiple (' post' , 3 , { author: ' davert' }, { publish_date: ' 01.01.1997' })
249
+ I .haveMultiple (' post' , 3 , { author: ' davert' }, { publish_date: ' 01.01.1997' });
250
250
```
251
251
252
252
#### Parameters
253
253
254
- - ` factory ` ** any**   ;
255
- - ` times ` ** any**   ;
256
- - ` params ` ** any?**   ;
257
- - ` options ` ** any?**   ;
254
+ * ` factory ` ** any**   ;
255
+ * ` times ` ** any**   ;
256
+ * ` params ` ** any?**   ;
257
+ * ` options ` ** any?**   ;
258
258
259
259
[ 1 ] : https://github.com/rosiejs/rosie
260
+
260
261
[ 2 ] : https://www.npmjs.com/package/faker
262
+
261
263
[ 3 ] : http://codecept.io/helpers/REST/
264
+
262
265
[ 4 ] : https://github.com/axios/axios#request-config
266
+
263
267
[ 5 ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise
0 commit comments