diff --git a/js/runner_client/VenusClientLibrary.js b/js/runner_client/VenusClientLibrary.js index 7b7796b..d19c96f 100644 --- a/js/runner_client/VenusClientLibrary.js +++ b/js/runner_client/VenusClientLibrary.js @@ -56,10 +56,12 @@ VenusClientLibrary.prototype.done = function(results) { // decorate the test results with metadata results.userAgent = window.navigator.userAgent; - results.codeCoverageData = sandbox.contentWindow.__coverage__; + results.codeCoverageData = sandbox.contentWindow && sandbox.contentWindow.__coverage__; results.testId = window.venus.testId; - this.socket.emit('results', results); + if (this.socket) { + this.socket.emit('results', results); + } // append the test results to the document doneEl.id = 'test-done-marker'; @@ -67,8 +69,8 @@ VenusClientLibrary.prototype.done = function(results) { $(document).trigger('results', results); - if (window.VenusTestList) { - VenusTestList.postTestResults(results); + if (window.parent && window.parent.VenusTestList && window.parent.VenusTestList.postTestResults) { + window.parent.VenusTestList.postTestResults(results); } }; diff --git a/lib/uac/GhostDriverUac.js b/lib/uac/GhostDriverUac.js index 8658dbd..daae63e 100644 --- a/lib/uac/GhostDriverUac.js +++ b/lib/uac/GhostDriverUac.js @@ -21,6 +21,7 @@ var webdriver = require('selenium-webdriver'), log = require('../util/logger'), + portscanner = require('portscanner'), deferred = require('deferred'), fsHelper = require('../util/fsHelper'), spawn = require('child_process').spawn, @@ -31,21 +32,11 @@ var webdriver = require('selenium-webdriver'), * @param {Object} options */ function GhostDriverUac(options) { - this.host = options.host || 'http://localhast'; + this.host = options.host || 'localhost'; this.port = options.port || 8910; - this.server = [ - this.host || 'http://localhost', - ':', - this.port - ].join(''); - this.startAttempts = 0; - if (this.server.indexOf('http://') !== 0) { - this.server = 'http://' + this.server; - } - if (options && options.binaryPath) { this.binaryPath = fsHelper.getFirstValidPath(options.binaryPath); } @@ -67,11 +58,19 @@ GhostDriverUac.prototype.start = function () { // First, try to kill other phantomjs processes spawn('killall', ['-9', 'phantomjs']).on('exit', function () { // Now, go ahead and start a new phantomjs process - spawnPhantom.call(this); + portscanner.findAPortNotInUse(this.port, + this.port + 2000, + this.host, + function(err, port) { + this.port = port; + this.server = 'http://' + this.host + ':' + this.port; + log.debug('Trying to create PhontamJS Selenium server at', this.server); + spawnPhantom.call(this); + }.bind(this)); }.bind(this)); function spawnPhantom () { - this.process = spawn(binary, ['--webdriver=' + this.options.port]); + this.process = spawn(binary, ['--webdriver=' + this.port]); this.process.on('exit', function (code) { // Invalid phantomjs path diff --git a/test/unit/client/.venus/config b/test/unit/client/.venus/config index 1b5a8f8..fe5e38e 100644 --- a/test/unit/client/.venus/config +++ b/test/unit/client/.venus/config @@ -4,8 +4,12 @@ }, includes: { dom: [ - '../../../../js/jquery/jquery-1.10.0.min.js', '../../../../js/underscore/underscore-min.js' + ], + default: [ + '../../../../js/jquery/jquery-1.10.0.min.js', + 'mocks/venusTestList.js', + 'mocks/venus.js' ] } } \ No newline at end of file diff --git a/test/unit/client/.venus/mocks/venus.js b/test/unit/client/.venus/mocks/venus.js new file mode 100644 index 0000000..e36dc57 --- /dev/null +++ b/test/unit/client/.venus/mocks/venus.js @@ -0,0 +1,3 @@ +window.venus = window.venus || { + testId: 1 +}; diff --git a/test/unit/client/.venus/mocks/venusTestList.js b/test/unit/client/.venus/mocks/venusTestList.js new file mode 100644 index 0000000..d0df45b --- /dev/null +++ b/test/unit/client/.venus/mocks/venusTestList.js @@ -0,0 +1,3 @@ +window.parent.VenusTestList = window.parent.VenusTestList || { + postTestResults: function() {} +}; diff --git a/test/unit/client/runner_client/VenusClientLibrary.fixture.html b/test/unit/client/runner_client/VenusClientLibrary.fixture.html new file mode 100644 index 0000000..f20447e --- /dev/null +++ b/test/unit/client/runner_client/VenusClientLibrary.fixture.html @@ -0,0 +1 @@ +
diff --git a/test/unit/client/runner_client/VenusClientLibrary.spec.js b/test/unit/client/runner_client/VenusClientLibrary.spec.js index aa70021..5353c7b 100644 --- a/test/unit/client/runner_client/VenusClientLibrary.spec.js +++ b/test/unit/client/runner_client/VenusClientLibrary.spec.js @@ -1,17 +1,33 @@ /** + * @venus-fixture VenusClientLibrary.fixture.html * @venus-code runnerClient/VenusClientLibrary.js */ describe('VenusClientLibrary', function() { + function createInstance() { + return new VenusClientLibrary({ + host: 'www.example.com', + port: 1234 + }); + } + it('should expect on the window as VenusClientLibrary', function() { expect(window.VenusClientLibrary).to.be.ok(); }); it('should instantiate a new VenusClientLibrary object', function() { - var venusClientLibrary = new VenusClientLibrary({ - host: 'www.example.com', - port: 1234 - }); + expect(createInstance()).to.be.a(VenusClientLibrary); + }); + + describe('.done()', function() { + it('should execute VenusTestList.postTestReults from the parent context', sinon.test(function() { + var instance = createInstance(), + resultsStub = this.stub(window.parent.VenusTestList, 'postTestResults'), + results = { + band: 'Godspeed You! Black Emperor' + }; - expect(venusClientLibrary).to.be.a(VenusClientLibrary); + instance.done(results); + sinon.assert.calledWith(resultsStub, sinon.match(results)); + })); }); });