1+ // ***********************************************
2+ // This example commands.js shows you how to
3+ // create various custom commands and overwrite
4+ // existing commands.
5+ //
6+ // For more comprehensive examples of custom
7+ // commands please read more here:
8+ // https://on.cypress.io/custom-commands
9+ // ***********************************************
10+ //
11+ //
12+ // -- This is a parent command --
13+ // Cypress.Commands.add("login", (email, password) => { ... })
14+ //
15+ //
16+ // -- This is a child command --
17+ // Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... })
18+ //
19+ //
20+ // -- This is a dual command --
21+ // Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... })
22+ //
23+ //
24+ // -- This is will overwrite an existing command --
25+ // Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })
26+
27+ async function reinitialisation ( ) {
28+ const kuzzle = Cypress . env ( 'kuzzle' ) ;
29+
30+ return cy
31+ . request ( {
32+ url : `http://${ kuzzle . host } :${ kuzzle . port } /${ kuzzle . index } ` ,
33+ method : 'DELETE' ,
34+ failOnStatusCode : false
35+ } )
36+ . then ( createIndexResponse => {
37+ cy . log ( `Request : create ${ kuzzle . index } status : ${ createIndexResponse . status } ` ) ;
38+ cy . wait ( 500 ) ;
39+ } ) ;
40+ }
41+
42+ Cypress . Commands . add ( 'createMessage' , ( body ) => {
43+ const kuzzle = Cypress . env ( 'kuzzle' ) ;
44+ return cy
45+ . request ( {
46+ url : `http://${ kuzzle . host } :${ kuzzle . port } /${ kuzzle . index } /${ kuzzle . collection } /_create` ,
47+ method : 'POST' ,
48+ body : body ,
49+ } )
50+ . its ( 'body' )
51+ . then ( response => {
52+ cy . log ( `Create status : ${ response . status } {${ body . text } }` ) ;
53+ cy . wait ( 500 ) ;
54+ return cy
55+ . request ( {
56+ url : `http://${ kuzzle . host } :${ kuzzle . port } /${ kuzzle . index } /_refresh` ,
57+ method : 'POST' ,
58+ } ) ;
59+ } )
60+ . then ( refreshResponse => {
61+ cy . log ( `Request : refresh ; status : ${ refreshResponse . status } ` ) ;
62+ cy . wait ( 1000 ) ;
63+ } ) ;
64+ } ) ;
65+
66+
67+ Cypress . Commands . add ( 'initialisation' , ( ) => {
68+ const kuzzle = Cypress . env ( 'kuzzle' ) ;
69+ return cy
70+ . request ( {
71+ url : `http://${ kuzzle . host } :${ kuzzle . port } /${ kuzzle . index } ` ,
72+ method : 'DELETE' ,
73+ failOnStatusCode : false
74+ } )
75+ . then ( deleteResponse => {
76+ cy . log ( `Request : delete ${ kuzzle . index } status : ${ deleteResponse . status } ` ) ;
77+ cy . wait ( 2000 ) ;
78+ // Create index
79+ return cy
80+ . request ( {
81+ url : `http://${ kuzzle . host } :${ kuzzle . port } /${ kuzzle . index } /_create` ,
82+ method : 'POST' ,
83+ } ) ;
84+ } )
85+ . then ( createIndexResponse => {
86+ cy . log ( `Request : create ${ kuzzle . index } status : ${ createIndexResponse . status } ` ) ;
87+ cy . wait ( 500 ) ;
88+ // Create collection
89+ return cy . request ( {
90+ url : `http://${ kuzzle . host } :${ kuzzle . port } /${ kuzzle . index } /${ kuzzle . collection } ` ,
91+ method : 'PUT' ,
92+ body : { }
93+ } ) ;
94+ } ) . then ( createCollectionResponse => {
95+ cy . log ( `Request : create ${ kuzzle . collection } status : ${ createCollectionResponse . status } ` ) ;
96+ cy . wait ( 500 ) ;
97+ } ) ;
98+ } ) ;
99+
100+ Cypress . Commands . add ( 'loadEnvironment' , ( env ) => {
101+ const kuzzle = Cypress . env ( 'kuzzle' ) ;
102+ if ( ! env . messages ) {
103+ reinitialisation ( ) ;
104+ } else {
105+ cy . initialisation ( )
106+ . then ( ( ) => {
107+ return cy
108+ . request ( {
109+ url : `http://${ kuzzle . host } :${ kuzzle . port } /${ kuzzle . index } /${ kuzzle . collection } /_mCreate` ,
110+ method : 'POST' ,
111+ body : { 'documents' : env . messages } ,
112+ } ) ;
113+ } )
114+ . then ( response => {
115+ cy . log ( `mCreate status : ${ response . status } ` ) ;
116+ cy . wait ( 500 ) ;
117+ return cy . request ( {
118+ url : `http://${ kuzzle . host } :${ kuzzle . port } /${ kuzzle . index } /_refresh` ,
119+ method : 'POST' ,
120+ } ) ;
121+ } )
122+ . then ( ( response ) => {
123+ cy . log ( `refresh status : ${ response . status } ` ) ;
124+ cy . wait ( 500 ) ;
125+ } ) ;
126+ }
127+ } ) ;
0 commit comments