This repository has been archived by the owner on Mar 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
form.js
377 lines (324 loc) · 13 KB
/
form.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import { confirmCalendarDay } from '../functions';
// thx to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
function removeSubstringsWithCurlyBrackets(stringValue) {
const regex = /{.*}/gi;
if (!stringValue.match) {
return stringValue;
}
return stringValue.replace(regex, '');
}
Cypress.Commands.add('clearField', (fieldName, modal) => {
cy.log(`clearField - fieldName=${fieldName}; modal=${modal}`);
const path = createFieldPath(fieldName, modal);
cy.get(path)
.find('input')
.clear();
});
Cypress.Commands.add('getStringFieldValue', (fieldName, modal) => {
cy.log(`getStringFieldValue - fieldName=${fieldName}; modal=${modal}`);
const path = createFieldPath(fieldName, modal);
return cy
.get(path)
.find('input')
.invoke('val'); /* note: beats me why .its('value'); returned undefined */
});
Cypress.Commands.add('getTextFieldValue', (fieldName, modal) => {
cy.log(`getStringFieldValue - fieldName=${fieldName}; modal=${modal}`);
const path = createFieldPath(fieldName, modal);
return cy
.get(path)
.find('textarea')
.invoke('val'); /* note: beats me why .its('value'); returned undefined */
});
Cypress.Commands.add('assertFieldNotShown', (fieldName, modal) => {
cy.log(`assertFieldNotShown - fieldName=${fieldName}; modal=${modal}`);
const path = createFieldPath(fieldName, modal);
return cy.get(path).should('not.exist');
});
function createFieldPath(fieldName, modal) {
let path = `.form-field-${fieldName}`;
if (modal) {
path = `.panel-modal ${path}`;
}
return path;
}
Cypress.Commands.add('getCheckboxValue', (fieldName, modal) => {
cy.log(`getCheckboxValue - fieldName=${fieldName}; modal=${modal}`);
const path = createFieldPath(fieldName, modal);
return cy.get(path).then(el => {
if (el.find('checked').length || el.find('.checked').length) {
return true;
}
return false;
});
});
Cypress.Commands.add('expectCheckboxValue', (fieldName, isChecked, modal) => {
cy.log(`expectCheckboxValue - fieldName=${fieldName}; isChecked=${isChecked}; modal=${modal}`);
const path = createFieldPath(fieldName, modal);
if (isChecked) {
return cy
.get(path)
.find('.checked')
.should('exist');
} else {
return cy
.get(path)
.find('.checked')
.should('not.exist');
}
});
Cypress.Commands.add('resetListValue', (fieldName, modal, rewriteUrl = null) => {
cy.log(`resetListValue - fieldName=${fieldName}; modal=${modal}`);
const patchUrlPattern = rewriteUrl || '/rest/api/window/.*[^/][^N][^E][^W]$';
const patchListValueAliasName = `patchListValue-${fieldName}-${new Date().getTime()}`;
cy.server();
cy.route('PATCH', new RegExp(patchUrlPattern)).as(patchListValueAliasName);
const path = createFieldPath(fieldName, modal);
cy.get(path)
.find('.meta-icon-close-alt')
.click()
.waitForFieldValue(`@${patchListValueAliasName}`, fieldName);
});
Cypress.Commands.add('clickOnIsActive', modal => {
const path = createFieldPath('IsActive', modal);
cy.get(path)
.find('.input-slider')
.click();
});
/*
* @param modal - use true if the field is in a modal overlay; required if the underlying window has a field with the same name
* @param {boolean} skipPatch - if true - the patch request will be skipped
*/
Cypress.Commands.add(
'clickOnCheckBox',
(fieldName, expectedPatchValue, modal, rewriteUrl = null, skipPatch = false) => {
cy.log(`clickOnCheckBox - fieldName=${fieldName}`);
const patchUrlPattern = rewriteUrl || '/rest/api/window/.*[^/][^N][^E][^W]$';
const patchCheckBoxAliasName = `patchCheckBox-${fieldName}-${new Date().getTime()}`;
if (!skipPatch) {
cy.server();
cy.route('PATCH', new RegExp(patchUrlPattern)).as(patchCheckBoxAliasName);
}
cy.log(`clickOnCheckBox - fieldName=${fieldName}; modal=${modal};`);
const path = createFieldPath(fieldName, modal);
cy.get(path)
.find('.input-checkbox-tick')
.click({ force: true }); // we don't care if the checkbox scrolled out of view
if (!skipPatch) {
cy.waitForFieldValue(`@${patchCheckBoxAliasName}`, fieldName, expectedPatchValue);
}
}
);
/*
* Right now it can only select the current date
*/
Cypress.Commands.add('selectDateViaPicker', (fieldName, modal) => {
const path = createFieldPath(fieldName, modal);
cy.get(path)
.find('.datepicker')
.click();
confirmCalendarDay();
cy.get(path)
.find('.form-control-label')
.click();
});
/**Selects a date in the picker
* should not be used for offsets larger than a couple of days
* @param {string} fieldName - name of the field
* @param {number} dayOffset - the number of days before/after today;
* @param {boolean} modal - use true, if the field is in a modal overlay; required if the underlying window has a field with the same name
*/
Cypress.Commands.add('selectOffsetDateViaPicker', (fieldName, dayOffset, modal) => {
const path = createFieldPath(fieldName, modal);
cy.get(path)
.find('.datepicker')
.click();
cy.get('.rdtPicker td').then(e => {
/**get the index of the day to select in the date picker */
let dayIndex = e.index(e.filter('.rdtToday')) + dayOffset;
e.filter(i => dayIndex == i).click();
});
cy.get(path)
.find('.form-control-label')
.click();
});
/**
* Function to fill in text inputs
*
* @param {string} fieldName - name of the field
* @param {string} stringValue - value to put into field
* @param {boolean} modal - use true, if the field is in a modal overlay; required if the underlying window has a field with the same name
* @param {string} rewriteUrl - use custom url for the request
* @param {boolean} noRequest - if set to true, don't wait for the response from the server
*/
Cypress.Commands.add('writeIntoStringField', (fieldName, stringValue, modal, rewriteUrl, noRequest) => {
const aliasName = `writeIntoStringField-${fieldName}-${new Date().getTime()}`;
const expectedPatchValue = removeSubstringsWithCurlyBrackets(stringValue);
// in the default pattern we want to match URLs that do *not* end with "/NEW"
const patchUrlPattern = rewriteUrl || '/rest/api/window';
cy.log(
`writeIntoStringField - fieldName=${fieldName}; stringValue=${stringValue}; modal=${modal}; patchUrlPattern=${patchUrlPattern}`
);
if (!noRequest) {
cy.server();
cy.route('PATCH', new RegExp(patchUrlPattern)).as(aliasName);
}
const path = createFieldPath(fieldName, modal);
cy.get(path)
.find('input')
.type('{selectall}')
.type(stringValue)
.type('{enter}');
if (!noRequest) {
cy.waitForFieldValue(`@${aliasName}`, fieldName, expectedPatchValue);
}
});
/**
* Function to fill in textareas
*
* @param {string} fieldName - name of the field
* @param {string} stringValue - value to put into field
* @param {boolean} modal - use true, if the field is in a modal overlay; required if the underlying window has a field with the same name
* @param {string} rewriteUrl - use custom url for the request
*/
Cypress.Commands.add('writeIntoTextField', (fieldName, stringValue, modal, rewriteUrl) => {
cy.log(`writeIntoTextField - fieldName=${fieldName}; stringValue=${stringValue}; modal=${modal}`);
const aliasName = `writeIntoTextField-${fieldName}-${new Date().getTime()}`;
const expectedPatchValue = removeSubstringsWithCurlyBrackets(stringValue);
// in the default pattern we want to match URLs that do *not* end with "/NEW"
const patchUrlPattern = rewriteUrl || '/rest/api/window/.*[^/][^N][^E][^W]$';
// here we want to match URLs that don *not* end with "/NEW"
cy.server();
cy.route('PATCH', new RegExp(patchUrlPattern)).as(aliasName);
const path = createFieldPath(fieldName, modal);
cy.get(path)
.find('textarea')
.type(`${stringValue}{enter}`);
cy.waitForFieldValue(`@${aliasName}`, fieldName, expectedPatchValue);
});
/**
* @param modal - use true, if the field is in a modal overlay; required if the underlying window has a field with the same name
* @param typeList - use when selecting value from a list not lookup field. Someone thought it's a great idea to return different
* responses for different fields.
* @param {boolean} skipRequest - if set to true, the PATCH request will be skipped
*/
Cypress.Commands.add(
'writeIntoLookupListField',
(
fieldName,
partialValue,
expectedListValue,
typeList = false,
modal = false,
rewriteUrl = null,
skipRequest = false
) => {
let path = `#lookup_${fieldName}`;
if (modal) {
path = `.panel-modal ${path}`;
}
const aliasName = `writeIntoLookupListField-${fieldName}-${new Date().getTime()}`;
//the value to wait for would not be e.g. "Letter", but {key: "540408", caption: "Letter"}
const expectedPatchValue = removeSubstringsWithCurlyBrackets(partialValue);
// in the default pattern we want to match URLs that do *not* end with "/NEW"
const patchUrlPattern = rewriteUrl || '/rest/api/window';
if (!skipRequest) {
cy.server();
cy.route('PATCH', new RegExp(patchUrlPattern)).as(aliasName);
}
cy.get(path).within(el => {
if (el.find('.lookup-widget-wrapper input').length) {
return (
cy
.get('input')
// we can't use `clear` here as sometimes it triggers request to the server
// and then the whole flow becomes flaky
.type('{selectall}')
.type(partialValue)
);
}
// this is extremely fiddly when selecting from a combo field such as bpartner address.
// it will work locally, but most of the times will fail in jenkins.
// Please create the tests such that adding an address in a combo field is not mandatory!
return cy.get('.lookup-dropdown').click();
});
cy.get('.input-dropdown-list').should('exist');
cy.contains('.input-dropdown-list-option', expectedListValue).click(/*{ force: true }*/);
if (!skipRequest) {
cy.waitForFieldValue(`@${aliasName}`, fieldName, expectedPatchValue, typeList /*expectEmptyRequest*/);
}
cy.get('.input-dropdown-list .input-dropdown-list-header').should('not.exist');
}
);
/**
* Select the given list value in a static list.
*
* @param {boolean} modal - use true, if the field is in a modal overlay; requered if the underlying window has a field with the same name
* @param {boolean} skipRequest - if set to true, cypress won't expect a request to the server and won't wait for it
*/
Cypress.Commands.add('selectInListField', (fieldName, listValue, modal, rewriteUrl = null, skipRequest) => {
cy.log(`selectInListField - fieldName=${fieldName}; listValue=${listValue}; modal=${modal}`);
const patchListFieldAliasName = `patchListField-${fieldName}-${new Date().getTime()}`;
const patchUrlPattern = rewriteUrl || '/rest/api/window/.*[^/][^N][^E][^W]$';
// here we want to match URLs that don *not* end with "/NEW"
if (!skipRequest) {
cy.server();
cy.route('PATCH', new RegExp(patchUrlPattern)).as(patchListFieldAliasName);
}
const path = createFieldPath(fieldName, modal);
cy.get(path)
.find('.input-dropdown')
.click();
// no f*cki'n clue why it started going ape shit when there was the correct '.input-dropdown-list-option' here
cy.get('.input-dropdown-list')
.contains(listValue)
.click({ force: true });
if (!skipRequest) {
cy.waitForFieldValue(`@${patchListFieldAliasName}`, fieldName, listValue);
}
});
/**
* Select the option with a given index from a static list. This command does not wait for response from the server.
*
* @param {string} fieldName - id of the field to select from
* @param {number} index - index of the item to select
* @param {boolean} modal - use true, if the field is in a modal overlay; requered if the underlying window has a field with the same name
*/
Cypress.Commands.add('selectNthInListField', (fieldName, index, modal) => {
cy.log(`selectNthInListField - fieldName=${fieldName}; index=${index}; modal=${modal}`);
const path = createFieldPath(fieldName, modal);
cy.get(path)
.find('.input-dropdown')
.click();
cy.get('.input-dropdown-list-option').then(options => {
for (let i = 0; i < options.length; i += 1) {
if (i === index) {
cy.get(options[i]).click();
}
}
});
});
Cypress.Commands.add(
'setCheckBoxValue',
(fieldName, isChecked, modal = false, rewriteUrl = null, skipRequest = false) => {
cy.log(`Set the Checkbox value ${fieldName} to ${isChecked}`);
// the expected value is the same as the checked state
// (used only for verification if the checkbox has the correct value)
const expectedPatchValue = isChecked;
cy.getCheckboxValue(fieldName, modal).then(theCheckboxValue => {
if (isChecked) {
if (theCheckboxValue) {
// Nothing to do, already checked
} else {
cy.clickOnCheckBox(fieldName, expectedPatchValue, modal, rewriteUrl, skipRequest);
}
} else {
if (theCheckboxValue) {
cy.clickOnCheckBox(fieldName, expectedPatchValue, modal, rewriteUrl, skipRequest);
} else {
// Nothing to do, already unchecked
}
}
});
}
);