Permalink
Browse files

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.
  • Loading branch information...
1 parent c87c459 commit a3e477cc11e9c2b76453c1d4b451e5654a877067 @n1k0 n1k0 committed Jul 14, 2012
Showing with 65 additions and 14 deletions.
  1. +1 −1 .travis.yml
  2. +1 −0 CHANGELOG.md
  3. +11 −0 bin/bootstrap.js
  4. +3 −3 modules/tester.js
  5. +0 −8 tests/run.js
  6. +41 −0 tests/selftest.js
  7. +3 −1 tests/suites/casper/encode.js
  8. +4 −0 tests/suites/http_status.js
  9. +1 −1 tests/suites/tester.js
View
@@ -6,4 +6,4 @@ before_script:
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
script:
- - "DISPLAY=:99.0 ./bin/casperjs test tests/suites"
+ - "DISPLAY=:99.0 ./bin/casperjs selftest"
View
@@ -7,6 +7,7 @@ XXXX-XX-XX, v1.0.0
- fixed [#178](https://github.com/n1k0/casperjs/issues/178) - added `Casper.getPageContent()` to access raw page body contents on non-html received content-types.
- 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)).
- fixed [#153](https://github.com/n1k0/casperjs/issues/153) - erroneous mouse event results when event.preventDefault() was used.
+- 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.
2012-06-26, v1.0.0-RC1
----------------------
View
@@ -250,6 +250,17 @@ if (!phantom || phantom.version.major !== 1 || phantom.version.minor < 5) {
} else if (phantom.casperArgs.get(0) === "test") {
phantom.casperScript = fs.absolute(fs.pathJoin(phantom.casperPath, 'tests', 'run.js'));
phantom.casperArgs.drop("test");
+ } else if (phantom.casperArgs.get(0) === "selftest") {
+ phantom.casperScript = fs.absolute(fs.pathJoin(phantom.casperPath, 'tests', 'run.js'));
+ phantom.casperArgs.options.includes = fs.pathJoin(phantom.casperPath, 'tests', 'selftest.js');
+ if (phantom.casperArgs.args.length > 1) {
+ // we want a single test file
+ phantom.casperArgs.args.push(fs.pathJoin(phantom.casperPath, phantom.casperArgs.get(1)));
+ } else {
+ // run the whole casperjs test suite
+ phantom.casperArgs.args.push(fs.pathJoin(phantom.casperPath, 'tests', 'suites'));
+ }
+ phantom.casperArgs.drop("selftest");
} else if (phantom.casperArgs.args.length === 0 || !!phantom.casperArgs.options.help) {
var phantomVersion = [phantom.version.major, phantom.version.minor, phantom.version.patch].join('.');
var f = require("utils").format;
View
@@ -709,6 +709,9 @@ var Tester = function Tester(casper, options) {
if (arguments.length === 0) {
throw new CasperError("runSuites() needs at least one path argument");
}
+ this.includes.forEach(function(include) {
+ phantom.injectJs(include);
+ });
Array.prototype.forEach.call(arguments, function _forEach(path) {
if (!fs.exists(path)) {
self.bar(f("Path %s doesn't exist", path), "RED_BAR");
@@ -745,9 +748,6 @@ var Tester = function Tester(casper, options) {
this.runTest = function runTest(testFile) {
this.bar(f('Test file: %s', testFile), 'INFO_BAR');
this.running = true; // this.running is set back to false with done()
- this.includes.forEach(function(include) {
- phantom.injectJs(include);
- });
this.exec(testFile);
};
View
@@ -38,14 +38,6 @@ function checkIncludeFile(include) {
casper.options.verbose = casper.cli.get('direct') || false;
casper.options.logLevel = casper.cli.get('log-level') || "error";
-// overriding Casper.open to prefix all test urls
-casper.setFilter('open.location', function(location) {
- if (!/^http/.test(location)) {
- return f('file://%s/%s', fs.workingDirectory, location);
- }
- return location;
-});
-
// test paths are passed as args
if (casper.cli.args.length) {
tests = casper.cli.args.filter(function(path) {
View
@@ -0,0 +1,41 @@
+/**
+ * CasperJS local HTTP test server
+ */
+var colorizer = require('colorizer').create('Colorizer');
+var fs = require('fs');
+var utils = require('utils');
+var server = require('webserver').create();
+var service;
+var testServerPort = 54321;
+
+function info(message) {
+ console.log(colorizer.colorize('INFO', 'INFO_BAR') + ' ' + message);
+}
+
+service = server.listen(testServerPort, function(request, response) {
+ var pageFile = fs.pathJoin(phantom.casperPath, request.url);
+ if (!fs.exists(pageFile) || !fs.isFile(pageFile)) {
+ response.statusCode = 404;
+ response.write("404 - NOT FOUND");
+ } else {
+ response.statusCode = 200;
+ response.write(fs.read(pageFile));
+ }
+ response.close();
+});
+
+// overriding Casper.open to prefix all test urls
+casper.setFilter('open.location', function(location) {
+ if (/^file/.test(location)) {
+ return location;
+ }
+ if (!/^http/.test(location)) {
+ return f('http://localhost:%d/%s', testServerPort, location);
+ }
+ return location;
+});
+
+// test suites completion listener
+casper.test.on('tests.complete', function() {
+ server.close();
+});
@@ -1,6 +1,8 @@
var fs = require('fs');
-casper.start('tests/site/index.html', function() {
+// FIXME: we're using local url scheme until https://github.com/ariya/phantomjs/pull/288 is
+// possibly merged
+casper.start('file://' + phantom.casperPath + '/tests/site/index.html', function() {
var imageUrl = 'file://' + phantom.casperPath + '/tests/site/images/phantom.png';
var image = this.base64encode(imageUrl);
@@ -1,3 +1,7 @@
+/**
+ * Special test server to test for HTTP status codes
+ *
+ */
var server = require('webserver').create();
var service = server.listen(8090, function (request, response) {
var code = parseInt(/^\/(\d+)$/.exec(request.url)[1], 10);
View
@@ -80,7 +80,7 @@ casper.then(function() {
t.comment('Tester.assertHttpStatus()');
// using file:// protocol, HTTP status is always null
- t.assertHttpStatus(null, 'Tester.assertHttpStatus() works as expected');
+ t.assertHttpStatus(200, 'Tester.assertHttpStatus() works as expected');
t.comment('Tester.assertMatch()');
t.assertMatch("the lazy dog", /lazy/, 'Tester.assertMatch() works as expected');

0 comments on commit a3e477c

Please sign in to comment.