This repository has been archived by the owner on Nov 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
131 lines (115 loc) · 5.23 KB
/
functions.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
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
};
// Do not track CasperJS in GA.
casper.options.onResourceRequested = function(casper, requestData, request) {
if (requestData.url.match(/google-analytics\.com/)) {
casper.log('Request to GA. Aborting: ' + requestData.url, 'debug');
request.abort();
}
};
// 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.
casper.on('resource.error', function(resourceError) {
if (resourceError.url != "" &&
!resourceError.url.match(/.*fonts\.net.*/) &&
!resourceError.url.match(/.*pbs\.twimg\.com.*/) &&
!resourceError.url.match(/.*twitter\.com.*/)
) {
casper.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')', 'warning');
casper.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString, 'warning');
}
});
// Screenshot fails.
casper.on('step.error', function(failure) {
this.capture('fail.png');
});
// Turn a (possibly) relative URI into a full RFC 3986-compliant URI
// With minor modifications, courtesy: https://gist.github.com/Yaffle/1088850
function absoluteUri(base, href) {
// Parse a URI and return its constituent parts
function parseUri(url) {
var match = String(url).replace(/^\s+|\s+$/g, '').match(/^([^:\/?#]+:)?(\/\/(?:[^:@]*(?::[^:@]*)?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/);
return (match ? { href: match[0] || '', protocol: match[1] || '', authority: match[2] || '', host: match[3] || '', hostname: match[4] || '',
port: match[5] || '', pathname: match[6] || '', search: match[7] || '', hash: match[8] || '' } : null);
}
// Resolve dots in the path
function resolvePathDots(input) {
var output = [];
input.replace(/^(\.\.?(\/|$))+/, '')
.replace(/\/(\.(\/|$))+/g, '/')
.replace(/\/\.\.$/, '/../')
.replace(/\/?[^\/]*/g, function (part) { part === '/..' ? output.pop() : output.push(part); });
return output.join('').replace(/^\//, input.charAt(0) === '/' ? '/' : '');
}
// Parse base and href
href = parseUri(href || '');
base = parseUri(base || '');
// Build and return the URI
return !href || !base ? null : (href.protocol || base.protocol) +
(href.protocol || href.authority ? href.authority : base.authority) +
(resolvePathDots(href.protocol || href.authority || href.pathname.charAt(0) === '/' ? href.pathname : (href.pathname ? ((base.authority && !base.pathname ? '/' : '') + base.pathname.slice(0, base.pathname.lastIndexOf('/') + 1) + href.pathname) : base.pathname))) +
(href.protocol || href.authority || href.pathname ? href.search : (href.search || base.search)) + href.hash;
}
/**
* Global pages tests, that are able to run on all valid Drupal pages.
*
* 10 test so far in here (with the caching tests commented out).
*/
function globalPageTests(casp) {
casp.test.assertHttpStatus(200);
casp.test.assertExists('title');
casp.test.assertDoesntExist('.warning', 'No Drupal warnings found');
casp.test.assertDoesntExist('.error', 'No Drupal errors found');
casp.test.assertDoesntExist('.node-unpublished', 'No unpublished nodes found');
casp.test.assertTextDoesntExist('PHP Fatal', 'No PHP fatals found');
// This is the required snippet to send all page views to a communal GA
// bucket.
//
// drush vset googleanalytics_codesnippet_after "ga('create', 'UA-54970022-1', 'auto', {'name': 'govcms'}); ga('govcms.send', 'pageview', {'anonymizeIp': true});"
casp.test.assertMatch(casp.getPageContent(), /.*UA-54970022-1.*/i, 'Found the shared Google Analytics tracker');
// Try to find a fonts.com broken font banner.
casp.test.assertDoesntExist('#mti_wfs_colophon', 'No fonts.com banner found');
// Caching headers.
var foundCacheHeader = false;
casp.currentResponse.headers.forEach(function(header) {
if (header.name == 'Cache-Control') {
foundCacheHeader = true;
casp.test.assertMatch(header.value, /public, max-age=\d{3,}/, 'Page is cacheable in Varnish and Akamai');
var cacheSecondsMatches = header.value.match(/max-age=(\d+)/);
if (cacheSecondsMatches.length > 1) {
var cacheSeconds = parseInt(cacheSecondsMatches[1], 10);
if (cacheSeconds >= 240) {
casp.test.pass('Page cache lifetime is >= than 4 minutes (' + cacheSeconds + ' seconds)');
}
else {
casp.test.fail('Page cache lifetime is < than 4 minutes (' + cacheSeconds + ' seconds)');
}
}
}
});
if (!foundCacheHeader) {
casp.test.fail('Found no "Cache-Control" header');
casp.test.fail('Page cache lifetime is < than 4 minutes');
}
}