-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.js
586 lines (471 loc) · 16.4 KB
/
actions.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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
'use strict';
const {
isResultItem,
isDOMNode,
describeDOMObject,
cssSelectorFirst,
cssSelectorArray,
xpathFirst,
xpathArray,
resultItemToDocument,
documentReadyState,
hasFormattingBox,
textContent,
} = require('./dom');
const {resultToArray, resultCount, executeSuccess, executePendingTag, executeFatalFailureTag} = require('./result');
const freezeCopyIfArray = value => {
if (Array.isArray(value)) {
return Object.freeze(value.slice());
}
return value;
};
const funcOrValue = value => (typeof value === 'function' ? value() : value);
const LEFT_QUOTE = '\u201c';
const RIGHT_QUOTE = '\u201d';
const HORIZONTAL_ELLIPSIS = '\u2026';
const EMPTY_FROZEN_ARRAY = Object.freeze([]);
const CALLBACK_DESCRIPTION_MAX_LENGTH = 64;
exports.parseTimeoutArgument = timeout => {
if (typeof timeout === 'string' && /^([\d.]+)s$/.test(timeout)) {
return parseFloat(timeout) * 1000;
}
if (typeof timeout === 'number') {
return timeout;
}
throw Error('Invalid timeout argument, expected a number or a duration string');
};
exports.describeCallback = func => {
const funcString = func.toString();
if (funcString.length <= CALLBACK_DESCRIPTION_MAX_LENGTH) {
return funcString;
}
return funcString.slice(0, CALLBACK_DESCRIPTION_MAX_LENGTH - 1) + HORIZONTAL_ELLIPSIS;
};
class Action {
/**
* @param {?Window|Node|Array.<(Window|Node)>} currentResult
* @param {Object} metaData
* @return {{status: string, reasonStrings: string[], reasonValues: string[], value: (?Window|Node|Array.<(Window|Node)>)}}
*/
execute(currentResult, metaData) {
return executeSuccess(currentResult);
}
/**
* @param {number} timeout milliseconds
* @return {string}
*/
describe(timeout) {
return '';
}
/**
* If true, this action should trigger a default amount check. Unless the user provides an amount check
* For example if this value is true for "myAction": `.myAction()` would have the same effect as `.myAction().amount(1, Infinity)`
* @return {boolean}
*/
get wantsDefaultAmountCheck() {
return false;
}
/**
* If true, this action perform an an amount check and the default amount check should not be applied.
* @return {boolean}
*/
get appliesAmountCheck() {
return false;
}
/**
* Schedule an additional check after this many milliseconds have passed since the start of the expression
* @return {number[]}
*/
get additionalCheckTimeout() {
return EMPTY_FROZEN_ARRAY;
}
}
class Noop extends Action {
constructor() {
super();
Object.freeze(this);
}
execute(currentResult) {
return executeSuccess(currentResult);
}
describe() {
return '';
}
}
class Target extends Action {
/**
* @param {?Window|Node|Array.<(Window|Node)>|Function} targets
*/
constructor(targets) {
super();
this.results = freezeCopyIfArray(targets);
Object.freeze(this);
}
execute(currentResult) {
const results = freezeCopyIfArray(funcOrValue(this.results));
for (const result of resultToArray(results)) {
if (!isResultItem(result)) {
return executeFatalFailureTag`a value that is not a WindowProxy nor a HTMLDocument, nor an Element was set as the target`;
}
}
return executeSuccess(results);
}
describe() {
const results = this.results;
// (some DOM nodes are typeof function)
if (typeof results === 'function' && !isDOMNode(results)) {
return `sets the target using a callback: \`${exports.describeCallback(results)}\``;
}
if (Array.isArray(results)) {
const descriptions = [];
for (const result of results) {
if (descriptions.length >= 5) {
descriptions.push('\u2026');
break;
}
descriptions.push(describeDOMObject(result));
}
return `sets the target to [${descriptions.join(', ')}]`;
}
return `sets the target to ${describeDOMObject(results)}`;
}
}
class Amount extends Action {
constructor(minimum, maximum = minimum) {
super();
this.minimum = minimum;
this.maximum = maximum;
if (typeof this.minimum !== 'number') {
throw Error('.amount(minimum, maximum): minimum must be a number');
}
if (typeof this.maximum !== 'number') {
throw Error('.amount(minimum, maximum): maximum must be a number');
}
if (this.minimum > this.maximum) {
throw Error('.amount(minimum, maximum): maximum must be greater than or equal to minimum');
}
Object.freeze(this);
}
execute(currentResult) {
const count = resultCount(currentResult);
if (count < this.minimum) {
if (count === 0) {
return executePendingTag`no results were found, instead of a minimum of ${this.minimum} results`;
}
return executePendingTag`only ${count} results were found, instead of a minimum of ${this.minimum} results`;
}
if (count > this.maximum) {
return executePendingTag`${count} results were found, instead of a maximum of ${this.maximum} results`;
}
return executeSuccess(currentResult);
}
describe(timeout) {
const prefix = `waits up to ${timeout / 1000} seconds until`;
if (this.minimum === this.maximum) {
return `${prefix} exactly ${this.minimum} results are found`;
}
if (this.minimum === 1 && this.maximum === Infinity) {
return `${prefix} a result is found`;
}
if (this.maximum === Infinity) {
return `${prefix} ${this.minimum} or more results are found`;
}
return `${prefix} between ${this.minimum} and ${this.maximum} (inclusive) results are found`;
}
get appliesAmountCheck() {
return true;
}
}
class Selector extends Action {
constructor(expression) {
super();
this.expression = expression;
Object.freeze(this);
}
execute(currentResult) {
const expression = funcOrValue(this.expression);
for (const resultItem of resultToArray(currentResult)) {
const result = cssSelectorFirst(resultItem, expression);
if (result) {
return executeSuccess(result);
}
}
return executeSuccess(null);
}
describe() {
if (typeof this.expression === 'function') {
return `finds the first descendant element matching a CSS selector from ` +
`a callback: \`${exports.describeCallback(this.expression)}\``;
}
return `finds the first descendant element matching the CSS selector ${LEFT_QUOTE}${this.expression}${RIGHT_QUOTE}`;
}
get wantsDefaultAmountCheck() {
return true;
}
}
class SelectorAll extends Action {
constructor(expression) {
super();
this.expression = expression;
Object.freeze(this);
}
execute(currentResult) {
const expression = funcOrValue(this.expression);
const elementListList = [];
for (const resultItem of resultToArray(currentResult)) {
const elementList = cssSelectorArray(resultItem, expression);
elementListList.push(elementList);
}
const value = [].concat(...elementListList);
Object.freeze(value);
return executeSuccess(value);
}
describe() {
if (typeof this.expression === 'function') {
return `finds all descendant elements matching a CSS selector from a callback: ` +
`\`${exports.describeCallback(this.expression)}\``;
}
return `finds all descendant elements matching the CSS selector ${LEFT_QUOTE}${this.expression}${RIGHT_QUOTE}`;
}
get wantsDefaultAmountCheck() {
return true;
}
}
class XPath extends Action {
constructor(expression) {
super();
this.expression = expression;
Object.freeze(this);
}
execute(currentResult) {
const expression = funcOrValue(this.expression);
for (const resultItem of resultToArray(currentResult)) {
const result = xpathFirst(resultItem, expression);
if (result && !isResultItem(result)) {
return executeFatalFailureTag`a value that is not an Element was returned by the XPath expression`;
}
if (result) {
return executeSuccess(result);
}
}
return executeSuccess(null);
}
describe() {
if (typeof this.expression === 'function') {
return `finds the first element matching a XPath expression from a callback: ` +
`\`${exports.describeCallback(this.expression)}\``;
}
return `finds the first element matching the XPath expression ${LEFT_QUOTE}${this.expression}${RIGHT_QUOTE}`;
}
get wantsDefaultAmountCheck() {
return true;
}
}
class XPathAll extends Action {
constructor(expression) {
super();
this.expression = expression;
Object.freeze(this);
}
execute(currentResult) {
const expression = funcOrValue(this.expression);
const elementListList = [];
for (const resultItem of resultToArray(currentResult)) {
const elementList = xpathArray(resultItem, expression);
elementListList.push(elementList);
}
const value = [].concat(...elementListList);
Object.freeze(value);
for (const resultItem of value) {
if (!isResultItem(resultItem)) {
return executeFatalFailureTag`a value that is not an Element was returned by the XPath expression`;
}
}
return executeSuccess(value);
}
describe() {
if (typeof this.expression === 'function') {
return `finds all elements matching a XPath expression from a callback: ` +
`\`${exports.describeCallback(this.expression)}\``;
}
return `finds all elements matching the XPath expression ${LEFT_QUOTE}${this.expression}${RIGHT_QUOTE}`;
}
get wantsDefaultAmountCheck() {
return true;
}
}
class DocumentInteractive extends Action {
constructor() {
super();
Object.freeze(this);
}
execute(currentResult) {
for (const resultItem of resultToArray(currentResult)) {
const document = resultItemToDocument(resultItem);
if (documentReadyState(document) === 'loading') {
return executePendingTag`the HTML document has not yet been parsed`;
}
}
return executeSuccess(currentResult);
}
describe(timeout) {
return `waits up to ${timeout / 1000} seconds until the HTML document has finished parsing`;
}
}
class DocumentComplete extends Action {
constructor() {
super();
Object.freeze(this);
}
execute(currentResult) {
for (const resultItem of resultToArray(currentResult)) {
const document = resultItemToDocument(resultItem);
const readyState = documentReadyState(document);
if (readyState === 'loading') {
return executePendingTag`the HTML document has not yet been parsed`;
}
if (readyState === 'interactive') {
return executePendingTag`the HTML document has not yet been loaded`;
}
}
return executeSuccess(currentResult);
}
describe(timeout) {
return `waits up to ${timeout / 1000} seconds until all synchronous resources of the HTML document have been loaded`;
}
}
class Delay extends Action {
constructor(timeout) {
super();
this.delayMs = exports.parseTimeoutArgument(timeout);
Object.freeze(this);
}
execute(currentResult, {executionStart, checkStart}) {
const {delayMs} = this;
const timeSinceStart = checkStart - executionStart;
if (timeSinceStart < delayMs) {
// eslint-disable-next-line max-len
return executePendingTag`the delay of ${delayMs / 1000} seconds has not yet elapsed, only ${timeSinceStart / 1000} seconds have elapsed so far`;
}
return executeSuccess(currentResult);
}
describe() {
return `waits until ${this.delayMs / 1000} seconds have elapsed since the start of the execution`;
}
get additionalCheckTimeout() {
return Object.freeze([this.delayMs]);
}
}
class IsDisplayed extends Action {
constructor() {
super();
Object.freeze(this);
}
execute(currentResult) {
if (!currentResult) {
return executeSuccess(currentResult);
}
if (Array.isArray(currentResult)) {
const result = currentResult.filter(object => hasFormattingBox(object));
return executeSuccess(Object.freeze(result));
}
const result = hasFormattingBox(currentResult) ? currentResult : null;
return executeSuccess(result);
}
describe() {
return `but only including elements which are displayed on the page`;
}
}
class Check extends Action {
constructor(callback) {
if (typeof callback !== 'function') {
throw Error('.check(callback): callback must be a function');
}
super();
this.callback = callback;
Object.freeze(this);
}
execute(currentResult) {
if (!currentResult) {
return executeSuccess(currentResult);
}
if (Array.isArray(currentResult)) {
const result = currentResult.filter(object => this.callback(object));
return executeSuccess(Object.freeze(result));
}
const result = this.callback(currentResult) ? currentResult : null;
return executeSuccess(result);
}
describe() {
return `but only including results that match a callback: \`${exports.describeCallback(this.callback)}\``;
}
}
class ContainsText extends Action {
constructor(text) {
super();
if (!text) {
throw Error('.containsText(text): text must be a string or RegExp');
}
if (typeof text !== 'string' && !(typeof text.test === 'function' && typeof text.exec === 'function')) {
throw Error('.containsText(text): text must be a string or RegExp');
}
this.text = text;
Object.freeze(this);
}
execute(currentResult) {
const test = resultItem => {
const content = textContent(resultItem);
return typeof this.text === 'string'
? content.indexOf(this.text) >= 0
: this.text.test(content);
};
if (!currentResult) {
return executeSuccess(currentResult);
}
if (Array.isArray(currentResult)) {
const result = currentResult.filter(test);
return executeSuccess(Object.freeze(result));
}
const result = test(currentResult) ? currentResult : null;
return executeSuccess(result);
}
describe() {
if (typeof this.text === 'string') {
return `but only including results that contain the text: ${LEFT_QUOTE}${this.text}${RIGHT_QUOTE}`;
}
return `but only including results that contain text matching the regular expression: ${this.text}`;
}
}
class First extends Action {
constructor() {
super();
Object.freeze(this);
}
execute(currentResult) {
if (Array.isArray(currentResult)) {
const result = currentResult.length ? currentResult[0] : null;
return executeSuccess(result);
}
return executeSuccess(currentResult);
}
describe() {
return `but only returning the first result`;
}
}
const DEFAULT_AMOUNT_ACTION = new Amount(1, Infinity);
exports.Action = Action;
exports.Noop = Noop;
exports.Target = Target;
exports.Amount = Amount;
exports.DEFAULT_AMOUNT_ACTION = DEFAULT_AMOUNT_ACTION;
exports.Selector = Selector;
exports.SelectorAll = SelectorAll;
exports.XPath = XPath;
exports.XPathAll = XPathAll;
exports.DocumentInteractive = DocumentInteractive;
exports.DocumentComplete = DocumentComplete;
exports.Delay = Delay;
exports.IsDisplayed = IsDisplayed;
exports.Check = Check;
exports.ContainsText = ContainsText;
exports.First = First;