From 2391a28b81f671b76d6a36497772b4a12df25488 Mon Sep 17 00:00:00 2001 From: egavr Date: Tue, 6 Feb 2018 11:49:23 +0300 Subject: [PATCH] fix: browser calibration --- lib/browser-pool/index.js | 6 +- lib/browser/browser.js | 7 - lib/browser/existing-browser.js | 47 +++++++ lib/browser/new-browser.js | 22 +--- lib/worker/browser-pool.js | 34 ++--- .../runner/mocha-runner/mocha-adapter.js | 7 +- package-lock.json | 8 +- package.json | 2 +- test/lib/browser-pool/index.js | 4 +- test/lib/browser/existing-browser.js | 113 ++++++++++++++++ test/lib/browser/new-browser.js | 74 ----------- test/lib/browser/utils.js | 2 +- test/lib/worker/browser-pool.js | 122 +++++++++--------- 13 files changed, 247 insertions(+), 201 deletions(-) diff --git a/lib/browser-pool/index.js b/lib/browser-pool/index.js index 8d02fd6af..e679bcc30 100644 --- a/lib/browser-pool/index.js +++ b/lib/browser-pool/index.js @@ -1,17 +1,15 @@ 'use strict'; -const {BrowserPool, Calibrator} = require('gemini-core'); +const {BrowserPool} = require('gemini-core'); const QBrowserPool = require('./q-browser-pool'); const Browser = require('../browser/new-browser'); const Events = require('../constants/runner-events'); exports.create = function(config, emitter) { - const calibrator = new Calibrator(); - const BrowserManager = { create: (id) => Browser.create(config, id), - start: (browser) => browser.init(calibrator), + start: (browser) => browser.init(), onStart: (browser) => emitSessionEvent(emitter, browser, Events.SESSION_START), onQuit: (browser) => emitSessionEvent(emitter, browser, Events.SESSION_END), diff --git a/lib/browser/browser.js b/lib/browser/browser.js index 46f35092b..c44431b41 100644 --- a/lib/browser/browser.js +++ b/lib/browser/browser.js @@ -4,7 +4,6 @@ const _ = require('lodash'); const q = require('q'); const URI = require('urijs'); const webdriverio = require('webdriverio'); -const Camera = require('./camera'); module.exports = class Browser { static create(config, id) { @@ -21,8 +20,6 @@ module.exports = class Browser { originWindowSize: null }; - this._camera = Camera.create(this); - this._session = this._createSession(); this._addCommands(); @@ -103,10 +100,6 @@ module.exports = class Browser { _.extend(this._changes, changes); } - captureViewportImage() { - return this._camera.captureViewportImage(); - } - get publicAPI() { return this._session; // exposing webdriver API as is } diff --git a/lib/browser/existing-browser.js b/lib/browser/existing-browser.js index 6ffa9e8eb..c264ea516 100644 --- a/lib/browser/existing-browser.js +++ b/lib/browser/existing-browser.js @@ -1,14 +1,19 @@ 'use strict'; +const Promise = require('bluebird'); const _ = require('lodash'); const url = require('url'); const Browser = require('./browser'); +const Camera = require('./camera'); const commandsList = require('./commands'); +const logger = require('../utils/logger'); module.exports = class ExistingBrowser extends Browser { constructor(config, id) { super(config, id); + this._camera = Camera.create(this); + this._meta = _.extend({}, this._config.meta); } @@ -44,6 +49,48 @@ module.exports = class ExistingBrowser extends Browser { return this._config.baseUrl ? url.resolve(this._config.baseUrl, uri) : uri; } + init(sessionId, calibrator) { + try { + this.config.prepareBrowser && this.config.prepareBrowser(this.publicAPI); + } catch (e) { + logger.warn(`WARN: couldn't prepare browser ${this.id}\n`, e.stack); + } + + return this.attach(sessionId) + .then(() => this._performCalibration(calibrator)); + } + + _performCalibration(calibrator) { + if (!this.config.calibrate || this._camera.isCalibrated()) { + return Promise.resolve(); + } + + return calibrator.calibrate(this) + .then((calibration) => this._camera.calibrate(calibration)); + } + + attach(sessionId) { + this.sessionId = sessionId; + + return Promise.resolve(this); + } + + quit() { + this.sessionId = null; + } + + open(url) { + return this._session.url(url); + } + + evalScript(script) { + return this._session.execute(`return ${script}`).then(({value}) => value); + } + + captureViewportImage() { + return this._camera.captureViewportImage(); + } + get meta() { return this._meta; } diff --git a/lib/browser/new-browser.js b/lib/browser/new-browser.js index 67132db69..6d3786dfa 100644 --- a/lib/browser/new-browser.js +++ b/lib/browser/new-browser.js @@ -25,16 +25,15 @@ module.exports = class NewBrowser extends Browser { }); } - init(calibrator) { + init() { return q(() => this._session) .call() .then(() => this._switchOffScreenshotOnReject()) .then(() => this._setHttpTimeout(this._config.sessionRequestTimeout)) .then(() => this._session.init()) - .then(() => this._setDefaultWindowSize()) - .then(() => this._performCalibration(calibrator)) .then(() => this._restoreHttpTimeout()) .then(() => this._switchOnScreenshotOnReject()) + .then(() => this._setDefaultWindowSize()) .thenResolve(this); } @@ -60,23 +59,6 @@ module.exports = class NewBrowser extends Browser { return windowSize ? this._session.windowHandleSize(windowSize) : q(); } - _performCalibration(calibrator) { - if (!this.config.calibrate || this._camera.isCalibrated()) { - return; - } - - return calibrator.calibrate(this) - .then((calibration) => this._camera.calibrate(calibration)); - } - - open(url) { - return this._session.url(url); - } - - evalScript(script) { - return this._session.execute(`return ${script}`); - } - reset() { return this._session .then(() => { diff --git a/lib/worker/browser-pool.js b/lib/worker/browser-pool.js index cfc570a98..486daabe7 100644 --- a/lib/worker/browser-pool.js +++ b/lib/worker/browser-pool.js @@ -1,9 +1,9 @@ 'use strict'; +const {Calibrator} = require('gemini-core'); const _ = require('lodash'); const Browser = require('../browser/existing-browser'); const RunnerEvents = require('./constants/runner-events'); -const logger = require('../utils/logger'); module.exports = class BrowserPool { static create(config, emitter) { @@ -15,6 +15,8 @@ module.exports = class BrowserPool { this._emitter = emitter; this._browsers = {}; + + this._calibrator = new Calibrator(); } getBrowser(browserId, sessionId) { @@ -22,33 +24,19 @@ module.exports = class BrowserPool { let browser = _.find(this._browsers[browserId], (browser) => !browser.sessionId); - if (!browser) { - browser = Browser.create(this._config, browserId); - this._browsers[browserId].push(browser); - - try { - this._initBrowser(browser); - } catch (e) { - logger.warn(`WARN: couldn't intialize browser ${browserId}\n`, e.stack); - } + if (browser) { + return browser.attach(sessionId); } - browser.sessionId = sessionId; - browser.updateChanges({currentWindowSize: null}); - - return browser; - } - - _initBrowser(browser) { - const config = this._config.forBrowser(browser.id); - if (config.prepareBrowser) { - config.prepareBrowser(browser.publicAPI); - } + browser = Browser.create(this._config, browserId); + this._browsers[browserId].push(browser); - this._emitter.emit(RunnerEvents.NEW_BROWSER, browser.publicAPI, {browserId: browser.id}); + return browser.init(sessionId, this._calibrator) + .then(() => this._emitter.emit(RunnerEvents.NEW_BROWSER, browser.publicAPI, {browserId: browser.id})) + .then(() => browser); } freeBrowser(browser) { - browser.sessionId = null; + browser.quit(); } }; diff --git a/lib/worker/runner/mocha-runner/mocha-adapter.js b/lib/worker/runner/mocha-runner/mocha-adapter.js index 7669b9fe5..4d53be2f3 100644 --- a/lib/worker/runner/mocha-runner/mocha-adapter.js +++ b/lib/worker/runner/mocha-runner/mocha-adapter.js @@ -83,9 +83,12 @@ module.exports = class MochaAdapter extends EventEmitter { } _requestBrowser() { - this._browser = this._browserAgent.getBrowser(this._sessionId); + return this._browserAgent.getBrowser(this._sessionId) + .then((browser) => { + this._browser = browser; - this.suite.ctx.browser = this._browser.publicAPI; + this.suite.ctx.browser = this._browser.publicAPI; + }); } _freeBrowser() { diff --git a/package-lock.json b/package-lock.json index 821d32b02..865f77ddb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "hermione", - "version": "0.57.0", + "version": "0.58.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -1881,9 +1881,9 @@ } }, "gemini-core": { - "version": "2.5.0", - "resolved": "https://registry.npmjs.org/gemini-core/-/gemini-core-2.5.0.tgz", - "integrity": "sha512-feYKv9R5easfh8MkUaoY/XGZChJEiqbNPXb+spvXQQIGV29BXrYNWJrY3JNyuDe5z1RI2wDUz1pwsYegr3YXVg==", + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/gemini-core/-/gemini-core-2.5.1.tgz", + "integrity": "sha512-BXkjRzfAY+yxllw1sG2GzIjzgMLfnKp7VkL8+qTEsVKn14b+EvONfYpDEC7YfpUzzPBze0GjjZ4g73tTzeimVg==", "requires": { "bluebird": "3.5.1", "debug": "2.6.9", diff --git a/package.json b/package.json index 622def713..1a844f07d 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "commander": "^2.12.2", "fs-extra": "^5.0.0", "gemini-configparser": "^1.0.0", - "gemini-core": "^2.5.0", + "gemini-core": "^2.5.1", "glob-extra": "^3.0.0", "inherit": "^2.2.2", "lodash": "^4.17.4", diff --git a/test/lib/browser-pool/index.js b/test/lib/browser-pool/index.js index 9e6798445..84e0eba26 100644 --- a/test/lib/browser-pool/index.js +++ b/test/lib/browser-pool/index.js @@ -1,6 +1,6 @@ 'use strict'; -const {BrowserPool: CoreBrowserPool, Calibrator} = require('gemini-core'); +const {BrowserPool: CoreBrowserPool} = require('gemini-core'); const _ = require('lodash'); const q = require('q'); const AsyncEmitter = require('gemini-core').events.AsyncEmitter; @@ -59,7 +59,7 @@ describe('browser-pool', () => { BrowserPool.create(); const browser = stubBrowser(); - browser.init.withArgs(sinon.match.instanceOf(Calibrator)).returns(q({session: 'id'})); + browser.init.returns(q({session: 'id'})); assert.becomes(getBrowserManager().start(browser), {session: 'id'}); }); diff --git a/test/lib/browser/existing-browser.js b/test/lib/browser/existing-browser.js index b3b2c7a03..270dfdd1b 100644 --- a/test/lib/browser/existing-browser.js +++ b/test/lib/browser/existing-browser.js @@ -1,6 +1,10 @@ 'use strict'; +const {Calibrator} = require('gemini-core'); const webdriverio = require('webdriverio'); +const Camera = require('lib/browser/camera'); +const Browser = require('lib/browser/existing-browser'); +const logger = require('lib/utils/logger'); const {mkExistingBrowser_: mkBrowser_, mkSessionStub_} = require('./utils'); describe('NewBrowser', () => { @@ -11,6 +15,7 @@ describe('NewBrowser', () => { session = mkSessionStub_(sandbox); sandbox.stub(webdriverio, 'remote'); webdriverio.remote.returns(session); + sandbox.stub(logger, 'warn'); }); afterEach(() => sandbox.restore()); @@ -117,4 +122,112 @@ describe('NewBrowser', () => { assert.calledWith(session.addCommand, 'assertView'); }); }); + + describe('init', () => { + it('should call prepareBrowser on new browser', () => { + const prepareBrowser = sandbox.stub(); + + return mkBrowser_({prepareBrowser}) + .init() + .then(() => assert.calledOnceWith(prepareBrowser, session)); + }); + + it('should not fail on error in prepareBrowser', () => { + const prepareBrowser = sandbox.stub().throws(); + + return mkBrowser_({prepareBrowser}) + .init() + .then(() => assert.calledOnce(logger.warn)); + }); + + it('should attach a browser to a provided session', () => { + const browser = mkBrowser_(); + + return browser.init('100-500') + .then(() => assert.equal(browser.sessionId, '100-500')); + }); + + describe('camera calibration', () => { + let calibrator; + + beforeEach(() => { + calibrator = sinon.createStubInstance(Calibrator); + + calibrator.calibrate.resolves(); + + sandbox.stub(Camera.prototype, 'calibrate'); + sandbox.stub(Camera.prototype, 'isCalibrated'); + }); + + it('should perform calibration if `calibrate` is turn on', () => { + calibrator.calibrate.withArgs(sinon.match.instanceOf(Browser)).resolves({foo: 'bar'}); + + return mkBrowser_({calibrate: true}) + .init(null, calibrator) + .then(() => assert.calledOnceWith(Camera.prototype.calibrate, {foo: 'bar'})); + }); + + it('should not perform calibration if `calibrate` is turn off', () => { + return mkBrowser_({calibrate: false}) + .init(null, calibrator) + .then(() => { + assert.notCalled(Camera.prototype.calibrate); + }); + }); + + it('should not perform calibration if camera is already calibrated', () => { + Camera.prototype.isCalibrated.returns(true); + + return mkBrowser_({calibrate: true}) + .init(null, calibrator) + .then(() => { + assert.notCalled(Camera.prototype.calibrate); + }); + }); + + it('should perform calibration after attaching of a session id', () => { + sandbox.spy(Browser.prototype, 'attach'); + + return mkBrowser_({calibrate: true}) + .init(null, calibrator) + .then(() => assert.callOrder(Browser.prototype.attach, calibrator.calibrate)); + }); + }); + }); + + describe('open', () => { + it('should open URL', () => { + return mkBrowser_().open('some-url') + .then(() => assert.calledOnceWith(session.url, 'some-url')); + }); + }); + + describe('evalScript', () => { + it('should execute script with added `return` operator', () => { + return mkBrowser_().evalScript('some-script') + .then(() => assert.calledOnceWith(session.execute, 'return some-script')); + }); + + it('should return the value of the executed script', () => { + session.execute.resolves({value: {foo: 'bar'}}); + + return assert.becomes(mkBrowser_().evalScript('some-script'), {foo: 'bar'}); + }); + }); + + describe('captureViewportImage', () => { + beforeEach(() => { + sandbox.stub(Camera.prototype, 'captureViewportImage'); + }); + + it('should delegate actual capturing to camera object', () => { + Camera.prototype.captureViewportImage.resolves({some: 'image'}); + + return mkBrowser_().captureViewportImage() + .then((image) => { + assert.calledOnce(Camera.prototype.captureViewportImage); + assert.deepEqual(image, {some: 'image'}); + }); + }); + }); }); diff --git a/test/lib/browser/new-browser.js b/test/lib/browser/new-browser.js index d465461eb..e9f220af3 100644 --- a/test/lib/browser/new-browser.js +++ b/test/lib/browser/new-browser.js @@ -2,21 +2,16 @@ const q = require('q'); const webdriverio = require('webdriverio'); -const {Calibrator} = require('gemini-core'); -const Browser = require('lib/browser/new-browser'); const logger = require('lib/utils/logger'); const signalHandler = require('lib/signal-handler'); -const Camera = require('lib/browser/camera'); const {mkNewBrowser_: mkBrowser_, mkSessionStub_} = require('./utils'); describe('NewBrowser', () => { const sandbox = sinon.sandbox.create(); let session; - let calibrator; beforeEach(() => { session = mkSessionStub_(sandbox); - calibrator = sinon.createStubInstance(Calibrator); sandbox.stub(webdriverio, 'remote'); sandbox.stub(logger); webdriverio.remote.returns(session); @@ -224,75 +219,6 @@ describe('NewBrowser', () => { .init() .then(() => assert.propertyVal(session.requestHandler.defaultOptions, 'screenshotOnReject', true)); }); - - describe('camera calibration', () => { - beforeEach(() => { - sandbox.stub(Camera.prototype, 'calibrate'); - sandbox.stub(Camera.prototype, 'isCalibrated'); - }); - - it('should perform calibration if `calibrate` is turn on', () => { - calibrator.calibrate.withArgs(sinon.match.instanceOf(Browser)).resolves({foo: 'bar'}); - - return mkBrowser_({calibrate: true}) - .init(calibrator) - .then(() => assert.calledOnceWith(Camera.prototype.calibrate, {foo: 'bar'})); - }); - - it('should not perform calibration if `calibrate` is turn off', () => { - return mkBrowser_({calibrate: false}) - .init(calibrator) - .then(() => { - assert.notCalled(Camera.prototype.calibrate); - }); - }); - - it('should not perform calibration if camera is already calibrated', () => { - Camera.prototype.isCalibrated.returns(true); - - return mkBrowser_({calibrate: true}) - .init(calibrator) - .then(() => { - assert.notCalled(Camera.prototype.calibrate); - }); - }); - }); - }); - - describe('open', () => { - it('should open URL', () => { - return mkBrowser_() - .init() - .then((browser) => browser.open('some-url')) - .then(() => assert.calledOnceWith(session.url, 'some-url')); - }); - }); - - describe('evalScript', () => { - it('should execute script with added `return` operator', () => { - return mkBrowser_() - .init() - .then((browser) => browser.evalScript('some-script')) - .then(() => assert.calledOnceWith(session.execute, 'return some-script')); - }); - }); - - describe('captureViewportImage', () => { - beforeEach(() => { - sandbox.stub(Camera.prototype, 'captureViewportImage'); - }); - - it('should delegate actual capturing to camera object', () => { - Camera.prototype.captureViewportImage.resolves({some: 'image'}); - - return mkBrowser_() - .init() - .then((browser) => browser.captureViewportImage()) - .then((image) => { - assert.calledOnce(Camera.prototype.captureViewportImage); - assert.deepEqual(image, {some: 'image'}); - }); - }); }); describe('reset', () => { diff --git a/test/lib/browser/utils.js b/test/lib/browser/utils.js index dc98a5d31..ab3dc7485 100644 --- a/test/lib/browser/utils.js +++ b/test/lib/browser/utils.js @@ -42,7 +42,7 @@ exports.mkSessionStub_ = (sandbox) => { session.init = sandbox.stub().named('init').returns(session); session.end = sandbox.stub().named('end').resolves(); session.url = sandbox.stub().named('url').returns(session); - session.execute = sandbox.stub().named('execute').returns(session); + session.execute = sandbox.stub().named('execute').resolves({}); session.windowHandleSize = sandbox.stub().named('windowHandleSize').resolves({value: {}}); session.requestHandler = {defaultOptions: {}}; diff --git a/test/lib/worker/browser-pool.js b/test/lib/worker/browser-pool.js index a5562168d..abb5e6e14 100644 --- a/test/lib/worker/browser-pool.js +++ b/test/lib/worker/browser-pool.js @@ -1,6 +1,7 @@ 'use strict'; const EventEmitter = require('events').EventEmitter; +const {Calibrator} = require('gemini-core'); const _ = require('lodash'); const Browser = require('lib/browser/existing-browser'); const BrowserPool = require('lib/worker/browser-pool'); @@ -26,60 +27,45 @@ describe('worker/browser-pool', () => { }; const stubBrowser = (opts) => { - return _.defaults(opts || {}, { + const bro = _.defaults(opts || {}, { updateChanges: () => {} }); + + bro.init = sandbox.stub().resolves(); + bro.attach = sandbox.stub().resolves(bro); + bro.quit = sandbox.stub(); + + return bro; }; beforeEach(() => { sandbox.stub(logger, 'warn'); + + sandbox.stub(Browser, 'create').returns({ + updateChanges: () => {} + }); }); afterEach(() => sandbox.restore()); describe('getBrowser', () => { - beforeEach(() => { - sandbox.stub(Browser, 'create').returns({ - updateChanges: () => {} - }); - }); - it('should create a new browser if there are no free browsers in a cache', () => { const config = stubConfig(); const browserPool = createPool({config}); + const browser = stubBrowser({browserId: 'bro-id'}); - Browser.create.withArgs(config, 'bro-id').returns(stubBrowser({browserId: 'bro-id'})); - - const browser = browserPool.getBrowser('bro-id', '100-500'); - - assert.propertyVal(browser, 'browserId', 'bro-id'); - assert.propertyVal(browser, 'sessionId', '100-500'); - }); - - it('should call prepareBrowser on new browser', () => { - const prepareBrowser = sinon.stub(); - const config = stubConfig({prepareBrowser}); - const browserPool = createPool({config}); - const bro = stubBrowser({publicAPI: {some: 'api'}}); - - Browser.create.returns(bro); + Browser.create.withArgs(config, 'bro-id').returns(browser); - browserPool.getBrowser(); - - assert.calledOnceWith(prepareBrowser, {some: 'api'}); + return assert.becomes(browserPool.getBrowser('bro-id'), browser); }); - it('should not fail on error in prepareBrowser', () => { - const config = stubConfig({prepareBrowser: sinon.stub().throws()}); - const browserPool = createPool({config}); - const bro = stubBrowser({publicAPI: {foo: 'bar'}}); - - Browser.create.returns(bro); + it('should init a new created browser if there are no free browsers in a cache', () => { + const browser = stubBrowser({browserId: 'bro-id'}); - const browser = browserPool.getBrowser(); + Browser.create.returns(browser); - assert.equal(browser, bro); - assert.calledOnce(logger.warn); + return createPool().getBrowser('bro-id', '100-500') + .then(() => assert.calledOnceWith(browser.init, '100-500', sinon.match.instanceOf(Calibrator))); }); it('should emit "NEW_BROWSER" event on creating of a browser', () => { @@ -91,37 +77,40 @@ describe('worker/browser-pool', () => { Browser.create.returns(stubBrowser({id: 'bro-id', publicAPI: {some: 'api'}})); - browserPool.getBrowser(); - - assert.calledOnceWith(onNewBrowser, {some: 'api'}, {browserId: 'bro-id'}); + return browserPool.getBrowser() + .then(() => assert.calledOnceWith(onNewBrowser, {some: 'api'}, {browserId: 'bro-id'})); }); - it('should not fail on error in "NEW_BROWSER" handler', () => { - const emitter = new EventEmitter(); - const browserPool = createPool({emitter}); - - emitter.on(RunnerEvents.NEW_BROWSER, sinon.stub().throws()); + it('should not create a new browser if there is a free browser in a cache', () => { + const browserPool = createPool(); - const bro = stubBrowser({id: 'bro-id', publicAPI: {some: 'api'}}); - Browser.create.returns(bro); + Browser.create.returns(stubBrowser()); - const browser = browserPool.getBrowser(); + return browserPool.getBrowser('bro-id', '100-500') + .then((browser) => { + browserPool.freeBrowser(browser); + Browser.create.resetHistory(); - assert.equal(browser, bro); - assert.calledOnce(logger.warn); + return browserPool.getBrowser('bro-id', '500-100') + .then((anotherBrowser) => { + assert.deepEqual(browser, anotherBrowser); + assert.notCalled(Browser.create); + }); + }); }); - it('should not create a new browser if there is a free browser in a cache', () => { + it('should attach a given session to a free browser in a cache', () => { const browserPool = createPool(); - const browser = browserPool.getBrowser('bro-id', '100-500'); - browserPool.freeBrowser(browser); + Browser.create.returns(stubBrowser()); - Browser.create.resetHistory(); + return browserPool.getBrowser('bro-id', '100-500') + .then((browser) => { + browserPool.freeBrowser(browser); - assert.deepEqual(browserPool.getBrowser('bro-id', '500-100'), browser); - assert.equal(browser.sessionId, '500-100'); - assert.notCalled(Browser.create); + return browserPool.getBrowser('bro-id', '500-100') + .then((anotherBrowser) => assert.calledOnceWith(anotherBrowser.attach, '500-100')); + }); }); it('should not emit "NEW_BROWSER" event on getting of a free browser from a cache', () => { @@ -129,27 +118,34 @@ describe('worker/browser-pool', () => { const onNewBrowser = sandbox.spy().named('onNewBrowser'); const browserPool = createPool({emitter}); - emitter.on(RunnerEvents.NEW_BROWSER, onNewBrowser); + Browser.create.returns(stubBrowser()); - const browser = browserPool.getBrowser('bro-id', '100-500'); - browserPool.freeBrowser(browser); + emitter.on(RunnerEvents.NEW_BROWSER, onNewBrowser); - onNewBrowser.reset(); + return browserPool.getBrowser('bro-id') + .then((browser) => { + browserPool.freeBrowser(browser); - browserPool.getBrowser('bro-id'); + onNewBrowser.reset(); - assert.notCalled(onNewBrowser); + return browserPool.getBrowser('bro-id') + .then(() => assert.notCalled(onNewBrowser)); + }); }); }); describe('freeBrowser', () => { it('should set session id to "null"', () => { const browserPool = createPool(); - const browser = {sessionId: '100-500'}; - browserPool.freeBrowser(browser); + Browser.create.returns(stubBrowser()); + + return browserPool.getBrowser('bro-id', '100-500') + .then((browser) => { + browserPool.freeBrowser(browser); - assert.isNull(browser.sessionId); + assert.calledOnce(browser.quit); + }); }); }); });