From bda413d2fc5dde20b4a6e4944be9d693868c84a9 Mon Sep 17 00:00:00 2001 From: Priyansh Garg Date: Thu, 5 Jan 2023 20:14:22 +0530 Subject: [PATCH 1/3] Add global app property. --- lib/index.js | 14 ++++++++++++++ lib/testsuite/index.js | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/lib/index.js b/lib/index.js index bac5ccfcb5..6be44af459 100644 --- a/lib/index.js +++ b/lib/index.js @@ -359,6 +359,20 @@ Object.defineProperty(Nightwatch, 'browser', { } }); +Object.defineProperty(Nightwatch, 'app', { + configurable: true, + get() { + if (global.browser) { + return global.browser; + } + + const err = new TypeError('Nightwatch client is not yet available.'); + err.addDetailedErr = true; + + throw err; + } +}); + Object.defineProperty(Nightwatch, 'Key', { configurable: true, get() { diff --git a/lib/testsuite/index.js b/lib/testsuite/index.js index 4c3197017f..1ac1ef6bc1 100644 --- a/lib/testsuite/index.js +++ b/lib/testsuite/index.js @@ -260,6 +260,13 @@ class TestSuite { return null; } + Object.defineProperty(global, 'app', { + configurable: true, + get: function() { + return global.browser; + } + }); + Object.defineProperty(global, 'by', { configurable: true, get: function() { From 8b6061edd87e772219bb476088b4abb8b122112f Mon Sep 17 00:00:00 2001 From: Priyansh Garg Date: Fri, 20 Jan 2023 19:28:39 +0530 Subject: [PATCH 2/3] Add tests. --- test/apidemos/appium/appiumTest.js | 33 ++++++++++++ test/src/apidemos/appium/testAppiumAPI.js | 66 +++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 test/apidemos/appium/appiumTest.js create mode 100644 test/src/apidemos/appium/testAppiumAPI.js diff --git a/test/apidemos/appium/appiumTest.js b/test/apidemos/appium/appiumTest.js new file mode 100644 index 0000000000..703939030e --- /dev/null +++ b/test/apidemos/appium/appiumTest.js @@ -0,0 +1,33 @@ +const assert = require('assert'); + +describe('appium api demo', function () { + after((app) => app.end()); + + const availableAppiumCommands = [ + 'startActivity', + 'getCurrentActivity', + 'getCurrentPackage', + 'getOrientation', + 'setOrientation', + 'getGeolocation', + 'setGeolocation', + 'pressKeyCode', + 'longPressKeyCode', + 'hideKeyboard', + 'isKeyboardShown', + 'getContexts', + 'getContext', + 'setContext' + ]; + + it('test chrome available API commands', async function () { + // app variable is available globally + // eslint-disable-next-line + assert.strictEqual(app !== undefined, true); + + availableAppiumCommands.forEach((command) => { + // eslint-disable-next-line + assert.strictEqual(typeof app.appium[command], 'function'); + }); + }); +}); diff --git a/test/src/apidemos/appium/testAppiumAPI.js b/test/src/apidemos/appium/testAppiumAPI.js new file mode 100644 index 0000000000..95057cad8e --- /dev/null +++ b/test/src/apidemos/appium/testAppiumAPI.js @@ -0,0 +1,66 @@ +const {strictEqual} = require('assert'); +const path = require('path'); +const common = require('../../../common.js'); +const MockServer = require('../../../lib/mockserver.js'); +const NightwatchClient = common.require('index.js'); + +describe('appium api demos', function () { + beforeEach(function (done) { + this.server = MockServer.init(undefined, {port: 4723}); + this.server.on('listening', () => { + done(); + }); + }); + + afterEach(function (done) { + this.server.close(function () { + done(); + }); + }); + + it('run appium api demo tests basic', function () { + const testsPath = path.join(__dirname, '../../../apidemos/appium/appiumTest.js'); + + const globals = { + waitForConditionPollInterval: 50, + + reporter(results) { + if (results.lastError) { + throw results.lastError; + } + } + }; + + MockServer.addMock({ + url: '/wd/hub/session', + method: 'POST', + response: JSON.stringify({ + value: { + sessionId: '1352110219202', + version: 'TEST', + platform: 'TEST' + } + }) + }); + + return NightwatchClient.runTests(testsPath, { + selenium: { + host: 'localhost', + port: 4723, + start_process: false, + use_appium: true + }, + desiredCapabilities: { + browserName: '' + }, + output: false, + skip_testcases_on_fail: false, + silent: true, + persist_globals: true, + globals, + output_folder: false + }).then(_ => { + strictEqual(NightwatchClient.app !== undefined, true); + }); + }); +}); From 53b2557c90bd114f5fb5bbb1350ff94094391bf4 Mon Sep 17 00:00:00 2001 From: Priyansh Garg Date: Thu, 2 Feb 2023 23:48:14 +0530 Subject: [PATCH 3/3] Work on suggestion and add new test. --- .eslintrc | 1 + lib/index.js | 18 ++------ test/apidemos/appium/appiumTest.js | 10 +++-- test/src/apidemos/appium/testAppiumAPI.js | 54 ++++++++++++++++++----- 4 files changed, 55 insertions(+), 28 deletions(-) diff --git a/.eslintrc b/.eslintrc index 95be16079b..1a45d44c88 100644 --- a/.eslintrc +++ b/.eslintrc @@ -114,6 +114,7 @@ "by": "readonly", "expect": "readonly", "browser": "readonly", + "app": "readonly", "Key": "readonly" } } diff --git a/lib/index.js b/lib/index.js index 6be44af459..2c237e0561 100644 --- a/lib/index.js +++ b/lib/index.js @@ -345,7 +345,7 @@ Object.defineProperty(Nightwatch, 'by', { } }); -Object.defineProperty(Nightwatch, 'browser', { +const globalBrowserDescriptor = { configurable: true, get() { if (global.browser) { @@ -357,21 +357,11 @@ Object.defineProperty(Nightwatch, 'browser', { throw err; } -}); - -Object.defineProperty(Nightwatch, 'app', { - configurable: true, - get() { - if (global.browser) { - return global.browser; - } +}; - const err = new TypeError('Nightwatch client is not yet available.'); - err.addDetailedErr = true; +Object.defineProperty(Nightwatch, 'browser', globalBrowserDescriptor); - throw err; - } -}); +Object.defineProperty(Nightwatch, 'app', globalBrowserDescriptor); Object.defineProperty(Nightwatch, 'Key', { configurable: true, diff --git a/test/apidemos/appium/appiumTest.js b/test/apidemos/appium/appiumTest.js index 703939030e..059fd0f8dd 100644 --- a/test/apidemos/appium/appiumTest.js +++ b/test/apidemos/appium/appiumTest.js @@ -20,14 +20,18 @@ describe('appium api demo', function () { 'setContext' ]; - it('test chrome available API commands', async function () { + it('test appium available API commands', async function () { // app variable is available globally - // eslint-disable-next-line assert.strictEqual(app !== undefined, true); availableAppiumCommands.forEach((command) => { - // eslint-disable-next-line assert.strictEqual(typeof app.appium[command], 'function'); }); }); + + it('Search for Nightwatch', async function() { + app // available globally + .click('id', 'com.app:id/search') + .sendKeys('id', 'com.app:id/search', 'Nightwatch'); + }); }); diff --git a/test/src/apidemos/appium/testAppiumAPI.js b/test/src/apidemos/appium/testAppiumAPI.js index 95057cad8e..47dfc7d2b3 100644 --- a/test/src/apidemos/appium/testAppiumAPI.js +++ b/test/src/apidemos/appium/testAppiumAPI.js @@ -31,17 +31,49 @@ describe('appium api demos', function () { } }; - MockServer.addMock({ - url: '/wd/hub/session', - method: 'POST', - response: JSON.stringify({ - value: { - sessionId: '1352110219202', - version: 'TEST', - platform: 'TEST' - } - }) - }); + MockServer + .addMock({ + url: '/wd/hub/session', + method: 'POST', + response: JSON.stringify({ + value: { + sessionId: '13521-10219-202', + version: 'TEST', + platform: 'TEST' + } + }) + }, true) + .addMock({ + url: '/wd/hub/session/13521-10219-202/elements', + postdata: { + using: 'id', + value: 'com.app:id/search' + }, + method: 'POST', + response: JSON.stringify({ + status: 0, + state: 'success', + value: [{'element-6066-11e4-a52e-4f735466cecf': '0'}] + }) + }, true, true) + .addMock({ + url: '/wd/hub/session/13521-10219-202/element/0/click', + method: 'POST', + response: JSON.stringify({ + value: null + }) + }, true) + .addMock({ + url: '/wd/hub/session/13521-10219-202/element/0/value', + method: 'POST', + postdata: JSON.stringify({ + text: 'Nightwatch', + value: 'Nightwatch'.split('') + }), + response: JSON.stringify({ + value: null + }) + }, true); return NightwatchClient.runTests(testsPath, { selenium: {