chore: port fake plugin to the plugin monorepo
MODIFY
packages/appium-plugins/packages/base/package.json
packages/appium-plugins/packages/base/package.json
},
"scripts": {
"test:unit": "mocha ./build/test/unit/*-specs.js",
- "test:e2e": "ts-mocha test/e2e/*-specs.js"
+ "test:e2e": "mocha -t 160000 test/e2e/*-specs.js"
},
"devDependencies": {
}

MODIFY
packages/appium-plugins/packages/base/tsconfig.json
packages/appium-plugins/packages/base/tsconfig.json
"./test/**/*"
],
"exclude": [
- "build/*",
- "./node_modules"
+ "build",
+ "node_modules"
]
}

ADD
packages/appium-plugins/packages/fake/README.md
+# appium-fake-plugin
+A fake Appium 2.0 plugin used for testing and examples

ADD
packages/appium-plugins/packages/fake/index.js
+import FakePlugin from './lib/plugin';
+
+export default FakePlugin;
+export { FakePlugin };

ADD
packages/appium-plugins/packages/fake/lib/plugin.js
+/* eslint-disable no-case-declarations */
+
+import BasePlugin from '@appium/base-plugin';
+import B from 'bluebird';
+
+
+export default class FakePlugin extends BasePlugin {
+
+ updatesServer = true;
+
+ commands = ['getPageSource', 'findElement', 'getFakeSessionData', 'setFakeSessionData'];
+
+ newMethodMap = {
+ '/session/:sessionId/fake_data': {
+ GET: {command: 'getFakeSessionData'},
+ POST: {command: 'setFakeSessionData', payloadParams: {required: ['data']}}
+ },
+ };
+
+ fakeRoute (req, res) {
+ this.logger.debug('Sending fake route response');
+ res.send(JSON.stringify({fake: 'fakeResponse'}));
+ }
+
+ async updateServer (expressApp/*, httpServer*/) { // eslint-disable-line require-await
+ this.logger.debug('Updating server');
+ expressApp.all('/fake', this.fakeRoute.bind(this));
+ }
+
+ async handle (next, driver, cmdName, ...args) {
+ switch (cmdName) {
+ case 'getPageSource':
+ await B.delay(10);
+ return `<Fake>${JSON.stringify(args)}</Fake>`;
+ case 'findElement':
+ this.logger.info(`Before the command ${cmdName} is run with args ${JSON.stringify(args)}`);
+ const originalRes = await next();
+ this.logger.info(`After the command ${cmdName} is run`);
+ originalRes.fake = true;
+ return originalRes;
+ case 'getFakeSessionData':
+ return driver.fakeSessionData || null;
+ case 'setFakeSessionData':
+ driver.fakeSessionData = args[0];
+ return null;
+ default:
+ throw new Error(`Don't know how to handle command ${cmdName}`);
+ }
+ }
+}

ADD
packages/appium-plugins/packages/fake/package.json
+{
+ "name": "@appium/fake-plugin",
+ "description": "A fake Appium 2.0 plugin",
+ "tags": [
+ "appium"
+ ],
+ "version": "0.2.0",
+ "author": "Appium <maintainers@appium.io>",
+ "license": "Apache-2.0",
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/appium/appium-fake-plugin.git"
+ },
+ "bugs": {
+ "url": "https://github.com/appium/appium-fake-plugin/issues"
+ },
+ "engines": {
+ "node": ">=10",
+ "npm": ">=6"
+ },
+ "appium": {
+ "pluginName": "fake",
+ "mainClass": "FakePlugin"
+ },
+ "main": "./build/index.js",
+ "directories": {
+ "lib": "./lib"
+ },
+ "files": [
+ "lib",
+ "build"
+ ],
+ "dependencies": {
+ "@appium/base-plugin": "^1.5.0",
+ "appium-support": "2.x",
+ "bluebird": "^3.7.2",
+ "lodash": "^4.17.11"
+ },
+ "scripts": {
+ "test:unit": "mocha ./build/test/unit/*-specs.js",
+ "test:e2e": "mocha -t 160000 test/e2e/*-specs.js"
+ },
+ "devDependencies": {
+ }
+}

