Add helper method for matching endpoints and method names to command names
MODIFY
index.js
index.js
@@ -9,11 +9,12 @@ export default BaseDriver;
// MJSONWP exports
import { MobileJsonWireProtocol, routeConfiguringFunction, errors, isErrorType,
- errorFromCode, ALL_COMMANDS, NO_SESSION_ID_COMMANDS,
- isSessionCommand } from './lib/mjsonwp';
+ errorFromCode, ALL_COMMANDS, METHOD_MAP, routeToCommandName,
+ NO_SESSION_ID_COMMANDS, isSessionCommand } from './lib/mjsonwp';
export { MobileJsonWireProtocol, routeConfiguringFunction, errors, isErrorType,
- errorFromCode, ALL_COMMANDS, NO_SESSION_ID_COMMANDS, isSessionCommand };
+ errorFromCode, ALL_COMMANDS, METHOD_MAP, routeToCommandName,
+ NO_SESSION_ID_COMMANDS, isSessionCommand };
// Express exports
import { server } from './lib/express/server';

MODIFY
lib/mjsonwp/index.js
lib/mjsonwp/index.js
import { MJSONWP as MobileJsonWireProtocol, isSessionCommand,
routeConfiguringFunction } from './mjsonwp';
-import { NO_SESSION_ID_COMMANDS, ALL_COMMANDS } from './routes';
+import { NO_SESSION_ID_COMMANDS, ALL_COMMANDS, METHOD_MAP,
+ routeToCommandName } from './routes';
import { errors, isErrorType, errorFromCode } from './errors';
export { MobileJsonWireProtocol, routeConfiguringFunction, errors, isErrorType,
- errorFromCode, ALL_COMMANDS, NO_SESSION_ID_COMMANDS, isSessionCommand };
+ errorFromCode, ALL_COMMANDS, METHOD_MAP, routeToCommandName,
+ NO_SESSION_ID_COMMANDS, isSessionCommand };

MODIFY
lib/mjsonwp/routes.js
lib/mjsonwp/routes.js
@@ -455,7 +455,56 @@ for (let v of _.values(METHOD_MAP)) {
}
}
+const RE_ESCAPE = /[\-\[\]{}()+?.,\\\^$|#\s]/g;
+const RE_PARAM = /([:*])(\w+)/g;
+
+class Route {
+ constructor (route) {
+ this.paramNames = [];
+
+ let reStr = route.replace(RE_ESCAPE, "\\$&");
+ reStr = reStr.replace(RE_PARAM, (_, mode, name) => {
+ this.paramNames.push(name);
+ return mode === ":" ? "([^/]*)" : "(.*)";
+ });
+ this.routeRegexp = new RegExp(`^${reStr}$`);
+ }
+
+ parse (url) {
+ let matches = url.match(this.routeRegexp);
+ if (!matches) return;
+ let i = 0;
+ let params = {};
+ while (i < this.paramNames.length) {
+ const paramName = this.paramNames[i++];
+ params[paramName] = matches[i];
+ }
+ return params;
+ }
+}
+
+function routeToCommandName (endpoint, method) {
+ let dstRoute = null;
+ const actualEndpoint = _.startsWith(endpoint, '/') ? endpoint : `/${endpoint}`;
+ for (let currentRoute of _.keys(METHOD_MAP)) {
+ const route = new Route(currentRoute);
+ // we don't care about the actual session id for matching
+ if (route.parse(`/wd/hub/session/ignored-session-id${actualEndpoint}`) ||
+ route.parse(`/wd/hub${actualEndpoint}`) || route.parse(actualEndpoint)) {
+ dstRoute = currentRoute;
+ break;
+ }
+ }
+ if (!dstRoute) return;
+
+ const methods = _.get(METHOD_MAP, dstRoute);
+ if (_.has(methods, method)) {
+ const dstMethod = _.get(methods, method);
+ if (dstMethod.command) return dstMethod.command;
+ }
+}
+
// driver commands that do not require a session to already exist
const NO_SESSION_ID_COMMANDS = ['createSession', 'getStatus', 'getSessions'];
-export { METHOD_MAP, ALL_COMMANDS, NO_SESSION_ID_COMMANDS };
+export { METHOD_MAP, ALL_COMMANDS, NO_SESSION_ID_COMMANDS, routeToCommandName };

MODIFY
test/mjsonwp/routes-specs.js
test/mjsonwp/routes-specs.js
// transpile:mocha
import { _ } from 'lodash';
-import { METHOD_MAP } from '../../lib/mjsonwp/routes';
+import { METHOD_MAP, routeToCommandName } from '../../lib/mjsonwp/routes';
import crypto from 'crypto';
import chai from 'chai';
@@ -42,4 +42,35 @@ describe('MJSONWP', () => {
});
});
+ describe('check route to command name conversion', () => {
+ it('should properly lookup correct command name for endpoint with session', () => {
+ const cmdName = routeToCommandName('/timeouts/implicit_wait', 'POST');
+ cmdName.should.equal('implicitWait');
+ });
+
+ it('should properly lookup correct command name for endpoint without session', () => {
+ const cmdName = routeToCommandName('/status', 'GET');
+ cmdName.should.equal('getStatus');
+ });
+
+ it('should properly lookup correct command name for endpoint without leading slash', () => {
+ const cmdName = routeToCommandName('status', 'GET');
+ cmdName.should.equal('getStatus');
+ });
+
+ it('should properly lookup correct command name for fully specified endpoint', () => {
+ const cmdName = routeToCommandName('/wd/hub/status', 'GET');
+ cmdName.should.equal('getStatus');
+ });
+
+ it('should not find command name if incorrect input data has been specified', () => {
+ for (let [route, method] of [['/wd/hub/status', 'POST'],
+ ['/xstatus', 'GET'],
+ ['status', 'POST']]) {
+ const cmdName = routeToCommandName(route, method);
+ chai.should().equal(cmdName, undefined);
+ }
+ });
+ });
+
});

Add helper method for matching endpoints and method names to command names (#104)