diff --git a/lib/commands/context/exports.ts b/lib/commands/context/exports.ts index ad50bac7..9043009f 100644 --- a/lib/commands/context/exports.ts +++ b/lib/commands/context/exports.ts @@ -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); diff --git a/test/unit/commands/context.spec.ts b/test/unit/commands/context.spec.ts index af3faa6a..676a221a 100644 --- a/test/unit/commands/context.spec.ts +++ b/test/unit/commands/context.spec.ts @@ -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';