-
Notifications
You must be signed in to change notification settings - Fork 0
/
casper_ext.js
387 lines (350 loc) · 10.6 KB
/
casper_ext.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
/*
* Casper_UM is an extension of the casperjs singleton instance
*
* Copyright (c) 2015
*
* Author: Ryan Long
* Version: 1.0
*/
/* global patchRequire, module*/
var require = patchRequire(require),
casper = require("casper").create(),
exec = require("child_process").execFile;
casper.options.pageSettings = {
loadImages: false,
loadPlugins: false,
userAgent: "Mozilla/5.0 (Windows NT 6.3; rv:36.0) Gecko/20100101 Firefox/36.0",
webSecurityEnabled: false,
ignoreSslErrors: true, //this bs doesnt work
localToRemoteUrlAccessEnabled: true,
XSSAuditingEnabled: false
};
casper.options.logLevel = "info";
casper.options.verbose = false;
casper.options.waitTimeout = 120 * 1000;
/********************************************************
* CASPER VARIABLES
* ******************************************************/
casper.pageIsLoaded = true;
casper.account = {};
casper.logType = {
"DEBUG": 1,
"REQUEST": 1,
"RESPONSE": 1,
"INFO": 2,
"MESSAGE": 2,
"WARN": 3,
"ERROR": 4
};
/********************************************************
* UTILITY FUNCTIONS
* ******************************************************/
/**
* Returns a time stamp
*
* @method timesStamp
* @return {String} Returns a string containing the date and local time
*/
casper.timeStamp = function () {
"use strict";
var date = new Date();
return (date.toDateString() + " " + date.toLocaleTimeString());
};
/**
* Saves a snapshot of the current web page
*
* @method snap
* @param {string} path - (optional) the path to store the screen shot. The default is the current directory with /timestamp/_screenshot.png
* @return {Boolean} Returns true on success
*/
casper.snap = function (path) {
"use strict";
this.capture(path || new Date().getTime().toString() + "_screenshot.png");
return true;
};
/**
* Saves a snapshot of the current web page and exits execution
*
* @method snapAndExit
* @param {string} path - (optional) the path to store the screen shot. The default is the current directory with /timestamp/_screenshot.png
* @return {Boolean} Returns true on success
*/
casper.snapAndExit = function (path) {
"use strict";
this.capture(path || new Date().getTime().toString() + "_screenshot.png");
this.exit();
return true;
};
//TODO STILL IN DEVELOPMENT - DO NOT USE
casper.fileType = function (path) {
"use strict";
var fileType = null;
exec("file", [path], null, function (error, stdout, stderr) {
fileType = stdout;
});
while (!fileType) {}
fileType = fileType.replace(path, "").replace(/:/g, "").replace(/^\s/, "");
return fileType;
};
/**
* Determines if a file is a PDF
*
* @method isPDF
* @param {string} path - (optional) the path to store the screen shot. The default is the current directory with /timestamp/_screenshot.png
* @return {Boolean} Returns true if the file is a PDF, false otherwise.
*/
casper.isPDF = function (path) {
"use strict";
//ensure the file exists, log error message and return false
if (!require('fs').exists(path)) {
casper.log(path + " does not exist", "error");
return false;
}
//get the contents of the file
var searchString = require('fs').read(path);
//if file starts with %PDF- and ends with %%EOF, good chance its a pdf
if (searchString.trim().search(/^%PDF-/) === -1 && searchString.trim().search(/$%%EOF/) === -1) {
return false;
} else {
return true;
}
};
/**
* Removes all leading and trailing whitespace and carriage return from a string
* @method trim
* @param {String} string - the string to trim
* @return {String} Returns the trimmed string
*/
casper.trim = function (string) {
"use strict";
return string.trim().replace("\n", "").replace(/\s{2,}/g, " ");
};
/**
* Waits for page to fully load then executes callback
* @method waitForPageLoad
* @param {Function} callback - callback function
*/
casper.waitForPageLoad = function (callback) {
"use strict";
this.waitFor(function () {
return this.pageIsLoaded;
}, function () {
callback();
});
};
/********************************************************
* DEBUGGING METHODS
* ******************************************************/
/**
* Assertion to test if a statement evaluates to true or false. If the statement
* evaluates to false, message is logged to console and a callback is fired
*
* @method assert
* @param {Boolean} test - statement to test
* @param {String} message - the message to log if the test is true
* @param {Function} callback
*/
casper.assert = function (test, message, callback) {
"use strict";
if (!test) {
this.log(message, "error");
if (callback) {
callback();
}
}
};
/**
* Assertion to test if a statement evaluates to true or false. If the statement
* evaluates to false, message is logged to console and a callback is fired
*
* @method assert
* @param {Boolean} test - statement to test
* @param {String} message - the message to log if the test is true
* @param {Function} callback
*/
casper.assertNot = function (test, message, callback) {
"use strict";
if (test) {
this.log(message, "error");
if (callback) {
callback();
}
}
};
/**
* Logs messges to the console.
*
* If no type is given, the default of "INFO" is used.
*
* @method log
* @param {String} Argument[0] the message to be logged
* @param {String} Argument[1] the log type
*/
//redefine casper.log to write to stderr instead of stdout
/*eslint-disable*/
casper.log = function () {
var type = (arguments[1] === undefined) ? "INFO" : arguments[1].toString().toUpperCase(),
msg = arguments[0];
if (casper.logType[type] >= casper.logType[this.options.logLevel.toUpperCase()]) {
switch (type) {
case "MESSAGE":
type = "\033[1;97;42m[REMSG]\033[0m ";
break;
case "REQUEST":
type = "\033[1;97;45m[ RQST]\033[0m ";
break;
case "RESPONSE":
type = "\033[1;97;44m[ RESP]\033[0m ";
break;
case "DEBUG":
type = "\033[;93m[DEBUG]\033[0m ";
break;
case "ERROR":
type = "\033[1;97;41m[ERROR]\033[0m ";
break;
case "WARN":
type = "\033[1;91;220m[ WARN]\033[0m ";
break;
case "INFO":
default:
type = "\033[;92m[ INFO]\033[0m ";
break;
}
require("system").stderr.write(type + "\033[96m" + this.timeStamp() + "\033[97m " + msg + "\033[0m\n");
}
};
/*eslint-enable*/
/**
* Exits out of casper and sends an error object to STDOUT
*
* @method errorExit
* @param {String} Argument 0 the error code
* @param {String} Argument 1 the error type
* @param {String} Argument 2 the error message
*/
casper.errorExit = function () {
"use strict";
var errorObject = {
"success": false,
"errorCode": arguments[0],
"errorType": arguments[1],
"errorMsg": arguments[2]
};
require("system").stdout.write(JSON.stringify(errorObject, null, 4) + "\n");
this.exit();
};
/**
* Converts an object to JSON string
*
* @method renderJSON
* @param {Object} object the object to be rendered as JSON
* @return {String} Returns the object converted to JSON string
*/
casper.renderJSON = function (object) {
"use strict";
return JSON.stringify(object, null, 4);
};
/**
* Removes all empty spaces and carriage returns from string
*
* @method parseString
* @param {String} text - the text to be parsed
* @return {String} returns the parsed string
*/
casper.parseString = function (text) {
return text.trim().replace("\n", "").replace(/\s{2,}/g, ' ');
}
/**
* Parses a date object to YYYY-MM-DD
*
* @method parseDate
* @param {Date} date an instance of Date
* @return {String} Returns the parsed date string
*/
casper.parseDate = function (date) {
"use strict";
var year, day, month;
year = date.getFullYear();
day = date.getDate();
month = date.getMonth() + 1;
day = "0" + day;
month = "0" + month;
day = day.slice(-2);
month = month.slice(-2);
return (year.toString() + "-" + month.toString() + "-" + day.toString());
};
/********************************************************
* CASPER EVENT LISTENERS
* *****************************************************/
casper.on("page.initialized", function () {
"use strict";
this.log("Current URL: " + this.getCurrentUrl(), "info");
});
casper.on("page.error", function (err, trace) {
"use strict";
this.log("Page error\n" + JSON.stringify(err, null, 4) + trace, "warn");
});
casper.on("timeout", function (err) {
"use strict";
this.log("Timeout\n" + JSON.stringify(err, null, 4), "error");
});
casper.on("waitFor.timeout", function (err) {
"use strict";
this.log("waitFor timeout\n" + JSON.stringify(err, null, 4), "error");
});
casper.on("complete.error", function (err) {
"use strict";
this.log("Complete error\n" + JSON.stringify(err, null, 4), "warn");
});
casper.on("load.failed", function (err) {
"use strict";
this.log("Load failed\n" + JSON.stringify(err, null, 4), "warn");
});
casper.on("step.error", function (err) {
"use strict";
this.log("Step error\n" + JSON.stringify(err, null, 4), "warn");
});
casper.on("resource.error", function (err) {
"use strict";
//Loops through all errors that should be ignored to prevent the script from stopping on acceptable errors
this.log("Resource error\n" + JSON.stringify(err, null, 4), "warn");
});
casper.on("error", function (err, trace) {
"use strict";
this.log("Unknown error\n" + JSON.stringify(err, null, 4) + trace, "warn");
});
//Used to catch remote messages, like console.log messages in a CasperJS
//evaluate()
casper.on("remote.message", function (msg) {
"use strict";
this.log(msg, "message");
});
casper.on("remote.alert", function (msg) {
"use strict";
this.log(msg, "message");
});
//Used to tell if a page has finished loading or not
casper.on("load.started", function () {
"use strict";
this.pageIsLoaded = false;
});
//Used to tell if a page has finished loading or not
casper.on("load.finished", function () {
"use strict";
this.pageIsLoaded = true;
});
//For Debugging
casper.on("resource.requested", function (request) {
"use strict";
if (casper.options.verbose === true) {
this.log(JSON.stringify(request, null, 4), "request");
}
});
//For Debugging
casper.on("resource.received", function (response) {
"use strict";
if (casper.options.verbose === true) {
this.log(JSON.stringify(response, null, 4), "response");
}
});
module.exports = casper;