diff --git a/lib/commands/context.ts b/lib/commands/context.ts new file mode 100644 index 0000000..3c42ec9 --- /dev/null +++ b/lib/commands/context.ts @@ -0,0 +1,19 @@ +import { errors } from 'appium/driver'; + +const WINDOWS_CONTEXT = 'NATIVE_APP'; + +export async function getContexts (): Promise { + return [WINDOWS_CONTEXT]; +} + +export async function getCurrentContext (): Promise { + return WINDOWS_CONTEXT; +} + +export async function setContext (context: string): Promise { + if (context !== WINDOWS_CONTEXT) { + throw new errors.NoSuchContextError( + `The Windows Driver only supports '${WINDOWS_CONTEXT}' context.` + ); + } +} \ No newline at end of file diff --git a/lib/driver.js b/lib/driver.js index 722949f..622cfad 100644 --- a/lib/driver.js +++ b/lib/driver.js @@ -13,6 +13,7 @@ import * as gestureCommands from './commands/gestures'; import * as powershellCommands from './commands/powershell'; import * as recordScreenCommands from './commands/record-screen'; import * as touchCommands from './commands/touch'; +import * as contextCommands from './commands/context'; import { POWER_SHELL_FEATURE } from './constants'; import { newMethodMap } from './method-map'; import { executeMethodMap } from './execute-method-map'; @@ -29,6 +30,8 @@ const NO_PROXY = [ ['POST', new RegExp('^/session/[^/]+/appium/device/pull_file')], ['POST', new RegExp('^/session/[^/]+/appium/device/pull_folder')], ['GET', new RegExp('^/session/[^/]+/screenshot')], + ['GET', new RegExp('^/session/[^/]+/contexts?')], + ['POST', new RegExp('^/session/[^/]+/context')], // Workarounds for // - https://github.com/appium/appium/issues/15923 // - https://github.com/appium/appium/issues/16316 @@ -205,6 +208,10 @@ export class WindowsDriver extends BaseDriver { stopRecordingScreen = recordScreenCommands.stopRecordingScreen; performActions = touchCommands.performActions; + + getContexts = contextCommands.getContexts; + getCurrentContext = contextCommands.getCurrentContext; + setContext = contextCommands.setContext; } export default WindowsDriver; diff --git a/test/e2e/commands/context-e2e-specs.js b/test/e2e/commands/context-e2e-specs.js new file mode 100644 index 0000000..48300e6 --- /dev/null +++ b/test/e2e/commands/context-e2e-specs.js @@ -0,0 +1,39 @@ +import { buildWdIoOptions } from '../helpers'; +import { remote as wdio } from 'webdriverio'; + +describe('context', function () { + let chai; + /** @type {import('webdriverio').Browser} */ + let driver; + + before(async function () { + chai = await import('chai'); + const chaiAsPromised = await import('chai-as-promised'); + + chai.should(); + chai.use(chaiAsPromised.default); + + driver = await wdio(buildWdIoOptions('Root')); + }); + + after(async function () { + try { + if (driver) { + await driver.deleteSession(); + } + } finally { + driver = null; + } + }); + + it('should support context api', async function () { + (await driver.getAppiumContext()).should.equal('NATIVE_APP'); + (await driver.getAppiumContexts()).should.eql(['NATIVE_APP']); + await driver.switchAppiumContext('NATIVE_APP'); + }); + + it('should throw an error if invalid context', async function () { + await driver.switchAppiumContext('INVALID_CONTEXT').should.rejected; + }); + +}); diff --git a/test/unit/driver-specs.js b/test/unit/driver-specs.js index 794ecf0..10aa63a 100644 --- a/test/unit/driver-specs.js +++ b/test/unit/driver-specs.js @@ -42,6 +42,19 @@ describe('driver.js', function () { driver.caps.cap.should.equal('foo'); }); + describe('context simulation', function () { + it('should support context commands', async function () { + let driver = new WindowsDriver({ app: 'myapp'}, false); + (await driver.getCurrentContext()).should.equal('NATIVE_APP'); + (await driver.getContexts()).should.eql(['NATIVE_APP']); + await driver.setContext('NATIVE_APP'); + }); + it('should throw an error if invalid context', async function () { + let driver = new WindowsDriver({ app: 'myapp'}, false); + await driver.setContext('INVALID_CONTEXT').should.rejected; + }); + }); + // TODO: Implement or delete //it('should set the default context', async function () { // let driver = new SelendroidDriver({}, false);