How to use registerToGrid method in Appium

Best JavaScript code snippet using appium

Run Appium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

grid-register.js

Source: grid-register.js Github

copy
1"use strict";
2var request = require('request')
3  , fs = require('fs')
4  , logger = require('./logger.js').get('appium');
5
6exports.registerNode = function (configFile, addr, port) {
7  fs.readFile(configFile, 'utf-8', function (err, data) {
8    if (err) {
9      logger.error("Unable to load node configuration file to register with grid");
10      return;
11    }
12    // Check presence of data before posting  it to the selenium grid
13    if (data) {
14      postRequest(data, addr, port);
15    } else {
16      logger.error("No data found in the node configuration file to send to the grid");
17    }
18  });
19};
20
21function registerToGrid(options_post, jsonObject) {
22  request(options_post, function (error, response/*, body*/) {
23    if (error !== null || response.statusCode !== 200) {
24      logger.error("Request to register with grid was Unsuccessful...");
25    } else {
26      var logMessage = "Appium successfully registered with the grid on " + jsonObject.configuration.hubHost + ":" + jsonObject.configuration.hubPort;
27      logger.debug(logMessage);
28    }
29  });
30}
31
32function postRequest(data, addr, port) {
33  //parse json to get hub host and port
34  var jsonObject;
35  try {
36    jsonObject = JSON.parse(data);
37  } catch (e) {
38    return logger.error("Syntax error in node configuration file: " + e.message);
39  }
40
41  // if the node config doesnt have the appium/webdriver url, host, and port,
42  // automatically ad it based on how appium was init
43  // otherwise, we will take whatever the user setup
44  // because we will always set localhost/127.0.0.1. this won't work if your
45  // node and grid aren't in the same place
46  if (!jsonObject.configuration.url || !jsonObject.configuration.host || !jsonObject.configuration.port) {
47    jsonObject.configuration.url = 'http://' + addr + ':' + port + '/wd/hub';
48    jsonObject.configuration.host = addr;
49    jsonObject.configuration.port = port;
50
51    // re-serialize the configuration with the auto populated data
52    data = JSON.stringify(jsonObject);
53  }
54
55  // prepare the header
56  var post_headers = {
57    'Content-Type'  : 'application/json'
58  , 'Content-Length': data.length
59  };
60  // the post options
61  var options_post = {
62    url       : 'http://' + jsonObject.configuration.hubHost + ':' + jsonObject.configuration.hubPort + '/grid/register'
63  , method  : 'POST'
64  , body    : data
65  , headers : post_headers
66  };
67
68  if (jsonObject.configuration.register !== true) {
69    logger.debug("no registration sent ( " + jsonObject.configuration.register + " = false )");
70  } else {
71    var registerCycleTime = jsonObject.configuration.registerCycle;
72    if (registerCycleTime !== null && registerCycleTime > 0) {
73      //initiate a new Thread
74      var first = true;
75      logger.debug("starting auto register thread for grid. Will try to register every " + registerCycleTime + " ms.");
76      setInterval(function () {
77        if (first !== true) {
78          isAlreadyRegistered(jsonObject, function (isRegistered) {
79            if (isRegistered !== null && isRegistered !== true) {
80              // make the http POST to the grid for registration
81              registerToGrid(options_post, jsonObject);
82            }
83          });
84        } else {
85          first = false;
86          registerToGrid(options_post, jsonObject);
87        }
88      }, registerCycleTime);
89
90    }
91  }
92}
93
94function isAlreadyRegistered(jsonObject, cb) {
95  //check if node is already registered
96  var id = "http://" + jsonObject.configuration.host + ":" + jsonObject.configuration.port;
97  request({
98    uri       : 'http://' + jsonObject.configuration.hubHost + ':' + jsonObject.configuration.hubPort +
99      '/grid/api/proxy' + "?id=" + id
100  , method  : "GET"
101  , timeout : 10000
102  }, function (error, response /*, body*/) {
103    if (error !== null || response === undefined || response.statusCode !== 200) {
104      logger.debug("hub down or not responding.");
105      return cb(null);
106    }
107    var responseData = JSON.parse(response.body);
108    return cb(responseData.success);
109  });
110
111}
112
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Appium on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)