@@ -28,9 +28,9 @@ Most of web application have API, and it can be used to create and delete test r
2828By combining REST API with Factories you can easily create records for tests:
2929
3030``` js
31- I .
have (
' user' , { login
: ' davert' , email
: ' [email protected] ' })
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
: ' [email protected] ' })
; 32+ let id = await I .have (' post' , { title: ' My first post' });
33+ I .haveMultiple (' comment' , 3 , {post_id: id});
3434```
3535
3636To make this work you need
@@ -53,14 +53,14 @@ See the example for Posts factories:
5353``` js
5454// tests/factories/posts.js
5555
56- const { Factory } = require (' rosie' )
57- const { faker } = require (' @faker-js/faker' )
56+ const { Factory } = require (' rosie' );
57+ const { faker } = require (' @faker-js/faker' );
5858
5959module .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 ());
6464```
6565
6666For more options see [ rosie documentation] [ 1 ] .
@@ -71,12 +71,12 @@ Then configure ApiDataHelper to match factories and REST API:
7171
7272ApiDataFactory has following config options:
7373
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
8080
8181See the example:
8282
@@ -121,19 +121,19 @@ For instance, to set timeout you should add:
121121
122122By default to create a record ApiDataFactory will use endpoint and plural factory name:
123123
124- - create: ` POST {endpoint}/{resource} data `
125- - delete: ` DELETE {endpoint}/{resource}/id `
124+ * create: ` POST {endpoint}/{resource} data `
125+ * delete: ` DELETE {endpoint}/{resource}/id `
126126
127127Example (` endpoint ` : ` http://app.com/api ` ):
128128
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 `
131131
132132This behavior can be configured with following options:
133133
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}" } `
137137
138138Requests can also be overridden with a function which returns [ axois request config] [ 4 ] .
139139
@@ -146,31 +146,31 @@ delete: (id) => ({ method: 'delete', url: '/posts', data: { id } })
146146Requests can be updated on the fly by using ` onRequest ` function. For instance, you can pass in current session from a cookie.
147147
148148``` 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+ }
154154```
155155
156156### Responses
157157
158158By default ` I.have() ` returns a promise with a created data:
159159
160160``` js
161- let client = await I .have (' client' )
161+ let client = await I .have (' client' );
162162```
163163
164164Ids of created records are collected and used in the end of a test for the cleanup.
165165If you need to receive ` id ` instead of full response enable ` returnId ` in a helper config:
166166
167167``` js
168168// returnId: false
169- let clientId = await I .have (' client' )
169+ let clientId = await I .have (' client' );
170170// clientId == 1
171171
172172// returnId: true
173- let clientId = await I .have (' client' )
173+ let clientId = await I .have (' client' );
174174// client == { name: 'John', email: '[email protected] ' }175175```
176176
@@ -190,47 +190,47 @@ By default `id` property of response is taken. This behavior can be changed by s
190190
191191### Parameters
192192
193- - ` config `   ;
193+ * ` config `   ;
194194
195- ### \ _ requestCreate
195+ ### _ requestCreate
196196
197197Executes request to create a record in API.
198198Can be replaced from a in custom helper.
199199
200200#### Parameters
201201
202- - ` factory ` ** any**   ;
203- - ` data ` ** any**   ;
202+ * ` factory ` ** any**   ;
203+ * ` data ` ** any**   ;
204204
205- ### \ _ requestDelete
205+ ### _ requestDelete
206206
207207Executes request to delete a record in API
208208Can be replaced from a custom helper.
209209
210210#### Parameters
211211
212- - ` factory ` ** any**   ;
213- - ` id ` ** any**   ;
212+ * ` factory ` ** any**   ;
213+ * ` id ` ** any**   ;
214214
215215### have
216216
217217Generates a new record using factory and saves API request to store it.
218218
219219``` js
220220// create a user
221- I .have (' user' )
221+ I .have (' user' );
222222// create user with defined email
223223// and receive it when inside async function
224- const user = await I .
have (
' user' , { email
: ' [email protected] ' }) 224+ const user = await I .
have (
' user' , { email
: ' [email protected] ' }); 225225// 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 })
227227```
228228
229229#### Parameters
230230
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
234234
235235Returns ** [ Promise] [ 5 ] <any >**   ;
236236
@@ -240,24 +240,28 @@ Generates bunch of records and saves multiple API requests to store them.
240240
241241``` js
242242// create 3 posts
243- I .haveMultiple (' post' , 3 )
243+ I .haveMultiple (' post' , 3 );
244244
245245// create 3 posts by one author
246- I .haveMultiple (' post' , 3 , { author: ' davert' })
246+ I .haveMultiple (' post' , 3 , { author: ' davert' });
247247
248248// 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' });
250250```
251251
252252#### Parameters
253253
254- - ` factory ` ** any**   ;
255- - ` times ` ** any**   ;
256- - ` params ` ** any?**   ;
257- - ` options ` ** any?**   ;
254+ * ` factory ` ** any**   ;
255+ * ` times ` ** any**   ;
256+ * ` params ` ** any?**   ;
257+ * ` options ` ** any?**   ;
258258
259259[ 1 ] : https://github.com/rosiejs/rosie
260+
260261[ 2 ] : https://www.npmjs.com/package/faker
262+
261263[ 3 ] : http://codecept.io/helpers/REST/
264+
262265[ 4 ] : https://github.com/axios/axios#request-config
266+
263267[ 5 ] : https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise
0 commit comments