-
Notifications
You must be signed in to change notification settings - Fork 0
/
usertesting-job-crawler.js
141 lines (124 loc) · 3.18 KB
/
usertesting-job-crawler.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
// CLI usage:
// phantomjs [--ssl-protocol=any] usertesting-job-crawler.js [-v|--verbose]
var system = require('system');
var fs = require('fs');
var VERBOSE = false;
var loadInProgress = false;
// Calculate path of this file
var PWD = '';
var current_path_arr = system.args[0].split('/');
if (current_path_arr.length == 1) {
PWD = '.';
} else {
current_path_arr.pop();
PWD = current_path_arr.join('/');
}
// ...from command
var configFile = PWD + '/config.json'
system.args.forEach(function(val, i) {
if (val == '-v' || val == '--verbose') {
VERBOSE = true;
}
if (val == '--config') {
if (system.args.length == i) console.log('failed to set config - no option given');
else {
configFile = system.args[i + 1];
}
}
});
try {
var settings = JSON.parse(fs.read(configFile));
if (!settings.username || !settings.password || !settings.init_url) {
console.log('Missing username, password, and/or initial URL. Exiting...');
phantom.exit();
}
} catch (e) {
console.log('Could not find ' + configFile);
phantom.exit();
}
var page = require('webpage').create();
page.onConsoleMessage = function(msg) {
if (!VERBOSE) {
return;
}
console.log(msg);
};
page.onError = function(msg, trace) {
if (!VERBOSE) {
return;
}
console.error('Error on page: ' + msg);
}
page.onCallback = function(query, msg) {
if (query == 'username') {
return settings.username;
}
if (query == 'password') {
return settings.password;
}
if (query == 'report-jobs') {
if (VERBOSE) { console.log('Found the following jobs: ' + msg); }
else { console.log(msg); }
return;
}
if (query == 'report-no-jobs') {
if (VERBOSE) { console.log('No jobs found'); }
else { console.log('None'); }
return;
}
if (query == 'fatal-error') {
console.log('Fatal error: ' + msg);
phantom.exit();
}
return null;
}
page.onLoadStarted = function() {
loadInProgress = true;
};
page.onLoadFinished = function() {
loadInProgress = false;
};
page.viewportSize = {
width: 1920,
height: 1080
};
page.open(settings.init_url);
var steps = [
function() { // Log in
page.evaluate(function() {
document.getElementById('user_email').value = window.callPhantom('username');
document.getElementById('user_password').value = window.callPhantom('password');
document.forms["new_user"].submit();
});
},
function() { // dashboard
page.render('ut.png');
page.evaluate(function() {
var tbl = document.querySelector(".available-test__table");
var jobs = tbl.querySelector('tbody').rows;
var jobCount = jobs.length;
if (jobCount > 0) {
var removals = tbl.querySelectorAll(".banner--error, .input--checkbox, .btn-group");
for (var i = 0; i < removals.length; i++) {
removals[i].parentNode.removeChild(removals[i]);
}
var html = tbl.outerHTML;
window.callPhantom('report-jobs', html);
}
else {
window.callPhantom('report-no-jobs');
}
});
}
];
var i = 0;
interval = setInterval(function() {
if (loadInProgress) {
return;
}
if (!steps[i] || typeof steps[i] != "function") {
return phantom.exit();
}
steps[i]();
i++;
}, 300);