Skip to content
stefanjudis edited this page Sep 4, 2014 · 1 revision

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.

Helper functions

addOffender

Adds a offender message for a given metric:

(function(phantomas) {
  if (foo > 1234) {
    phantomas.addOffender('foo', 'Value higher than expected in ' + bar);
  }
})(window.__phantomas);

emit

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
});

incrMetric

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);

log

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!");

setMetric

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);

setMarkerMetric

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);

spy

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.

Clone this wiki locally