Last active
June 6, 2019 23:22
-
-
Save mgrybyk/c48b847568f2c6f5e36b0592cbaf60b6 to your computer and use it in GitHub Desktop.
compare webdriver.json and jsonwp.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable no-console */ | |
const request = require('request') | |
const appiumJson = require('../packages/webdriver/protocol/appium.json') | |
const jsonwpJson = require('../packages/webdriver/protocol/jsonwp.json') | |
const mjsonwpJson = require('../packages/webdriver/protocol/mjsonwp.json') | |
const webdriverJson = require('../packages/webdriver/protocol/webdriver.json') | |
const url = 'https://raw.githubusercontent.com/appium/appium-base-driver/master/lib/protocol/routes.js' | |
let errors = 0 | |
const ignoredDefinitions = [ | |
{ uri: '/session/:sessionId/alert/text', methodName: 'POST' }, | |
{ uri: '/session', methodName: 'POST' }, | |
{ uri: '/session/:sessionId', methodName: 'GET' }, | |
{ uri: '/session/:sessionId/timeouts', methodName: 'POST' }, | |
{ uri: '/session/:sessionId/window', methodName: 'POST' }, | |
{ uri: '/session/:sessionId/element/:elementId/value', methodName: 'POST' }, | |
{ uri: '/session/:sessionId/buttondown', methodName: 'POST' }, | |
{ uri: '/session/:sessionId/buttonup', methodName: 'POST' }, | |
{ uri: '/session/:sessionId/touch/scroll', methodName: 'POST' }, | |
{ uri: '/session/:sessionId/touch/doubleclick', methodName: 'POST' }, | |
{ uri: '/session/:sessionId/touch/longclick', methodName: 'POST' }, | |
{ uri: '/session/:sessionId/local_storage', methodName: 'POST' }, | |
{ uri: '/session/:sessionId/session_storage', methodName: 'POST' }, | |
{ uri: '/session/:sessionId/element/active', methodName: 'POST' }, | |
{ uri: '/session/:sessionId/appium/start_recording_screen', methodName: 'POST' }, | |
{ uri: '/session/:sessionId/appium/stop_recording_screen', methodName: 'POST' }, | |
{ uri: '/session/:sessionId/appium/device/install_app', methodName: 'POST' }, | |
{ uri: '/session/:sessionId/element/:elementId/screenshot', methodName: 'GET' }, | |
{ uri: '/session/:sessionId/window/rect', methodName: 'POST' }, | |
{ uri: '/session/:sessionId/appium/device/app_state', methodName: 'POST' }, | |
] | |
const ignoredUrls = [ | |
'/session/:sessionId/window/handle', | |
'/session/:sessionId/window/:windowhandle/size', | |
'/session/:sessionId/window/:windowhandle/position', | |
'/session/:sessionId/window/:windowhandle/maximize', | |
'/session/:sessionId/element/:elementId', | |
'/session/:sessionId/rotation', // this one exists in Appium routes but missing in Appium docs | |
'/session/:sessionId/appium/compare_images', // same here | |
// '/session/:sessionId/appium/device/system_time', | |
'/session/:sessionId/appium/device/network_speed', // missing appium docs | |
'/session/:sessionId/screenshot/:elementId', | |
] | |
const assertion = { | |
equal(v1, v2, message) { | |
if (v1 !== v2) { | |
console.log(message) | |
errors++ | |
return false | |
} | |
return true | |
}, | |
ok(v, message) { | |
if (!v) { | |
console.log(message || v) | |
errors++ | |
return false | |
} | |
return true | |
} | |
} | |
run() | |
async function run() { | |
const routesFile = await getAppiumRoutes() | |
const METHOD_MAP = getRoutes(routesFile) | |
const transformedRoutes = trasformRoutes(METHOD_MAP) | |
compare(Object.assign({}, mjsonwpJson, jsonwpJson, webdriverJson, appiumJson), transformedRoutes) | |
console.log(errors) | |
} | |
async function getAppiumRoutes() { | |
return new Promise((resolve, reject) => { | |
request(url, undefined, (error, response, body) => { | |
if (error || !body) { | |
return reject(error || 'something went wrong') | |
} | |
resolve(body) | |
}) | |
}) | |
} | |
function getRoutes(routesFile) { | |
const strArr = routesFile.toString().split('\n') | |
const startIdx = strArr.indexOf('const METHOD_MAP = {') + 1 | |
const endIdx = strArr.indexOf('};') + 1 | |
const finalArr = strArr.slice(startIdx, endIdx) | |
const routesStr = 'METHOD_MAP = {\n' + finalArr.join('\n') | |
let METHOD_MAP = {} | |
eval(routesStr) | |
return METHOD_MAP | |
} | |
function trasformRoutes(METHOD_MAP) { | |
const trasformedRoutes = {} | |
Object.entries(METHOD_MAP).forEach(([uri, methods]) => { | |
const wdioUri = uri.replace('/wd/hub', '') | |
trasformedRoutes[wdioUri] = {} | |
Object.entries(methods).forEach(([methodName, methodDefinition]) => { | |
if (!ignoredDefinitions.some(x => x.uri === wdioUri && x.methodName === methodName)) { | |
const { command, payloadParams } = methodDefinition | |
const parameters = [] | |
trasformedRoutes[wdioUri][methodName] = { command, parameters } | |
if (payloadParams) { | |
['required', 'optional'].forEach(paramType => { | |
const params = payloadParams[paramType] | |
if (Array.isArray(params)) { | |
params.forEach(paramName => { | |
parameters.push({ | |
name: Array.isArray(paramName) ? paramName[0] : paramName, | |
required: !Array.isArray(paramName) && paramType === 'required' | |
}) | |
}) | |
} | |
}) | |
} | |
} | |
}) | |
}) | |
return trasformedRoutes | |
} | |
function compare(wdio, appium) { | |
// console.log(Object.keys(wdio).length, Object.keys(appium).length) | |
Object.entries(appium).forEach(([uri, methods]) => { | |
if (!ignoredUrls.includes(uri) && assertion.ok(wdio[uri], `MISSING ${uri}`)) { | |
Object.entries(methods).forEach(([methodName, methodDefinition]) => { | |
const { /*command,*/ parameters } = methodDefinition | |
if (assertion.ok(wdio[uri][methodName], `${uri} -> ${methodName} is missing`)) { | |
// const wdioCommand = wdio[uri][methodName].command | |
let wdioParameters = wdio[uri][methodName].parameters | |
// assertion.equal(wdioCommand, command, `${uri} -> ${methodName} -> ${command} != ${wdioCommand}`) | |
if (parameters.length === 0 && !wdioParameters) { | |
wdioParameters = [] | |
} | |
if (assertion.equal(Array.isArray(wdioParameters), true, `${uri} -> ${methodName} -> missing parameters!`)) { | |
assertion.equal(parameters.length, wdioParameters.length, `${uri} -> ${methodName} -> wrong parameters quantity!`) | |
parameters.forEach(parameter => { | |
const wdioParameterArr = wdioParameters.filter(x => x.name === parameter.name) | |
if (assertion.equal(wdioParameterArr.length, 1, `${uri} -> ${methodName} -> missing parameter ${parameter.name}`)) { | |
const wdioParameter = wdioParameterArr[0] | |
assertion.equal(parameter.required, wdioParameter.required, `${uri} -> ${methodName} -> ${parameter.name} -> required ${parameter.required} != ${wdioParameter.required}`) | |
} | |
}) | |
} | |
} | |
}) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment