Commit 7bd5c1b5 7bd5c1b5b3b323e6bafe203c8588fff27cfaaac8 by Nicolas Perriault

closes #180 - local http test server

CasperJS test suites are now run against a local phantomjs based
HTTP server.

A new `casperjs selftest` command has been added to conveniently
run CasperJS own test suite.
1 parent 76e0b5c1
...@@ -6,4 +6,4 @@ before_script: ...@@ -6,4 +6,4 @@ before_script:
6 - "export DISPLAY=:99.0" 6 - "export DISPLAY=:99.0"
7 - "sh -e /etc/init.d/xvfb start" 7 - "sh -e /etc/init.d/xvfb start"
8 script: 8 script:
9 - "DISPLAY=:99.0 ./bin/casperjs test tests/suites" 9 - "DISPLAY=:99.0 ./bin/casperjs selftest"
......
...@@ -7,6 +7,7 @@ XXXX-XX-XX, v1.0.0 ...@@ -7,6 +7,7 @@ XXXX-XX-XX, v1.0.0
7 - fixed [#178](https://github.com/n1k0/casperjs/issues/178) - added `Casper.getPageContent()` to access raw page body contents on non-html received content-types. 7 - fixed [#178](https://github.com/n1k0/casperjs/issues/178) - added `Casper.getPageContent()` to access raw page body contents on non-html received content-types.
8 - fixed [#164](https://github.com/n1k0/casperjs/issues/164) - ability to force CLI parameters as strings (see [related documentation](http://casperjs.org/cli.html#raw)). 8 - fixed [#164](https://github.com/n1k0/casperjs/issues/164) - ability to force CLI parameters as strings (see [related documentation](http://casperjs.org/cli.html#raw)).
9 - fixed [#153](https://github.com/n1k0/casperjs/issues/153) - erroneous mouse event results when event.preventDefault() was used. 9 - fixed [#153](https://github.com/n1k0/casperjs/issues/153) - erroneous mouse event results when event.preventDefault() was used.
10 - closed [#180](https://github.com/n1k0/casperjs/issues/180) - CasperJS tests are now run against a local HTTP test server. A new `casper selftest` command has been added as well.
10 11
11 2012-06-26, v1.0.0-RC1 12 2012-06-26, v1.0.0-RC1
12 ---------------------- 13 ----------------------
......
...@@ -250,6 +250,17 @@ if (!phantom || phantom.version.major !== 1 || phantom.version.minor < 5) { ...@@ -250,6 +250,17 @@ if (!phantom || phantom.version.major !== 1 || phantom.version.minor < 5) {
250 } else if (phantom.casperArgs.get(0) === "test") { 250 } else if (phantom.casperArgs.get(0) === "test") {
251 phantom.casperScript = fs.absolute(fs.pathJoin(phantom.casperPath, 'tests', 'run.js')); 251 phantom.casperScript = fs.absolute(fs.pathJoin(phantom.casperPath, 'tests', 'run.js'));
252 phantom.casperArgs.drop("test"); 252 phantom.casperArgs.drop("test");
253 } else if (phantom.casperArgs.get(0) === "selftest") {
254 phantom.casperScript = fs.absolute(fs.pathJoin(phantom.casperPath, 'tests', 'run.js'));
255 phantom.casperArgs.options.includes = fs.pathJoin(phantom.casperPath, 'tests', 'selftest.js');
256 if (phantom.casperArgs.args.length > 1) {
257 // we want a single test file
258 phantom.casperArgs.args.push(fs.pathJoin(phantom.casperPath, phantom.casperArgs.get(1)));
259 } else {
260 // run the whole casperjs test suite
261 phantom.casperArgs.args.push(fs.pathJoin(phantom.casperPath, 'tests', 'suites'));
262 }
263 phantom.casperArgs.drop("selftest");
253 } else if (phantom.casperArgs.args.length === 0 || !!phantom.casperArgs.options.help) { 264 } else if (phantom.casperArgs.args.length === 0 || !!phantom.casperArgs.options.help) {
254 var phantomVersion = [phantom.version.major, phantom.version.minor, phantom.version.patch].join('.'); 265 var phantomVersion = [phantom.version.major, phantom.version.minor, phantom.version.patch].join('.');
255 var f = require("utils").format; 266 var f = require("utils").format;
......
...@@ -709,6 +709,9 @@ var Tester = function Tester(casper, options) { ...@@ -709,6 +709,9 @@ var Tester = function Tester(casper, options) {
709 if (arguments.length === 0) { 709 if (arguments.length === 0) {
710 throw new CasperError("runSuites() needs at least one path argument"); 710 throw new CasperError("runSuites() needs at least one path argument");
711 } 711 }
712 this.includes.forEach(function(include) {
713 phantom.injectJs(include);
714 });
712 Array.prototype.forEach.call(arguments, function _forEach(path) { 715 Array.prototype.forEach.call(arguments, function _forEach(path) {
713 if (!fs.exists(path)) { 716 if (!fs.exists(path)) {
714 self.bar(f("Path %s doesn't exist", path), "RED_BAR"); 717 self.bar(f("Path %s doesn't exist", path), "RED_BAR");
...@@ -745,9 +748,6 @@ var Tester = function Tester(casper, options) { ...@@ -745,9 +748,6 @@ var Tester = function Tester(casper, options) {
745 this.runTest = function runTest(testFile) { 748 this.runTest = function runTest(testFile) {
746 this.bar(f('Test file: %s', testFile), 'INFO_BAR'); 749 this.bar(f('Test file: %s', testFile), 'INFO_BAR');
747 this.running = true; // this.running is set back to false with done() 750 this.running = true; // this.running is set back to false with done()
748 this.includes.forEach(function(include) {
749 phantom.injectJs(include);
750 });
751 this.exec(testFile); 751 this.exec(testFile);
752 }; 752 };
753 753
......
...@@ -38,14 +38,6 @@ function checkIncludeFile(include) { ...@@ -38,14 +38,6 @@ function checkIncludeFile(include) {
38 casper.options.verbose = casper.cli.get('direct') || false; 38 casper.options.verbose = casper.cli.get('direct') || false;
39 casper.options.logLevel = casper.cli.get('log-level') || "error"; 39 casper.options.logLevel = casper.cli.get('log-level') || "error";
40 40
41 // overriding Casper.open to prefix all test urls
42 casper.setFilter('open.location', function(location) {
43 if (!/^http/.test(location)) {
44 return f('file://%s/%s', fs.workingDirectory, location);
45 }
46 return location;
47 });
48
49 // test paths are passed as args 41 // test paths are passed as args
50 if (casper.cli.args.length) { 42 if (casper.cli.args.length) {
51 tests = casper.cli.args.filter(function(path) { 43 tests = casper.cli.args.filter(function(path) {
......
1 /**
2 * CasperJS local HTTP test server
3 */
4 var colorizer = require('colorizer').create('Colorizer');
5 var fs = require('fs');
6 var utils = require('utils');
7 var server = require('webserver').create();
8 var service;
9 var testServerPort = 54321;
10
11 function info(message) {
12 console.log(colorizer.colorize('INFO', 'INFO_BAR') + ' ' + message);
13 }
14
15 service = server.listen(testServerPort, function(request, response) {
16 var pageFile = fs.pathJoin(phantom.casperPath, request.url);
17 if (!fs.exists(pageFile) || !fs.isFile(pageFile)) {
18 response.statusCode = 404;
19 response.write("404 - NOT FOUND");
20 } else {
21 response.statusCode = 200;
22 response.write(fs.read(pageFile));
23 }
24 response.close();
25 });
26
27 // overriding Casper.open to prefix all test urls
28 casper.setFilter('open.location', function(location) {
29 if (/^file/.test(location)) {
30 return location;
31 }
32 if (!/^http/.test(location)) {
33 return f('http://localhost:%d/%s', testServerPort, location);
34 }
35 return location;
36 });
37
38 // test suites completion listener
39 casper.test.on('tests.complete', function() {
40 server.close();
41 });
1 var fs = require('fs'); 1 var fs = require('fs');
2 2
3 casper.start('tests/site/index.html', function() { 3 // FIXME: we're using local url scheme until https://github.com/ariya/phantomjs/pull/288 is
4 // possibly merged
5 casper.start('file://' + phantom.casperPath + '/tests/site/index.html', function() {
4 var imageUrl = 'file://' + phantom.casperPath + '/tests/site/images/phantom.png'; 6 var imageUrl = 'file://' + phantom.casperPath + '/tests/site/images/phantom.png';
5 var image = this.base64encode(imageUrl); 7 var image = this.base64encode(imageUrl);
6 8
......
1 /**
2 * Special test server to test for HTTP status codes
3 *
4 */
1 var server = require('webserver').create(); 5 var server = require('webserver').create();
2 var service = server.listen(8090, function (request, response) { 6 var service = server.listen(8090, function (request, response) {
3 var code = parseInt(/^\/(\d+)$/.exec(request.url)[1], 10); 7 var code = parseInt(/^\/(\d+)$/.exec(request.url)[1], 10);
......
...@@ -80,7 +80,7 @@ casper.then(function() { ...@@ -80,7 +80,7 @@ casper.then(function() {
80 80
81 t.comment('Tester.assertHttpStatus()'); 81 t.comment('Tester.assertHttpStatus()');
82 // using file:// protocol, HTTP status is always null 82 // using file:// protocol, HTTP status is always null
83 t.assertHttpStatus(null, 'Tester.assertHttpStatus() works as expected'); 83 t.assertHttpStatus(200, 'Tester.assertHttpStatus() works as expected');
84 84
85 t.comment('Tester.assertMatch()'); 85 t.comment('Tester.assertMatch()');
86 t.assertMatch("the lazy dog", /lazy/, 'Tester.assertMatch() works as expected'); 86 t.assertMatch("the lazy dog", /lazy/, 'Tester.assertMatch() works as expected');
......