Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/commands/context/exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,15 @@ export async function setContext(this: AndroidDriver, name?: string | null): Pro
return;
}

const webviewsMapping = await getWebViewsMapping.bind(this)(this.opts);
const contexts = this.assignContexts(webviewsMapping);
// if the context we want doesn't exist, fail
if (!contexts?.includes(newContext)) {
throw new errors.NoSuchContextError();
let webviewsMapping: WebviewsMapping[] = [];
// the native context always exists, so do not wait for the webview scan to switch to it
if (newContext !== NATIVE_WIN) {
webviewsMapping = await getWebViewsMapping.bind(this)(this.opts);
const contexts = this.assignContexts(webviewsMapping);
// if the context we want doesn't exist, fail
if (!contexts?.includes(newContext)) {
throw new errors.NoSuchContextError();
}
}

await this.switchContext(newContext, webviewsMapping);
Expand Down
9 changes: 9 additions & 0 deletions test/unit/commands/context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ describe('Context', function () {
await mockContextExports(t, {});
await assert.rejects(driver.setContext('fake'), errors.NoSuchContextError);
});
it('should switch to native context without scanning webviews', async function (t) {
const getWebViewsMappingStub = sandbox.stub();
await mockContextExports(t, {getWebViewsMapping: getWebViewsMappingStub});
driver.curContext = 'WV';
await driver.setContext(NATIVE_WIN);
assert.strictEqual(getWebViewsMappingStub.notCalled, true);
assert.strictEqual((driver.switchContext as sinon.SinonStub).calledWithExactly(NATIVE_WIN, []), true);
assert.strictEqual(driver.curContext, NATIVE_WIN);
});
it('should not switch to context if already in it', async function (t) {
await mockContextExports(t, {});
driver.curContext = 'ANOTHER';
Expand Down
Loading