-
-
Notifications
You must be signed in to change notification settings - Fork 141
Phantomas scope
Phantomas exposes window.__phantomas
"scope" containing helper functions that can be called from within the page phantomas is run against.
Defined in core/scope.js
file and injected when onInitialized
event is triggered.
Adds a offender message for a given metric:
(function(phantomas) {
if (foo > 1234) {
phantomas.addOffender('foo', 'Value higher than expected in ' + bar);
}
})(window.__phantomas);
Emits an event that phantomas module can bind to.
(function(phantomas) {
phantomas.emit('foo', 'param1', 'param2');
})(window.__phantomas);
// in phantomas module
phantomas.on('foo', function(param1, param2) {
// event handling
});
Increases the value of given metric (by default by 1
):
(function(phantomas) {
var start = Date.now();
phantomas.spy(Element.prototype, 'addEventListener', function(eventType) {
phantomas.incrMetric('eventOn' + eventType);
});
})(window.__phantomas);
Adds an entry to phantomas log (available in --verbose
mode or logged to a file when --log
is provided):
window.__phantomas && window.__phantomas.log("An event foo took place!");
Sets a value of given metric:
(function(phantomas) {
var start = Date.now();
document.addEventListener("DOMContentLoaded", function() {
phantomas.setMetric('onDOMReadyTime', Date.now() - start);
}, false);
})(window.__phantomas);
Sets a value of given metric to the time that elapsed since responseEnd
event (i.e. the arrival of the last byte of the initial HTML):
(function(phantomas) {
document.addEventListener("DOMContentLoaded", function() {
phantomas.setMarkerMetric('onDOMReadyTime');
}, false);
})(window.__phantomas);
Proxy function to trace calls to native DOM methods (used mostly inside phantomas modules):
(function(phantomas) {
phantomas.spy(window.document, 'getElementById', function(id) {
phantomas.log('document.getElementById called with "' + id + '"');
});
})(window.__phantomas);
Callback is provided with arguments original function was called with.