From dcb4d1969b3f5d007eb1e566a4046da66bfeadfe Mon Sep 17 00:00:00 2001 From: Daniel Wittner Date: Mon, 16 Mar 2015 13:41:05 +0100 Subject: [PATCH] additional param sessionId for captureHeadlessBrowser --- lib/server-cli.js | 18 +++++++++++++----- test/server-cli-test.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 5 deletions(-) diff --git a/lib/server-cli.js b/lib/server-cli.js index 65b6fbf..cd2e3e9 100644 --- a/lib/server-cli.js +++ b/lib/server-cli.js @@ -85,18 +85,26 @@ module.exports = ServerCli.prototype = { return httpServer; }, - captureHeadlessBrowser: function (serverUrl, cb) { + captureHeadlessBrowser: function (serverUrl, sessionId, cb) { this.logger.log('Starting headless browser...'); + if (cb === undefined && typeof sessionId === 'function') { + cb = sessionId; + sessionId = undefined; + } + var url = serverUrl + '/capture'; + if (sessionId !== undefined) { + url += '?id=' + sessionId; + } this.phantom.create(function (proxy) { - proxy.page.open(serverUrl + '/capture', function (success) { + proxy.page.open(url, function (success) { if (success) { - this.logger.log('Browser was captured.'); + this.logger.log('Headless browser was captured.'); } else { this.logger.log( - 'Browser was not captured. Something went wrong :-(' + 'Headless browser was not captured. Something went wrong :-(' ); } - cb(); + cb && cb(); }.bind(this)); }.bind(this)); }, diff --git a/test/server-cli-test.js b/test/server-cli-test.js index 67e5a99..bea00b9 100644 --- a/test/server-cli-test.js +++ b/test/server-cli-test.js @@ -244,5 +244,44 @@ buster.testCase("buster-server binary", { }); }.bind(this)); } + }, + + "captureHeadlessBrowser": { + + setUp: function () { + this.openStub = this.stub(); + this.stub(this.cli.phantom, "create", + function (cb) { + var proxy = { + page: { + open: this.openStub + } + }; + cb(proxy); + }.bind(this)); + + }, + + "ignores missing callback": function () { + refute.exception( + this.cli.captureHeadlessBrowser.bind( + this.cli, "http://localhost:1111" + ) + ); + }, + + "passes sessionId to capture page": function () { + + this.cli.captureHeadlessBrowser("http://localhost:1111", 0); + + assert.calledOnceWith(this.openStub, "http://localhost:1111/capture?id=0"); + }, + + "doesn't pass sessionId if not defined": function () { + + this.cli.captureHeadlessBrowser("http://localhost:1111", function () {}); + + assert.calledOnceWith(this.openStub, "http://localhost:1111/capture"); + } } });