This repository has been archived by the owner on Aug 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathworker.js
674 lines (586 loc) · 15.8 KB
/
worker.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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
var page = require('webpage').create();
var system = require('system');
var fs = require('fs');
/**
* The options object.
*
* @type {Object}
*/
options = {
// Debug mode
debug: false,
// Verbose mode
verbose: false,
// Screenshot mode
screenshot: false,
// Custom User-Agent
userAgent: ''
};
/**
* The number of requests.
* @type {Number}
*/
var requests = 0;
/**
* The results.
*
* @type {Array}
*/
var results = [];
/**
* The requested page url.
*
* @type {string}
*/
var requestUrl;
/**
* Url reported when request Url is undefined.
*
* @type {string}
*/
var defaultUrl = 'about:blank';
/**
* Parse commandline arguments
*/
uid = system.args[1];
scenario = JSON.parse(system.args[2]);
arguments = JSON.parse(system.args[3]);
for (var id in arguments) {
options[id] = arguments[id];
}
/**
* Set the User-Agent if it was provided.
*/
if (options['userAgent'] !== '') {
page.settings.userAgent = options['userAgent'];
}
/**
* The map of resources that are requested/loaded in the page.
*
* @type {Object}
*/
var resources = {};
/**
* A boolean to indicate if the page is ready or not.
*
* @type {boolean}
*/
var pageReady = false;
/**
* Colors used for formatting the output to look nice on the terminal.
*
* @type {Object}
*/
var textFormatting = {
HEADER: '\033[95m',
OKBLUE: '\033[94m',
OKGREEN: '\033[92m',
WARNING: '\033[93m',
FAIL: '\033[91m',
ENDC: '\033[0m',
BOLD: '\033[1m',
UNDERLINE: '\033[4m'
};
/**
* Is ready function.
* This function is used to check if script should wait for any resources
* before continuing to run any actions in the scenario.
*
* @return {boolean}
*/
function isReady() {
if (action === 0) {
return true;
}
if (pageReady === false) {
return false;
}
for (var id in resources) {
if (resources[id].status == 'requested') {
return false;
}
}
return true;
}
/**
* Indicates the exited status.
*
* @type {boolean}
*/
var exited = false;
/**
* Exit functionality.
*/
function exit() {
if (!exited) {
exited = true;
console.log(JSON.stringify(results, null, 2));
phantom.exit(0);
}
}
// After 120 seconds, we always kill worker, it is probably stuck
setTimeout(exit, 120000);
/**
* Action index.
*
* @type {number}
*/
action = 0;
/**
* Scenario functionality.
*/
function nextAction() {
if (isReady() === true) {
current = scenario[action];
if (!current) {
exit();
}
output(JSON.stringify(current));
var actionSuccess = true;
switch (current.action) {
case 'open_url':
loadpage(current.value);
break;
case 'set_value':
set_value(current.selector, current.value);
break;
case 'submit':
submit(current.selector);
break;
case 'click':
click(current.selector);
break;
case 'click_one':
click_one(current.selector);
break;
case 'sleep':
sleep(current.value);
break;
case 'wait_for_element':
actionSuccess = wait_for_element(current.selector);
break;
case 'check_element_exists':
check_element_exists(current, contains_text);
break;
default:
warn('unknown action: ' + JSON.stringify(current));
}
if (options.screenshot === true) {
// Increase height to capture full page
var pageHeight = page.evaluate(function() {
return document.body.scrollHeight;
});
page.viewportSize = { width: 1920, height: pageHeight };
page.clipRect = { top: 0, left: 0, width: 1920, height: pageHeight };
// Take screenshot and save to file
page.render('screenshot_' + action + '.png');
}
if (actionSuccess === true) {
action++;
}
}
setTimeout(function() {
nextAction();
}, 200);
}
/**
* Set a value for an element.
* @param {string} selector The selector.
* @param {string} value The value, when multiple values are available, one
* is chosen at random.
*/
function set_value(selector, value) {
// Randomly set a value between two values
if (value instanceof Array) {
value = value[Math.floor(Math.random() * value.length)];
output("Using value for set_value: " + value);
}
page.evaluate(function(selector, value) {
qselector = document.querySelector(selector);
qselector.value = value;
// Manually dispatch events for front-end frameworks like Angular2
var eventType;
switch(qselector.tagName) {
case "INPUT":
eventType = "input";
break;
default:
eventType = "change";
break;
}
var event = document.createEvent("UIEvent");
event.initUIEvent(eventType, true, true, window, 0);
qselector.dispatchEvent(event);
}, selector, value);
}
/**
* Submit form.
*
* @param {string} selector The selector.
*/
function submit(selector) {
page.evaluate(function(selector) {
document.querySelector(selector).submit();
}, selector);
}
/**
* Click element.
*
* @param {string} selector The selector.
*/
function click(selector) {
var returnedValue = page.evaluate(function(selector) {
var message = '';
var elements = document.querySelectorAll(selector);
/**
* Click element helper.
*
* @param {string} element The element that will be clicked.
*/
function click_element(element) {
// TODO: click on an <a> element is not supported in all browsers by default.
// find a more clean solution.
if (typeof element.click === 'function') {
element.click();
} else if (element.fireEvent) {
return element.fireEvent('onclick');
} else {
var evObj = document.createEvent('Events');
evObj.initEvent('click', true, false);
element.dispatchEvent(evObj);
}
}
if (elements !== null && elements.length === 1) {
click_element(elements[0]);
} else if (elements !== null && elements.length > 1) {
message = 'Selector ' + selector + ' matches more than 1 element, clicking the first element.';
click_element(elements[0]);
} else {
message = 'Selector ' + selector + ' does not match any element in the dom';
}
return message;
}, selector);
if (returnedValue !== '') {
warn(returnedValue);
}
}
/**
* Clicks a random element in the list.
*
* @param {string} selector The selector.
*/
function click_one(selector) {
var returnedValue = page.evaluate(function(selector) {
elements = document.querySelectorAll(selector);
var message = '';
if (elements === null || elements.length === 0) {
message = 'Selector ' + selector + ' does not match any element in the dom';
} else {
element = elements[Math.floor(Math.random() * elements.length)];
element.click();
}
return message;
}, selector);
if (returnedValue !== '') {
warn(returnedValue);
}
}
/**
* Loads a page or picks a page from a list of urls.
*
* @param {Array.<string>|string} url The url(s) that will be loaded.
*/
function loadpage(url) {
if (url instanceof Array) {
url = url[Math.floor(Math.random() * url.length)];
}
requestUrl = url;
page.viewportSize = { width: 1920, height: 1080 };
page.clipRect = { top: 0, left: 0, width: 1920, height: 1080 };
page.open(url);
}
/**
* Return true if the element is visible.
*
* @param {string} selector The selector.
* @return {boolean}
*/
function wait_for_element(selector) {
var found = page.evaluate(function(selector) {
var elements = document.querySelectorAll(selector);
if (elements !== null && elements.length > 0) {
return true;
} else {
return false;
}
}, selector);
return found;
}
/**
* Makes the script sleeps for amount of time in ms.
*
* @param {number} value value of sleep in ms.
*/
function sleep(value) {
// Randomly select a sleep time between two values
if (value instanceof Object) {
var min = value.min;
var max = value.max;
value = Math.floor(Math.random() * (max - min) ) + min;
}
pageReady = false;
setTimeout(function() {
pageReady = true;
}, value);
}
/**
* Search for a specific element in a page.
*
* @param {object} current scenario.
*
* @param {function} function that do other checks.
*/
function check_element_exists(current, checker) {
var found = page.evaluate(function(current, checker) {
var elements = document.querySelectorAll(current.selector), i;
if (elements === null || elements.length === 0) {
return false;
}
if (typeof checker !== "function") {
return true;
}
// Do other checks if are set.
for (i = 0; i < elements.length; ++i) {
var found = checker(elements[i], current)
if (found === true) {
return true;
}
}
return false;
}, current, checker);
results.push({
'success': found,
'url': requestUrl,
'step': JSON.stringify(current).replace(new RegExp('"', 'g'), '"')
});
return found;
}
/**
* Checker used for check_element_exists to add a content check.
*
* @param {object} a page element.
*
* @param {object} current scenario.
*/
function contains_text(element, current) {
var textProperty = 'textContent' in document ? 'textContent' : 'innerText';
return element[textProperty].indexOf(current.value) > -1
}
/**
* Start the run
*/
nextAction();
/*
* ---------------------------------------------------------------------- *
* Helper functions for logging.
* ---------------------------------------------------------------------- *
*/
/**
* Function used for output verbose logs.
*
* @param {string} message The message.
*/
function output(message) {
if (options['verbose']) {
console.log(pad(uid) + '\t' + Date.now() + ':\t' + message);
}
}
/**
* Function used for output debug logs.
*
* @param {string} message The message.
*/
function debug(message) {
if (options['debug']) {
console.log(pad(uid) + '\t' + Date.now() + ':\t' + message);
}
}
/**
* Function used for output verbose warnings.
*
* @param {string} message The message.
*/
function warn(message) {
if (options['verbose']) {
console.log(textFormatting.WARNING + pad(uid) + '\t' + Date.now() + ':\t' + message + textFormatting.ENDC);
}
}
/**
* Function used for output verbose errors.
*
* @param {string} message The message.
*/
function error(message) {
if (options['verbose']) {
console.log(textFormatting.FAIL + pad(uid) + '\t' + Date.now() + ':\t' + message + textFormatting.ENDC);
}
}
/**
* Function used for padding string with zeroes.
* Source [InfinitiesLoop] http://stackoverflow.com/questions/2998784/how-to-output-integers-with-leading-zeros-in-javascript
*
* @param {number} num The message.
* @param {number} size The message.
* @return {string}
*/
function pad(num, size) {
var s = '00000' + num;
return s.substr(s.length - size);
}
/*
* ---------------------------------------------------------------------- *
* Browser callbacks.
* ---------------------------------------------------------------------- *
*/
/**
* Page error handler.
* @param {string} msg The message.
* @param {Object} trace The trace.
*/
page.onError = function(msg, trace) {
error('Error: ' + msg + '\n' + JSON.stringify(trace));
};
/**
* Resource request handler.
* @param {Object} requestData The request data.
* @param {Object} networkRequest The network request.
*/
page.onResourceRequested = function(requestData, networkRequest) {
debug('onResourceRequested: \n' + JSON.stringify(requestData, networkRequest));
resources[requestData.id] = {
method: requestData.method,
url: requestData.url,
status: 'requested'
};
};
/**
* Resource received handler.
*
* @param {Object} response The response.
*/
page.onResourceReceived = function(response) {
debug('onResourceReceived: \n' + JSON.stringify(response));
if (resources[response.id] !== undefined) {
resources[response.id].status = 'done';
} else {
resources[response.id] = {
status: 'done'
};
}
};
/**
* Resource error handler.
*
* @param {Object} resourceError The resource that produced the error.
*/
page.onResourceError = function(resourceError) {
debug('onResourceError: \n' + JSON.stringify(resourceError));
if (resources[resourceError.id] !== undefined) {
resources[resourceError.id].status = 'error';
} else {
resources[resourceError.id] = {
status: 'error'
};
}
};
/**
* Resource timeout handler.
* @param {Object} request The request that timedout.
*/
page.onResourceTimeout = function(request) {
debug('onResourceTimeout: \n' + JSON.stringify(request));
if (resources[response.id] !== undefined) {
resources[response.id].status = 'timeout';
} else {
resources[response.id] = {
status: 'timeout'
};
}
};
/**
* Url change handler.
*
* @param {string} targetUrl The target url.
*/
page.onUrlChanged = function(targetUrl) {
debug('onUrlChanged: \n TargetUrl: ' + targetUrl);
requestUrl = targetUrl;
};
/**
* The page intialize time in milliseconds.
*
* @type {number}
*/
var pageInitializetime;
/**
* Handle when the page is initialized.
*/
page.onInitialized = function() {
debug('onInitialized ' + requestUrl);
// Increase requests
requests += 1;
// Reset checkReady()
pageReady = false;
resources = {};
// Save load start time
pageInitializetime = Date.now();
};
/**
* Handle when the page load is finished.
*
* @param {string} status The status is either 'suceess' or 'fail'
*/
page.onLoadFinished = function(status) {
debug('onLoadFinished ' + requestUrl + ': \n Status: ' + status);
pageReady = true;
if (status !== 'success') {
results.push({
'url': requestUrl,
'success': false,
'step': JSON.stringify(scenario[action - 1]).replace(new RegExp('"', 'g'), '"')
});
loadpage();
} else if (requestUrl !== defaultUrl){
// Only save results when we know a page was requested, onLoadFinished gets called a lot more then needed
if (pageInitializetime > 0) {
var pageLoadFinishTime = Date.now();
results.push({
'url': requestUrl,
'success': true,
'start': pageInitializetime,
'end': pageLoadFinishTime,
'time': pageLoadFinishTime - pageInitializetime,
'step': JSON.stringify(scenario[action - 1]).replace(new RegExp('"', 'g'), '"')
});
pageInitializetime = 0;
}
}
};
/**
* Handle when a new page is created
*
* @param {object} newPage New page object created by browser
*/
page.onPageCreated = function(newPage) {
newPage.onLoadFinished = function(){
page = newPage;
};
}
/**
* This is for printing any logs or errors that may have happened in the page.evaluate function.
*
* @param {string} msg The logged message.
*/
page.onConsoleMessage = function(msg) {
console.log(msg);
};