-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
144 lines (125 loc) · 4.16 KB
/
test.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
var fs = require('fs');
var utils = require('utils');
casper.options.verbose = true;
casper.options.logLevel = casper.cli.get("logLevel") || 'debug';
casper.options.exitOnError = false; // Keep going on error.
casper.options.timeout = 10 * 60 * 1000; // 10 minutes.
casper.options.pageSettings = {
javascriptEnabled: true,
loadImages: true,
loadPlugins: false,
userAgent: 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36'
};
casper.options.viewportSize = {
width: 1280,
height: 800
};
// HTML logging.
casper.on('open', function (location) {
this.echo(location + ' opened');
});
// Catch JS errors on the page.
casper.on('page.error', function(msg, trace) {
this.test.fail('JavaScript Error: ' + msg);
});
// Catch load errors for the page resources. Ignore some assets that we do not
// care about.
casper.on('resource.error', function(resource) {
if (!resource.url.match(/.*google-analytics\.com.*/) &&
!resource.url.match(/.*fonts\.net.*/) &&
!resource.url.match(/.*pbs\.twimg\.com.*/) &&
!resource.url.match(/.*rubiconproject\.com*/) &&
!resource.url.match(/.*\.googlesyndication\.com*/) &&
!resource.url.match(/.*doubleclick\.net*/) &&
!resource.url.match(/.*ping\.chartbeat.net*/) &&
!resource.url.match(/.*moatads\.com*/) &&
!resource.url.match(/.*twitter\.com.*/)
) {
this.echo(resource.url + " not found.", "RED_BAR");
}
});
// Do not track CasperJS in GA.
casper.on('page.resource.requested', function(requestData, request) {
if (requestData.url.match(/google-analytics\.com/)) {
casper.echo('Request to GA. Aborting: ' + requestData.url);
request.abort();
}
});
// Screenshot fails.
casper.on('step.error', function(failure) {
this.capture('fail.png');
});
var sites = casper.cli.get("sites").split(" ");
var siteURLs = [];
var limit = casper.cli.get("limit") || 20;
var timestamp = casper.cli.get("timestamp") || 1;
var numberOfSuccess = 9 * (limit + 1);
casper.each(sites, function (self, site) {
casper.test.begin('Testing ' + site, numberOfSuccess, function suite(test) {
casper.start(site + '?' + timestamp, function() {
siteURLs = [];
this.echo(this.getTitle());
globalPageTests(this);
var links = this.evaluate(function() {
links = document.getElementsByTagName('a');
links = Array.prototype.map.call(links,function(link){
return link.getAttribute('href');
});
return links;
});
// Add common URLs to the front of the stack.
// @todo Jacob to do this.
var commonUrls = [
//'/user/login',
//'/search/site/french%20open'
];
Array.prototype.forEach.call(commonUrls, function(link) {
links.unshift(link);
});
// Add newly found URLs to the stack
var baseUrl = this.getGlobal('location').origin;
Array.prototype.forEach.call(links, function(link) {
var addLink = true;
// Ensure this link is not an in-page hyperlink.
if (link.match(/^#.*/)) {
addLink = false;
}
// We have already tested the homepage.
else if (link.match(/^\/?$/)) {
addLink = false;
}
// No RSS.
else if (link.match(/\.xml$/)) {
addLink = false;
}
// No query params.
else if (link.match(/\/?\?.*/)) {
addLink = false;
}
// Ensure this link is on the same domain
else if (link.match(/^https?:\/\/.*/) && link.indexOf(baseUrl) !== 0) {
addLink = false;
}
if (addLink) {
var newUrl = absoluteUri(baseUrl, link);
if (siteURLs.indexOf(newUrl) == -1) {
if (siteURLs.length < limit) {
casper.echo(casper.colorizer.format('-> Pushed ' + newUrl + ' onto the stack', { fg: 'magenta' }));
siteURLs.push(newUrl);
}
}
}
});
});
casper.then(function() {
casper.each(siteURLs, function(self, link) {
casper.thenOpen(link + '?' + timestamp, function(a) {
globalPageTests(this);
});
});
});
casper.run(function() {
test.done();
});
});
});