Skip to content

Commit 88fc935

Browse files
committed
[js] Ensure callback for session teardown is called if an error cocurs while
creating the session.
1 parent f76d614 commit 88fc935

2 files changed

Lines changed: 54 additions & 1 deletion

File tree

javascript/node/selenium-webdriver/lib/webdriver.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,11 @@ class WebDriver {
776776
let session = flow.execute(
777777
() => executeCommand(executor, cmd),
778778
'WebDriver.createSession()');
779+
if (typeof opt_onQuit === 'function') {
780+
session = session.catch(err => {
781+
return Promise.resolve(opt_onQuit()).then(_ => {throw err});
782+
});
783+
}
779784
const ctor = opt_ctor || WebDriver;
780785
return new ctor(session, executor, flow, opt_onQuit);
781786
}

javascript/node/selenium-webdriver/test/lib/webdriver_test.js

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ describe('WebDriver', function() {
363363
return driver.getSession().then(v => assert.strictEqual(v, aSession));
364364
});
365365

366-
it('handles desired and requried capabilities', function() {
366+
it('handles desired and required capabilities', function() {
367367
let aSession = new Session(SESSION_ID, {'browserName': 'firefox'});
368368
let executor = new FakeExecutor().
369369
expect(CName.NEW_SESSION).
@@ -392,6 +392,23 @@ describe('WebDriver', function() {
392392
return driver.getSession().then(fail, assertIsStubError);
393393
});
394394

395+
it('invokes quit callback if it fails to create a session', function() {
396+
let called = false;
397+
let executor = new FakeExecutor()
398+
.expect(CName.NEW_SESSION)
399+
.withParameters({'desiredCapabilities': {'browserName': 'firefox'}})
400+
.andReturnError(new StubError())
401+
.end();
402+
403+
var driver =
404+
WebDriver.createSession(executor, {'browserName': 'firefox'},
405+
null, null, () => called = true);
406+
return driver.getSession().then(fail, err => {
407+
assert.ok(called);
408+
assertIsStubError(err);
409+
});
410+
});
411+
395412
it('usesActiveFlowByDefault', function() {
396413
let executor = new FakeExecutor().
397414
expect(CName.NEW_SESSION).
@@ -420,6 +437,37 @@ describe('WebDriver', function() {
420437

421438
return waitForIdle(otherFlow);
422439
});
440+
441+
describe('creation failures bubble up in control flow', function() {
442+
function runTest(...args) {
443+
let executor = new FakeExecutor()
444+
.expect(CName.NEW_SESSION)
445+
.withParameters({'desiredCapabilities': {'browserName': 'firefox'}})
446+
.andReturnError(new StubError())
447+
.end();
448+
449+
WebDriver.createSession(
450+
executor, {'browserName': 'firefox'}, ...args);
451+
return waitForAbort().then(assertIsStubError);
452+
}
453+
454+
it('no onQuit callback', () => runTest());
455+
it('has onQuit callback', () => runTest(null, null, function() {}));
456+
457+
it('onQuit callback failure suppress creation failure', function() {
458+
let e = new Error('hi!');
459+
let executor = new FakeExecutor()
460+
.expect(CName.NEW_SESSION)
461+
.withParameters({'desiredCapabilities': {'browserName': 'firefox'}})
462+
.andReturnError(new StubError())
463+
.end();
464+
465+
WebDriver.createSession(
466+
executor, {'browserName': 'firefox'}, null, null,
467+
() => {throw e});
468+
return waitForAbort().then(err => assert.strictEqual(err, e));
469+
});
470+
});
423471
});
424472
});
425473

0 commit comments

Comments
 (0)