Commit e8b1583f e8b1583fcc9069ed3466e8fdb168b4ec3642c064 by Nicolas Perriault

ClientUtils now logs messages with levels

1 parent 44c240ff
......@@ -788,7 +788,7 @@
try {
return document.querySelectorAll(selector);
} catch (e) {
console.log('findAll(): invalid selector provided "' + selector + '":' + e);
this.log('findAll(): invalid selector provided "' + selector + '":' + e, "error");
}
};
......@@ -802,7 +802,7 @@
try {
return document.querySelector(selector);
} catch (e) {
console.log('findOne(): invalid selector provided "' + selector + '":' + e);
this.log('findOne(): invalid selector provided "' + selector + '":' + e, "errors");
}
};
......@@ -833,14 +833,24 @@
return xhr.responseText;
} catch (e) {
if (e.name === "NETWORK_ERR" && e.code === 101) {
console.log('unfortunately, casperjs cannot make cross domain ajax requests');
this.log("unfortunately, casperjs cannot make cross domain ajax requests", "warning");
}
console.log('error while fetching ' + url + ': ' + e);
this.log("error while fetching " + url + ": " + e, "error");
return "";
}
};
/**
* Logs a message.
*
* @param String message
* @param String level
*/
this.log = function(message, level) {
console.log("[casper:" + (level || "debug") + "] " + message);
};
/**
* Sets a field (or a set of fields) value. Fails silently, but log
* error messages.
*
......@@ -855,9 +865,9 @@
field = fields[0];
}
if (!field instanceof HTMLElement) {
console.log('invalid field type; only HTMLElement and NodeList are supported');
this.log("invalid field type; only HTMLElement and NodeList are supported", "error");
}
console.log('set "' + field.getAttribute('name') + '" field value to ' + value);
this.log('set "' + field.getAttribute('name') + '" field value to ' + value, "debug");
var nodeName = field.nodeName.toLowerCase();
switch (nodeName) {
case "input":
......@@ -1153,7 +1163,7 @@
* @param String style
*/
this.formatMessage = function(message, style) {
var parts = /(\w+\(\))(.*)/.exec(message);
var parts = /([a-z0-9_\.]+\(\))(.*)/i.exec(message);
if (!parts) {
return message;
}
......@@ -1282,7 +1292,12 @@
page = require('webpage').create();
}
page.onConsoleMessage = function(msg) {
casper.log(msg, "info", "remote");
var level = "info", test = /^\[casper:(\w+)\]\s?(.*)/.exec(msg);
if (test && test.length === 3) {
level = test[1];
msg = test[2];
}
casper.log(msg, level, "remote");
};
page.onLoadStarted = function() {
casper.loadInProgress = true;
......
......@@ -38,7 +38,7 @@ casper.start('tests/site/index.html', function(self) {
self.test.assertEval(function() {
return typeof(__utils__) === "object";
}, 'start() injects ClientUtils instance within remote DOM');
self.test.comment('fetching')
self.test.comment('fetching');
self.test.assertEquals(self.fetchText('ul li'), 'onetwothree', 'fetchText() can retrieves text contents');
self.test.comment('encoding');
var image = self.base64encode('file://' + phantom.libraryPath + '/site/images/phantom.png');
......@@ -116,7 +116,28 @@ casper.test.assertMatch(xunit.getXML(), /<testcase classname="foo" name="bar"/,
xunit.addFailure('bar', 'baz', 'wrong', 'chucknorriz');
casper.test.assertMatch(xunit.getXML(), /<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/, 'addFailure() adds a failed testcase');
// Casper.ClientUtils.log
casper.then(function(self) {
casper.test.comment('client utils log');
var oldLevel = casper.options.logLevel;
casper.options.logLevel = 'debug';
casper.options.verbose = false;
casper.evaluate(function() {
__utils__.log('debug message');
__utils__.log('info message', 'info');
});
casper.test.assert(casper.result.log.some(function(e) {
return e.message === 'debug message' && e.level === 'debug' && e.space === 'remote';
}), 'ClientUtils.log() adds a log entry');
casper.test.assert(casper.result.log.some(function(e) {
return e.message === 'info message' && e.level === 'info' && e.space === 'remote';
}), 'ClientUtils.log() adds a log entry at a given level');
casper.options.logLevel = oldLevel;
casper.options.verbose = true;
});
// run suite
casper.run(function(self) {
self.test.assert(self.result.log.length > 0, 'log() logged messages');
self.test.assertEquals(self.result.log.length, 3, 'log() logged messages');
self.test.renderResults(true, 0, save);
});
......