Skip to content

Commit

Permalink
Test fixes for modern browsers
Browse files Browse the repository at this point in the history
  • Loading branch information
nicjansma committed Apr 4, 2018
1 parent 9c3e863 commit 6fd3713
Show file tree
Hide file tree
Showing 43 changed files with 437 additions and 174 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ module.exports = function() {
{
// Send beacons to null
pattern: /beacon_url: .*/,
replacement: "beacon_url: \"/blackhole\","
replacement: "beacon_url: \"/beacon\","
}
]
}
Expand Down
3 changes: 2 additions & 1 deletion boomerang.js
Original file line number Diff line number Diff line change
Expand Up @@ -1962,7 +1962,8 @@ BOOMR_check_doc_domain();
};

if (w.requestIdleCallback) {
w.requestIdleCallback(cb);
// set a timeout since rIC doesn't get called reliably in chrome headless
w.requestIdleCallback(cb, {timeout: 1000});
}
else if (w.setImmediate) {
w.setImmediate(cb);
Expand Down
1 change: 1 addition & 0 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"lodash": "~3.0.0",
"mocha": "~3.4.2",
"resourcetiming-compression": "^0.3.3",
"usertiming": "~0.1.8",
"usertiming-compression": "~0.1.4"
},
"resolutions": {
Expand Down
17 changes: 12 additions & 5 deletions plugins/auto-xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,11 +678,12 @@

/**
* Called once the resource can be sent
* @param markEnd Sets loadEventEnd once the function is run
* @param {boolean} [markEnd] Sets loadEventEnd once the function is run
* @param {number} [endTimestamp] End timestamp
*/
var sendResponseEnd = function(markEnd) {
var sendResponseEnd = function(markEnd, endTimestamp) {
if (markEnd) {
resource.timing.loadEventEnd = BOOMR.now();
resource.timing.loadEventEnd = endTimestamp || BOOMR.now();
}

// send any queued beacons first
Expand Down Expand Up @@ -736,7 +737,12 @@
// don't wait for onload if this was an aborted SPA navigation
if ((!ev || !ev.aborted) && d && d.readyState && d.readyState !== "complete") {
BOOMR.window.addEventListener("load", function() {
sendResponseEnd(true);
var loadTimestamp = BOOMR.now();

// run after the 'load' event handlers so loadEventEnd is captured
BOOMR.setImmediate(function() {
sendResponseEnd(true, loadTimestamp);
});
});

return;
Expand Down Expand Up @@ -1023,7 +1029,7 @@

// if the attribute change affected the src/currentSrc attributes we want to know that
// as that means we need to fetch a new Resource from the server
if (node._bmr && node._bmr.res && node._bmr.end[node._bmr.res]) {
if (node._bmr && typeof node._bmr.res === "number" && node._bmr.end[node._bmr.res]) {
exisitingNodeSrcUrlChanged = true;
}

Expand Down Expand Up @@ -1110,6 +1116,7 @@
// update _bmr with details about this resource
node._bmr.res = resourceNum;
node._bmr.idx = index;
delete node._bmr.end[resourceNum];

node.addEventListener("load", function(ev) { self.load_cb(ev, resourceNum); });
node.addEventListener("error", function(ev) { self.load_cb(ev, resourceNum); });
Expand Down
10 changes: 4 additions & 6 deletions plugins/navtiming.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,10 @@
data.nt_ssl_st = pt.secureConnectionStart;
}

// XXX Inconsistency warning. msFirstPaint is in milliseconds while Chrome's firstPaintTime is in seconds.microseconds.
// The server needs to deal with this.

if (pt.msFirstPaint) {
if (p.timing && p.timing.msFirstPaint) {
// msFirstPaint is IE9+ http://msdn.microsoft.com/en-us/library/ff974719
data.nt_first_paint = pt.msFirstPaint;
// and is in Unix Epoch format
data.nt_first_paint = p.timing.msFirstPaint;
}

if (pt.workerStart) {
Expand Down Expand Up @@ -347,7 +345,7 @@
}

for (k in data) {
if (data.hasOwnProperty(k) && !data[k]) {
if (data.hasOwnProperty(k) && data[k] === undefined) {
delete data[k];
}
}
Expand Down
Empty file added tests/assets/blank.js
Empty file.
69 changes: 61 additions & 8 deletions tests/boomerang-test-framework.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
fired_onbeacon: false,
fired_before_unload: false,
beacons: [],
sendBeacons: [],
page_ready: function() {
this.fired_page_ready = true;
},
Expand Down Expand Up @@ -56,6 +57,24 @@
return true;
}
};

(function() {
var savedSendBeacon;
if (window.navigator && typeof window.navigator.sendBeacon === "function") {
savedSendBeacon = window.navigator.sendBeacon;
window.navigator.sendBeacon = function(url, data) {
var result = savedSendBeacon.apply(window.navigator, arguments);
if (result) {
var reader = new FileReader();
reader.addEventListener("loadend", function() {
BOOMR.plugins.TestFramework.sendBeacons.push(reader.result);
});
reader.readAsText(data);
}
return result;
};
}
})();
})(window);

//
Expand All @@ -81,7 +100,7 @@
//
// Constants
//
t.BEACON_URL = "/blackhole";
t.BEACON_URL = "/beacon";
t.MAX_RESOURCE_WAIT = 500;

//
Expand Down Expand Up @@ -294,9 +313,9 @@
t.isUserTimingSupported = function() {
// don't check for PerformanceMark or PerformanceMeasure, they aren't polyfilled in usertiming.js
return (window.performance &&
typeof window.performance.getEntriesByType === "function" &&
typeof window.performance.mark === "function" &&
typeof window.performance.measure === "function");
typeof window.performance.getEntriesByType === "function" &&
typeof window.performance.mark === "function" &&
typeof window.performance.measure === "function");
};

t.isNetworkAPISupported = function() {
Expand Down Expand Up @@ -365,10 +384,12 @@
};

t.clearCookies = function() {
var date = new Date();
date.setTime(date.getTime() - (24 * 60 * 60 * 1000));
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var name = cookies[i].split("=")[0];
document.cookie = [name + "=", "expires" + new Date(), "path=/", "domain=" + location.hostname].join("; ");
var name = cookies[i].split("=")[0].trim();
document.cookie = [name + "=", "expires=" + date.toGMTString(), "path=/", "domain=" + location.hostname].join("; ");
}
};

