Skip to content

Commit

Permalink
fix: reuse session caps in worker from master
Browse files Browse the repository at this point in the history
It is necessary to correctly define available protocols of session inside worker
  • Loading branch information
DudaGod committed Apr 5, 2021
1 parent ead5f59 commit 5c70fe0
Show file tree
Hide file tree
Showing 13 changed files with 162 additions and 127 deletions.
12 changes: 8 additions & 4 deletions lib/browser/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ module.exports = class Browser {
this._addCommands();
}

async attach(sessionId) {
this._session = await this._attachSession(sessionId);
async attach(sessionId, sessionCaps) {
this._session = await this._attachSession(sessionId, sessionCaps);

this._addCommands();
}
Expand All @@ -59,12 +59,12 @@ module.exports = class Browser {
return webdriverio.remote(sessionOpts);
}

_attachSession(sessionId) {
_attachSession(sessionId, sessionCaps) {
const sessionOpts = this._getSessionOpts({isAttach: true});

// TODO: remove after - https://github.com/webdriverio/webdriverio/issues/6554
const detectedSessionEnvFlags = sessionEnvironmentDetector({
capabilities: sessionOpts.capabilities,
capabilities: sessionCaps,
requestedCapabilities: sessionOpts.capabilities
});

Expand Down Expand Up @@ -176,4 +176,8 @@ module.exports = class Browser {
get state() {
return this._state;
}

get capabilities() {
return this.publicAPI.capabilities;
}
};
4 changes: 2 additions & 2 deletions lib/browser/existing-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ module.exports = class ExistingBrowser extends Browser {
return this._config.baseUrl ? url.resolve(this._config.baseUrl, uri) : uri;
}

async init(sessionId, calibrator) {
await super.attach(sessionId);
async init({sessionId, sessionCaps} = {}, calibrator) {
await super.attach(sessionId, sessionCaps);

try {
this.config.prepareBrowser && this.config.prepareBrowser(this.publicAPI);
Expand Down
1 change: 1 addition & 0 deletions lib/runner/test-runner/regular-test-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ module.exports = class RegularTestRunner extends Runner {
browserId: this._browser.id,
browserVersion: this._browser.version,
sessionId: this._browser.sessionId,
sessionCaps: this._browser.capabilities,
file: this._test.file
}
);
Expand Down
9 changes: 7 additions & 2 deletions lib/worker/runner/browser-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ module.exports = class BrowserAgent {
this._pool = pool;
}

getBrowser(sessionId) {
return this._pool.getBrowser(this.browserId, this.browserVersion, sessionId);
getBrowser(sessionId, sessionCaps) {
return this._pool.getBrowser({
browserId: this.browserId,
browserVersion: this.browserVersion,
sessionId,
sessionCaps
});
}

freeBrowser(browser) {
Expand Down
4 changes: 2 additions & 2 deletions lib/worker/runner/browser-pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ module.exports = class BrowserPool {
this._calibrator = new Calibrator();
}

async getBrowser(browserId, browserVersion, sessionId) {
async getBrowser({browserId, browserVersion, sessionId, sessionCaps}) {
this._browsers[browserId] = this._browsers[browserId] || [];

let browser = _.find(this._browsers[browserId], (browser) => {
Expand All @@ -33,7 +33,7 @@ module.exports = class BrowserPool {
}

browser = Browser.create(this._config, browserId, browserVersion, this._emitter);
await browser.init(sessionId, this._calibrator);
await browser.init({sessionId, sessionCaps}, this._calibrator);

this._browsers[browserId].push(browser);

Expand Down
4 changes: 2 additions & 2 deletions lib/worker/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ module.exports = class Runner extends AsyncEmitter {
]);
}

runTest(fullTitle, {browserId, browserVersion, file, sessionId}) {
runTest(fullTitle, {browserId, browserVersion, file, sessionId, sessionCaps}) {
const tests = this._testParser.parse({file, browserId});
const test = tests.find((t) => t.fullTitle() === fullTitle);
const browserAgent = BrowserAgent.create(browserId, browserVersion, this._browserPool);
const runner = TestRunner.create(test, this._config.forBrowser(browserId), browserAgent);

return runner.run({sessionId});
return runner.run({sessionId, sessionCaps});
}
};
4 changes: 2 additions & 2 deletions lib/worker/runner/test-runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ module.exports = class TestRunner {
this._browserAgent = browserAgent;
}

async run({sessionId}) {
async run({sessionId, sessionCaps}) {
const test = this._test;
const hermioneCtx = test.hermioneCtx || {};

let browser;

try {
browser = await this._browserAgent.getBrowser(sessionId);
browser = await this._browserAgent.getBrowser(sessionId, sessionCaps);
} catch (e) {
throw Object.assign(e, {hermioneCtx});
}
Expand Down
34 changes: 18 additions & 16 deletions test/lib/browser/existing-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,16 @@ describe('ExistingBrowser', () => {
describe('init', () => {
it('should attach to browser with detected session environment flags', async () => {
const desiredCapabilities = {browserName: 'yabro'};
const detectedSessionEnvFlags = {isW3C: false, isChrome: false, isMobile: false};
const detectedSessionEnvFlags = {isW3C: false, isMobile: false};

await mkBrowser_({desiredCapabilities}).init();
await mkBrowser_({desiredCapabilities}).init({
sessionCaps: {
'goog:chromeOptions': {}
}
});

assert.calledOnce(webdriverio.attach);
assert.calledWithMatch(webdriverio.attach, detectedSessionEnvFlags);
assert.calledWithMatch(webdriverio.attach, {...detectedSessionEnvFlags, isChrome: true});
});

it('should attach to browser with session environment flags from config', async () => {
Expand Down Expand Up @@ -300,7 +304,7 @@ describe('ExistingBrowser', () => {
});

it('should attach a browser to a provided session', async () => {
const browser = await mkBrowser_().init('100-500');
const browser = await mkBrowser_().init({sessionId: '100-500'});

assert.equal(browser.sessionId, '100-500');
});
Expand Down Expand Up @@ -347,19 +351,19 @@ describe('ExistingBrowser', () => {
it('should perform calibration if `calibrate` is turn on', async () => {
calibrator.calibrate.withArgs(sinon.match.instanceOf(Browser)).resolves({foo: 'bar'});

await mkBrowser_({calibrate: true}).init(null, calibrator);
await mkBrowser_({calibrate: true}).init({}, calibrator);

assert.calledOnceWith(Camera.prototype.calibrate, {foo: 'bar'});
});

it('should not perform calibration if `calibrate` is turn off', async () => {
await mkBrowser_({calibrate: false}).init(null, calibrator);
await mkBrowser_({calibrate: false}).init({}, calibrator);

assert.notCalled(Camera.prototype.calibrate);
});

it('should perform calibration after attaching of a session id', async () => {
await mkBrowser_({calibrate: true}).init('100-500', calibrator);
it('should perform calibration after attaching of a session', async () => {
await mkBrowser_({calibrate: true}).init({sessionId: '100-500'}, calibrator);

const browser = calibrator.calibrate.lastCall.args[0];
assert.equal(browser.sessionId, '100-500');
Expand All @@ -370,7 +374,7 @@ describe('ExistingBrowser', () => {
const calibrator = sinon.createStubInstance(Calibrator);
calibrator.calibrate.resolves({foo: 'bar'});

const browser = await mkBrowser_({calibrate: true}).init(null, calibrator);
const browser = await mkBrowser_({calibrate: true}).init({}, calibrator);

assert.calledOnceWith(clientBridge.build, browser, {calibration: {foo: 'bar'}});
});
Expand Down Expand Up @@ -484,20 +488,18 @@ describe('ExistingBrowser', () => {
});
});

it('should extend options by calibration results', () => {
it('should extend options by calibration results', async () => {
const clientBridge = stubClientBridge_();
const calibrator = sinon.createStubInstance(Calibrator);
calibrator.calibrate.resolves({usePixelRatio: false});

const browser = mkBrowser_({calibrate: true});

return browser.init(null, calibrator)
.then(() => browser.prepareScreenshot())
.then(() => {
const opts = clientBridge.call.lastCall.args[1][1];
await browser.init({}, calibrator);
await browser.prepareScreenshot();

assert.propertyVal(opts, 'usePixelRatio', false);
});
const opts = clientBridge.call.lastCall.args[1][1];
assert.propertyVal(opts, 'usePixelRatio', false);
});

it('should use pixel ratio by default if calibration was not met', () => {
Expand Down
20 changes: 13 additions & 7 deletions test/lib/runner/test-runner/regular-test-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ describe('runner/test-runner/regular-test-runner', () => {
};

const stubBrowser_ = (opts = {}) => {
return {
id: opts.id || 'default-id',
state: opts.state || {isBroken: false},
sessionId: opts.sessionId || 'default-session-id',
return _.defaults(opts, {
id: 'default-id',
version: 'default-version',
capabilities: 'default-capabilities',
state: {isBroken: false},
sessionId: 'default-session-id',
applyState: sinon.stub().callsFake(function(state) {
this.state = state;
})
};
});
};

beforeEach(() => {
Expand All @@ -73,15 +75,19 @@ describe('runner/test-runner/regular-test-runner', () => {
it('should get browser before running test', async () => {
BrowserAgent.prototype.getBrowser.resolves(stubBrowser_({
id: 'bro',
sessionId: '100500'
version: '1.0',
sessionId: '100500',
capabilities: {browserName: 'bro'}
}));
const workers = mkWorkers_();

await run_({workers});

assert.calledOnceWith(workers.runTest, sinon.match.any, sinon.match({
browserId: 'bro',
sessionId: '100500'
browserVersion: '1.0',
sessionId: '100500',
sessionCaps: {browserName: 'bro'}
}));
});

Expand Down
28 changes: 22 additions & 6 deletions test/lib/worker/runner/browser-agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,31 @@ describe('worker/browser-agent', () => {

describe('getBrowser', () => {
it('should get a browser from the pool', () => {
browserPool.getBrowser.withArgs('bro-id', null, '100-500').returns({some: 'browser'});

assert.deepEqual(BrowserAgent.create('bro-id', null, browserPool).getBrowser('100-500'), {some: 'browser'});
browserPool.getBrowser.withArgs({
browserId: 'bro-id',
browserVersion: null,
sessionId: '100-500',
sessionCaps: 'some-caps'
}).returns({some: 'browser'});

assert.deepEqual(
BrowserAgent.create('bro-id', null, browserPool).getBrowser('100-500', 'some-caps'),
{some: 'browser'}
);
});

it('should get a browser with specific version from the pool', () => {
browserPool.getBrowser.withArgs('bro-id', '10.1', '100-500').returns({some: 'browser'});

assert.deepEqual(BrowserAgent.create('bro-id', '10.1', browserPool).getBrowser('100-500'), {some: 'browser'});
browserPool.getBrowser.withArgs({
browserId: 'bro-id',
browserVersion: '10.1',
sessionId: '100-500',
sessionCaps: 'some-caps'
}).returns({some: 'browser'});

assert.deepEqual(
BrowserAgent.create('bro-id', '10.1', browserPool).getBrowser('100-500', 'some-caps'),
{some: 'browser'}
);
});
});

Expand Down

0 comments on commit 5c70fe0

Please sign in to comment.