From 6cf0b3f2903bf1994df65cfc3c3588e003c33cb5 Mon Sep 17 00:00:00 2001 From: Daniel Wittner Date: Thu, 12 Mar 2015 12:48:15 +0100 Subject: [PATCH] waits now until headless browser is started before calling callback of run, needed for buster-ci --- lib/server-cli.js | 25 +++++++----- test/server-cli-test.js | 87 +++++++++++++++++++++++++++++------------ 2 files changed, 78 insertions(+), 34 deletions(-) diff --git a/lib/server-cli.js b/lib/server-cli.js index 5e379b3..65b6fbf 100644 --- a/lib/server-cli.js +++ b/lib/server-cli.js @@ -53,11 +53,14 @@ module.exports = ServerCli.prototype = { } var serverUrl = "http://localhost:" + port; - if(options['--capture-headless'].isSet) { - this.captureHeadlessBrowser(serverUrl); - } - callback(null, server); this.logger.log(this.binary + " running on " + serverUrl); + if (options['--capture-headless'].isSet) { + this.captureHeadlessBrowser(serverUrl, function () { + callback(null, server); + }.bind(this)); + } else { + callback(null, server); + } }.bind(this)); } catch (e) { this.handleError(e); @@ -82,15 +85,18 @@ module.exports = ServerCli.prototype = { return httpServer; }, - captureHeadlessBrowser: function(serverUrl) { + captureHeadlessBrowser: function (serverUrl, cb) { this.logger.log('Starting headless browser...'); - this.phantom.create(function(proxy) { - proxy.page.open(serverUrl + '/capture', function(success) { + this.phantom.create(function (proxy) { + proxy.page.open(serverUrl + '/capture', function (success) { if (success) { this.logger.log('Browser was captured.'); } else { - this.logger.log('Browser was not captured. Something went wrong :-(') + this.logger.log( + 'Browser was not captured. Something went wrong :-(' + ); } + cb(); }.bind(this)); }.bind(this)); }, @@ -109,7 +115,8 @@ module.exports = ServerCli.prototype = { }); this.cli.opt(['-c', '--capture-headless'], { - description: "Captures a headless webkit browser after the server was started." + description: "Captures a headless webkit browser " + + "after the server was started." }); }, diff --git a/test/server-cli-test.js b/test/server-cli-test.js index 26c2f7c..67e5a99 100644 --- a/test/server-cli-test.js +++ b/test/server-cli-test.js @@ -10,7 +10,7 @@ var assert = buster.assert; var refute = buster.refute; var serverCli = require("../lib/server-cli"); var testServer = require("../lib/middleware"); -var createServerFunc = function(port, binding, callback){ +var createServerFunc = function (port, binding, callback) { callback(null); }; @@ -45,7 +45,8 @@ buster.testCase("buster-server binary", { }, "starts server on default port": function (done) { - var createServer = this.stub(this.cli, "createServer", createServerFunc); + var createServer = this.stub(this.cli, "createServer", + createServerFunc); helper.run(this, [], done(function (err, server) { assert.calledOnce(createServer); assert.calledWith(createServer, 1111); @@ -53,7 +54,8 @@ buster.testCase("buster-server binary", { }, "starts server on specified port": function (done) { - var createServer = this.stub(this.cli, "createServer", createServerFunc); + var createServer = this.stub(this.cli, "createServer", + createServerFunc); helper.run(this, ["-p", "3200"], done(function () { assert.calledOnce(createServer); assert.calledWith(createServer, 3200); @@ -72,7 +74,7 @@ buster.testCase("buster-server binary", { "prints message if address is already in use (async)": function (done) { var error = new Error("EADDRINUSE, Address already in use"); var server = bane.createEventEmitter(); - server.listen = this.spy(function(port, binding, callback){ + server.listen = this.spy(function (port, binding, callback) { callback(error); }); this.stub(http, "createServer").returns(server); @@ -85,7 +87,8 @@ buster.testCase("buster-server binary", { }, "binds to specified address": function (done) { - var createServer = this.stub(this.cli, "createServer", createServerFunc); + var createServer = this.stub(this.cli, "createServer", + createServerFunc); helper.run(this, ["-b", "0.0.0.0"], done(function () { assert.calledOnce(createServer); assert.calledWith(createServer, 1111, "0.0.0.0"); @@ -93,40 +96,74 @@ buster.testCase("buster-server binary", { }, "binds to undefined when address not specified": function (done) { - var createServer = this.stub(this.cli, "createServer", createServerFunc); + var createServer = this.stub(this.cli, "createServer", + createServerFunc); helper.run(this, [], done(function () { assert.calledOnce(createServer); assert.calledWith(createServer, 1111, undefined); })); }, - "calls the function for capturing a headless browser if -c was passed": function(done) { - var createServer = this.stub(this.cli, "createServer", createServerFunc); - var captureHeadlessBrowser = this.stub(this.cli, "captureHeadlessBrowser"); + "captures headless browser if -c was passed": + function () { + var createServer = this.stub(this.cli, "createServer", + createServerFunc); + var captureHeadlessBrowser = this.stub(this.cli, + "captureHeadlessBrowser"); + + helper.run(this, ["-c"]); - helper.run(this, ["-c"], done(function () { assert.calledOnce(captureHeadlessBrowser); - assert.calledWithExactly(captureHeadlessBrowser, "http://localhost:1111"); - })); - }, + assert.calledWithExactly(captureHeadlessBrowser, + "http://localhost:1111", buster.sinon.match.func); + }, - "calls the function for capturing a headless browser if --capture-headless was passed": function(done) { - var createServer = this.stub(this.cli, "createServer", createServerFunc); - var captureHeadlessBrowser = this.stub(this.cli, "captureHeadlessBrowser"); + "captures headless browser if --capture-headless was passed": + function () { + var createServer = this.stub(this.cli, "createServer", + createServerFunc); + var captureHeadlessBrowser = this.stub(this.cli, + "captureHeadlessBrowser"); + + helper.run(this, ["--capture-headless"]); - helper.run(this, ["--capture-headless"], done(function () { assert.calledOnce(captureHeadlessBrowser); - assert.calledWithExactly(captureHeadlessBrowser, "http://localhost:1111"); - })); - }, + assert.calledWithExactly(captureHeadlessBrowser, + "http://localhost:1111", buster.sinon.match.func); + }, + + "creates phantom session if relevant parameter was passed": + function () { + var createServer = this.stub(this.cli, "createServer", + createServerFunc); + var createPhantom = this.stub(this.cli.phantom, "create"); - "creates a phantom session if relevant parameter was passed": function(done) { - var createServer = this.stub(this.cli, "createServer", createServerFunc); - var createPhantom = this.stub(this.cli.phantom, "create"); + helper.run(this, ["-c"]); - helper.run(this, ["-c"], done(function() { assert.calledOnce(createPhantom); - })); + }, + + "waits until capture page responses": function () { + var createServer = this.stub(this.cli, "createServer", + createServerFunc); + var responseCallback; + var createPhantom = this.stub(this.cli.phantom, "create", + function (cb) { + var proxy = { + page: { + open: function (url, cb) { + responseCallback = cb; + } + } + }; + cb(proxy); + }); + var cb = this.stub(); + + helper.run(this, ["-c"], cb); + refute.called(cb); + responseCallback(); + assert.calledOnce(cb); } },