Get external data and use results in script elsewhere

This example makes a http request and uses the resulting data to fill in a form in the script. It could be extended to pull externally stored credentials and pass them in to prevent needing to save plain text credentials into Synthetics directly.

//This script makes a REST call then uses the results later in the script to fill in form data
var request = require('urllib-sync').request;
//Make a REST call to get some geolocation data
var result = request('http://gd.geobytes.com/GetCityDetails');

$browser.get('https://google.com').then(function(){
  var parsedResponse = JSON.parse(result.data.toString('utf-8'));
  return parsedResponse;
}).then(function(parsedResponse){
  console.log("Search for "+parsedResponse.geobytescity);
  $browser.findElement($driver.By.id('lst-ib')).sendKeys(parsedResponse.geobytescity+"\n");
}).then(function(){
  $browser.waitForAndFindElement($driver.By.xpath('//*[@id="rso"]/div[2]/li[1]/div/h3/a'));
});
2 Likes