From 657a83bdcb331c23aa00a0fce319276abd662f13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Gonz=C3=A1lez=20Alonso?= Date: Thu, 20 Oct 2016 15:23:43 +0200 Subject: [PATCH] Allow to connect an Appium node to Selenium Grid 3 --- lib/grid-register.js | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/lib/grid-register.js b/lib/grid-register.js index bfbf5d5c5..bcf3debe8 100644 --- a/lib/grid-register.js +++ b/lib/grid-register.js @@ -26,7 +26,7 @@ async function registerToGrid (options_post, jsonObject) { if (response === undefined || response.statusCode !== 200) { throw new Error('Request failed'); } - let logMessage = `Appium successfully registered with the grid on ${jsonObject.configuration.hubHost}:${jsonObject.configuration.hubPort}`; + let logMessage = `Appium successfully registered with the grid on ${jsonObject.hubHost}:${jsonObject.hubPort}`; logger.debug(logMessage); } catch (err) { logger.error(`Request to register with grid was unsuccessful: ${err.message}`); @@ -42,19 +42,28 @@ async function postRequest (data, addr, port) { logger.errorAndThrow(`Syntax error in node configuration file: ${err.message}`); } + // backward compatibility with Selenium 2 config files + if (jsonObject.hasOwnProperty('configuration')) { + for (var property in jsonObject.configuration) { + if (jsonObject.configuration.hasOwnProperty(property)) { + jsonObject[property] = jsonObject.configuration[property]; + } + } + } + // if the node config does not have the appium/webdriver url, host, and port, // automatically add it based on how appium was initialized // otherwise, we will take whatever the user setup // because we will always set localhost/127.0.0.1. this won't work if your // node and grid aren't in the same place - if (!jsonObject.configuration.url || !jsonObject.configuration.host || !jsonObject.configuration.port) { - jsonObject.configuration.url = `http://${addr}:${port}/wd/hub`; - jsonObject.configuration.host = addr; - jsonObject.configuration.port = port; + if (!jsonObject.url || !jsonObject.host || !jsonObject.port) { + jsonObject.url = `http://${addr}:${port}/wd/hub`; + jsonObject.host = addr; + jsonObject.port = port; } // if the node config does not have id automatically add it - if (!jsonObject.configuration.id) { - jsonObject.configuration.id = `http://${jsonObject.configuration.host}:${jsonObject.configuration.port}`; + if (!jsonObject.id) { + jsonObject.id = `http://${jsonObject.host}:${jsonObject.port}`; } // re-serialize the configuration with the auto populated data @@ -67,19 +76,19 @@ async function postRequest (data, addr, port) { }; // the post options let post_options = { - url: `http://${jsonObject.configuration.hubHost}:${jsonObject.configuration.hubPort}/grid/register`, + url: `http://${jsonObject.hubHost}:${jsonObject.hubPort}/grid/register`, method: 'POST', body: data, headers: post_headers, resolveWithFullResponse: true // return the full response, not just the body }; - if (jsonObject.configuration.register !== true) { - logger.debug(`No registration sent (${jsonObject.configuration.register} = false)`); + if (jsonObject.register !== true) { + logger.debug(`No registration sent (${jsonObject.register} = false)`); return; } - let registerCycleTime = jsonObject.configuration.registerCycle; + let registerCycleTime = jsonObject.registerCycle; if (registerCycleTime !== null && registerCycleTime > 0) { // initiate a new Thread let first = true; @@ -101,10 +110,10 @@ async function postRequest (data, addr, port) { async function isAlreadyRegistered (jsonObject) { //check if node is already registered - let id = jsonObject.configuration.id; + let id = jsonObject.id; try { let response = await request({ - uri: `http://${jsonObject.configuration.hubHost}:${jsonObject.configuration.hubPort}/grid/api/proxy?id=${id}`, + uri: `http://${jsonObject.hubHost}:${jsonObject.hubPort}/grid/api/proxy?id=${id}`, method : 'GET', timeout : 10000, resolveWithFullResponse: true // return the full response, not just the body