Skip to content

Commit

Permalink
Merge pull request #218 from gemini-testing/fix/calibration
Browse files Browse the repository at this point in the history
fix: browser calibration
  • Loading branch information
eGavr authored Feb 7, 2018
2 parents 1f47c99 + 2391a28 commit e68d52e
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 201 deletions.
6 changes: 2 additions & 4 deletions lib/browser-pool/index.js
Original file line number Diff line number Diff line change
@@ -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),
Expand Down
7 changes: 0 additions & 7 deletions lib/browser/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -21,8 +20,6 @@ module.exports = class Browser {
originWindowSize: null
};

this._camera = Camera.create(this);

this._session = this._createSession();

this._addCommands();
Expand Down Expand Up @@ -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
}
Expand Down
47 changes: 47 additions & 0 deletions lib/browser/existing-browser.js
Original file line number Diff line number Diff line change
@@ -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);
}

Expand Down Expand Up @@ -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;
}
Expand Down
22 changes: 2 additions & 20 deletions lib/browser/new-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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(() => {
Expand Down
34 changes: 11 additions & 23 deletions lib/worker/browser-pool.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -15,40 +15,28 @@ module.exports = class BrowserPool {
this._emitter = emitter;

this._browsers = {};

this._calibrator = new Calibrator();
}

getBrowser(browserId, sessionId) {
this._browsers[browserId] = this._browsers[browserId] || [];

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();
}
};
7 changes: 5 additions & 2 deletions lib/worker/runner/mocha-runner/mocha-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions test/lib/browser-pool/index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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'});
});
Expand Down
Loading

0 comments on commit e68d52e

Please sign in to comment.