ADD
packages/appium-plugins/packages/fake/test/unit/plugin-specs.js
+import FakePlugin from '../../index';
+import B from 'bluebird';
+import chai from 'chai';
+import chaiAsPromised from 'chai-as-promised';
+
+chai.use(chaiAsPromised);
+const should = chai.should();
+
+class FakeExpress {
+ constructor () {
+ this.routes = {};
+ }
+
+ all (route, handler) {
+ this.routes[route] = handler;
+ }
+
+ async get (route) {
+ return await new B((resolve, reject) => {
+ try {
+ const res = {
+ send: resolve,
+ };
+ this.routes[route]({}, res);
+ } catch (e) {
+ reject(e);
+ }
+ });
+ }
+}
+
+describe('fake plugin', function () {
+ it('should exist', function () {
+ should.exist(FakePlugin);
+ });
+
+ it('should update an express app with a fake route', async function () {
+ const p = new FakePlugin('fake');
+ const app = new FakeExpress();
+ await app.get('/fake').should.eventually.be.rejected;
+ p.updateServer(app);
+ await app.get('/fake').should.eventually.eql(JSON.stringify({fake: 'fakeResponse'}));
+ });
+
+ it('should wrap find element', async function () {
+ const p = new FakePlugin('fake');
+ await p.handle(() => ({el: 'fakeEl'}), {}, 'findElement', 'arg1', 'arg2').should.eventually.eql({
+ el: 'fakeEl',
+ fake: true,
+ });
+ });
+
+ it('should handle page source', async function () {
+ const p = new FakePlugin('fake');
+ await p.handle(() => {}, {}, 'getPageSource', 'arg1', 'arg2').should.eventually.eql(
+ '<Fake>["arg1","arg2"]</Fake>'
+ );
+ });
+
+ it('should handle getFakeSessionData', async function () {
+ const p = new FakePlugin('fake');
+ await p.handle(() => {}, {fakeSessionData: 'hi'}, 'getFakeSessionData').should.eventually.eql('hi');
+ await p.handle(() => {}, {}, 'getFakeSessionData').should.eventually.eql(null);
+ });
+
+ it('should handle setFakeSessionData', async function () {
+ const p = new FakePlugin('fake');
+ const driver = {};
+ await p.handle(() => {}, driver, 'setFakeSessionData', 'foobar').should.eventually.eql(null);
+ await p.handle(() => {}, driver, 'getFakeSessionData').should.eventually.eql('foobar');
+ });
+});

ADD
packages/appium-plugins/packages/fake/tsconfig.json
+{
+ "extends": "../../tsconfig.json",
+ "compilerOptions": {
+ "outDir": "./build"
+ },
+ "include": [
+ "./index.js",
+ "./lib/**/*",
+ "./test/**/*"
+ ],
+ "exclude": [
+ "build",
+ "node_modules"
+ ]
+}

MODIFY
packages/appium-plugins/packages/images/package.json
packages/appium-plugins/packages/images/package.json
"@appium/base-plugin": "^1.5.0"
},
"scripts": {
- "test:unit": "ts-mocha test/unit/*-specs.js",
- "test:e2e": "ts-mocha test/e2e/*-specs.js"
+ "test:unit": "mocha ./build/test/unit/*-specs.js",
+ "test:e2e": "mocha -t 160000 test/e2e/*-specs.js"
},
"devDependencies": {
}

MODIFY
packages/appium-plugins/packages/images/tsconfig.json
packages/appium-plugins/packages/images/tsconfig.json
"outDir": "./build"
},
"include": [
- "./*.js",
+ "./index.js",
"./lib/**/*",
"./test/**/*"
],
"exclude": [
- "./build",
- "./node_modules"
+ "build",
+ "node_modules"
]
}

chore: port fake plugin to the plugin monorepo