Expand Down Expand Up @@ -724,9 +745,12 @@
var objName = objs[i];
var subObj = window.performance[objName];

copy[objName] = {};

if (subObj) {
if (typeof subObj === "function") {
copy[objName] = window.performance[objName];
continue;
}
copy[objName] = {};
for (var subObjAttr in subObj) {
copy[objName][subObjAttr] = subObj[subObjAttr];
}
Expand Down Expand Up @@ -769,4 +793,33 @@
// This only works if the test framework is loaded before boomerang
window.BOOMR_LOGN_always = false;

/*eslint-disable no-extend-native*/
// Polyfill via https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind
if (!Function.prototype.bind) {
Function.prototype.bind = function(oThis) {
if (typeof this !== "function") {
// closest thing possible to the ECMAScript 5
// internal IsCallable function
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable");
}

var aArgs = Array.prototype.slice.call(arguments, 1),
fToBind = this,
fNOP = function() {},
fBound = function() {
return fToBind.apply(this instanceof fNOP ? this : oThis,
aArgs.concat(Array.prototype.slice.call(arguments)));
};

if (this.prototype) {
// Function.prototype doesn't have a prototype property
fNOP.prototype = this.prototype;
}
fBound.prototype = new fNOP();

return fBound;
};
}
/*eslint-enable no-extend-native*/

}(window));
16 changes: 13 additions & 3 deletions tests/e2e/e2e-debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//
// Imports
//
var fs = require("fs");
var chai = require("chai");
var assert = chai.assert;
var path = require("path");
Expand All @@ -20,9 +19,19 @@ var ports = require(testsFile).ports;
function run(testPath, file) {
describe(testPath, function() {
var fileName = file + ".html";

it("Should pass " + testPath + "/" + fileName, function(done) {
var logCount = 0;

if (typeof browser.waitForAngularEnabled === "function") {
browser.waitForAngularEnabled(false);
}

console.log(
"Navigating to",
"http://" + servers.main + ":" + ports.main + "/pages/" + testPath + "/" + fileName
);

browser.driver.get("http://" + servers.main + ":" + ports.main + "/pages/" + testPath + "/" + fileName);

setInterval(function() {
Expand All @@ -42,9 +51,10 @@ function run(testPath, file) {
return browser.driver.isElementPresent(by.css("#BOOMR_test_complete"));
});

browser.driver.executeScript("return BOOMR_test.isComplete()").then(function(complete){
browser.driver.executeScript("return BOOMR_test.isComplete()").then(function(complete) {
assert.equal(complete, true, "BOOMR_test.isComplete()");
browser.driver.executeScript("return BOOMR_test.getTestFailureMessages()").then(function(testFailures){

browser.driver.executeScript("return BOOMR_test.getTestFailureMessages()").then(function(testFailures) {
if (testFailures.length > 0) {
throw new Error(testFailures);
}
Expand Down
27 changes: 19 additions & 8 deletions tests/e2e/e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,40 @@
//
var chai = require("chai");
var assert = chai.assert;
var path = require("path");

var testsFile = path.join(__dirname, "e2e.json");
var tests = require(testsFile).tests;
var servers = require(testsFile).server;
var ports = require(testsFile).ports;

var tests = require("./e2e.json").tests;
var servers = require("./e2e.json").server;
var ports = require("./e2e.json").ports;
var disabledTests = require("./e2e.disabled.json");

//
// Functions
//
function run(path, file) {
describe(path, function() {
function run(testPath, file) {
describe(testPath, function() {
var fileName = file + ".html";

it("Should pass " + path + "/" + fileName, function(done) {
it("Should pass " + testPath + "/" + fileName, function(done) {

if (typeof browser.waitForAngularEnabled === "function") {
browser.waitForAngularEnabled(false);
}

browser.driver.get("http://" + servers.main + ":" + ports.main + "/pages/" + path + "/" + fileName);
console.log(
"Navigating to",
"http://" + servers.main + ":" + ports.main + "/pages/" + testPath + "/" + fileName
);

browser.driver.get("http://" + servers.main + ":" + ports.main + "/pages/" + testPath + "/" + fileName);

browser.driver.wait(function() {
return browser.driver.isElementPresent(by.css("#BOOMR_test_complete"));
});

browser.driver.executeScript("return BOOMR_test.isComplete()").then(function(complete) {

assert.equal(complete, true, "BOOMR_test.isComplete()");

browser.driver.executeScript("return BOOMR_test.getTestFailureMessages()").then(function(testFailures) {
Expand Down
11 changes: 10 additions & 1 deletion tests/karma.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,18 @@ module.exports = function(config) {
plugins: [
"karma-coverage",
"karma-mocha",

// reporters
"karma-tap-reporter",
"karma-mocha-reporter",
"karma-phantomjs-launcher"

// launchers
"karma-chrome-launcher",
"karma-firefox-launcher",
"karma-ie-launcher",
"karma-opera-launcher",
"karma-phantomjs-launcher",
"karma-safari-launcher"
],
browsers: ["PhantomJS"],

Expand Down
21 changes: 11 additions & 10 deletions tests/page-templates/00-basic/10-method-queue.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
<%= header %>
<%= boomerangSnippet %>
<script src="10-method-queue.js" type="text/javascript"></script>
<script>
window.BOOMR_mq = [];
window.BOOMR_mq = window.BOOMR_mq || [];

window.BOOMR_mq.push(
["addVar", "var1", "value1"],
["addVar", "var2", "value2"]
);
</script>
<%= boomerangScript %>
<script src="10-method-queue.js" type="text/javascript"></script>
<script>
window.BOOMR_mq.push(
["addVar", "var3", "value3"],
["addVar", "var4", "value4"]
);

BOOMR_test.init({
testAfterOnBeacon: true,
onBoomerangLoaded: function() {
window.BOOMR_mq.push(
["addVar", "var3", "value3"],
["addVar", "var4", "value4"]
);
}
testAfterOnBeacon: true
});
</script>
<%= footer %>
4 changes: 2 additions & 2 deletions tests/page-templates/03-load-order/00-before-page-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ describe("e2e/03-load-order/00-before-page-load", function() {
}
});

it("Should have a end timestamp sometime after the NavigationTiming's loadEventStart timestamp and before now (if NavTiming supported)", function() {
it("Should have a end timestamp sometime after the NavigationTiming's loadEventEnd timestamp and before now (if NavTiming supported)", function() {
var b = tf.lastBeacon();
var now = +(new Date());
if (window.performance && window.performance.timing && window.performance.timing.navigationStart) {
assert.operator(b["rt.end"], ">=", window.performance.timing.loadEventStart);
assert.operator(b["rt.end"], ">=", window.performance.timing.loadEventEnd);
assert.operator(b["rt.end"], "<=", now);
}
else {
Expand Down
4 changes: 2 additions & 2 deletions tests/page-templates/03-load-order/01-after-page-load.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ describe("e2e/03-load-order/01-after-page-load", function() {
}
});

it("Should have a end timestamp equal to NavigationTiming's loadEventStart timestamp (if NavTiming supported)", function() {
it("Should have a end timestamp equal to NavigationTiming's loadEventEnd timestamp (if NavTiming supported)", function() {
var b = tf.lastBeacon();
if (window.performance && window.performance.timing) {
assert.equal(b["rt.end"], window.performance.timing.loadEventStart);
assert.equal(b["rt.end"], window.performance.timing.loadEventEnd);
}
else {
return this.skip();
Expand Down
Loading

0 comments on commit 6fd3713

Please sign in to comment.