Commit e8b1583f e8b1583fcc9069ed3466e8fdb168b4ec3642c064 by Nicolas Perriault

ClientUtils now logs messages with levels

1 parent 44c240ff
...@@ -788,7 +788,7 @@ ...@@ -788,7 +788,7 @@
788 try { 788 try {
789 return document.querySelectorAll(selector); 789 return document.querySelectorAll(selector);
790 } catch (e) { 790 } catch (e) {
791 console.log('findAll(): invalid selector provided "' + selector + '":' + e); 791 this.log('findAll(): invalid selector provided "' + selector + '":' + e, "error");
792 } 792 }
793 }; 793 };
794 794
...@@ -802,7 +802,7 @@ ...@@ -802,7 +802,7 @@
802 try { 802 try {
803 return document.querySelector(selector); 803 return document.querySelector(selector);
804 } catch (e) { 804 } catch (e) {
805 console.log('findOne(): invalid selector provided "' + selector + '":' + e); 805 this.log('findOne(): invalid selector provided "' + selector + '":' + e, "errors");
806 } 806 }
807 }; 807 };
808 808
...@@ -833,14 +833,24 @@ ...@@ -833,14 +833,24 @@
833 return xhr.responseText; 833 return xhr.responseText;
834 } catch (e) { 834 } catch (e) {
835 if (e.name === "NETWORK_ERR" && e.code === 101) { 835 if (e.name === "NETWORK_ERR" && e.code === 101) {
836 console.log('unfortunately, casperjs cannot make cross domain ajax requests'); 836 this.log("unfortunately, casperjs cannot make cross domain ajax requests", "warning");
837 } 837 }
838 console.log('error while fetching ' + url + ': ' + e); 838 this.log("error while fetching " + url + ": " + e, "error");
839 return ""; 839 return "";
840 } 840 }
841 }; 841 };
842 842
843 /** 843 /**
844 * Logs a message.
845 *
846 * @param String message
847 * @param String level
848 */
849 this.log = function(message, level) {
850 console.log("[casper:" + (level || "debug") + "] " + message);
851 };
852
853 /**
844 * Sets a field (or a set of fields) value. Fails silently, but log 854 * Sets a field (or a set of fields) value. Fails silently, but log
845 * error messages. 855 * error messages.
846 * 856 *
...@@ -855,9 +865,9 @@ ...@@ -855,9 +865,9 @@
855 field = fields[0]; 865 field = fields[0];
856 } 866 }
857 if (!field instanceof HTMLElement) { 867 if (!field instanceof HTMLElement) {
858 console.log('invalid field type; only HTMLElement and NodeList are supported'); 868 this.log("invalid field type; only HTMLElement and NodeList are supported", "error");
859 } 869 }
860 console.log('set "' + field.getAttribute('name') + '" field value to ' + value); 870 this.log('set "' + field.getAttribute('name') + '" field value to ' + value, "debug");
861 var nodeName = field.nodeName.toLowerCase(); 871 var nodeName = field.nodeName.toLowerCase();
862 switch (nodeName) { 872 switch (nodeName) {
863 case "input": 873 case "input":
...@@ -1153,7 +1163,7 @@ ...@@ -1153,7 +1163,7 @@
1153 * @param String style 1163 * @param String style
1154 */ 1164 */
1155 this.formatMessage = function(message, style) { 1165 this.formatMessage = function(message, style) {
1156 var parts = /(\w+\(\))(.*)/.exec(message); 1166 var parts = /([a-z0-9_\.]+\(\))(.*)/i.exec(message);
1157 if (!parts) { 1167 if (!parts) {
1158 return message; 1168 return message;
1159 } 1169 }
...@@ -1282,7 +1292,12 @@ ...@@ -1282,7 +1292,12 @@
1282 page = require('webpage').create(); 1292 page = require('webpage').create();
1283 } 1293 }
1284 page.onConsoleMessage = function(msg) { 1294 page.onConsoleMessage = function(msg) {
1285 casper.log(msg, "info", "remote"); 1295 var level = "info", test = /^\[casper:(\w+)\]\s?(.*)/.exec(msg);
1296 if (test && test.length === 3) {
1297 level = test[1];
1298 msg = test[2];
1299 }
1300 casper.log(msg, level, "remote");
1286 }; 1301 };
1287 page.onLoadStarted = function() { 1302 page.onLoadStarted = function() {
1288 casper.loadInProgress = true; 1303 casper.loadInProgress = true;
......
...@@ -38,7 +38,7 @@ casper.start('tests/site/index.html', function(self) { ...@@ -38,7 +38,7 @@ casper.start('tests/site/index.html', function(self) {
38 self.test.assertEval(function() { 38 self.test.assertEval(function() {
39 return typeof(__utils__) === "object"; 39 return typeof(__utils__) === "object";
40 }, 'start() injects ClientUtils instance within remote DOM'); 40 }, 'start() injects ClientUtils instance within remote DOM');
41 self.test.comment('fetching') 41 self.test.comment('fetching');
42 self.test.assertEquals(self.fetchText('ul li'), 'onetwothree', 'fetchText() can retrieves text contents'); 42 self.test.assertEquals(self.fetchText('ul li'), 'onetwothree', 'fetchText() can retrieves text contents');
43 self.test.comment('encoding'); 43 self.test.comment('encoding');
44 var image = self.base64encode('file://' + phantom.libraryPath + '/site/images/phantom.png'); 44 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"/, ...@@ -116,7 +116,28 @@ casper.test.assertMatch(xunit.getXML(), /<testcase classname="foo" name="bar"/,
116 xunit.addFailure('bar', 'baz', 'wrong', 'chucknorriz'); 116 xunit.addFailure('bar', 'baz', 'wrong', 'chucknorriz');
117 casper.test.assertMatch(xunit.getXML(), /<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/, 'addFailure() adds a failed testcase'); 117 casper.test.assertMatch(xunit.getXML(), /<testcase classname="bar" name="baz"><failure type="chucknorriz">wrong/, 'addFailure() adds a failed testcase');
118 118
119 // Casper.ClientUtils.log
120 casper.then(function(self) {
121 casper.test.comment('client utils log');
122 var oldLevel = casper.options.logLevel;
123 casper.options.logLevel = 'debug';
124 casper.options.verbose = false;
125 casper.evaluate(function() {
126 __utils__.log('debug message');
127 __utils__.log('info message', 'info');
128 });
129 casper.test.assert(casper.result.log.some(function(e) {
130 return e.message === 'debug message' && e.level === 'debug' && e.space === 'remote';
131 }), 'ClientUtils.log() adds a log entry');
132 casper.test.assert(casper.result.log.some(function(e) {
133 return e.message === 'info message' && e.level === 'info' && e.space === 'remote';
134 }), 'ClientUtils.log() adds a log entry at a given level');
135 casper.options.logLevel = oldLevel;
136 casper.options.verbose = true;
137 });
138
139 // run suite
119 casper.run(function(self) { 140 casper.run(function(self) {
120 self.test.assert(self.result.log.length > 0, 'log() logged messages'); 141 self.test.assertEquals(self.result.log.length, 3, 'log() logged messages');
121 self.test.renderResults(true, 0, save); 142 self.test.renderResults(true, 0, save);
122 }); 143 });
......