Your data. Anywhere you go.

New Relic for iOS or Android


Download on the App Store    Android App on Google play


New Relic Insights App for iOS


Download on the App Store


Learn more

Close icon

Conditional check for element

synthetics-script
developer

#1

Here is a sample script that shows how to check for the presence of an element to determine which page loaded, helpful for if your site intermittently loads different versions or displays popups. It’s necessary to use the isElementPresent instead of the findElement because findElementwill stop the script with an error if the element if not found. The Q library is used to promise-ify the two functions called so they can be properly sequenced or inserted into another script to call the checkSite function multiple times.

var Q = require('q');
function randomInt (low, high) {
    return Math.floor(Math.random() * (high - low) + low);
}

function getSite(random) {
var deferred = Q.defer();
  if(result == 1) {
    $browser.get('http://newrelic.com');
  } else {
    $browser.get('http://google.com');
  }
return deferred.promise;
}

function checkSite(){
var deferred = Q.defer();
var element = $browser.isElementPresent($driver.By.id("search-start"))
.then(function(elem){
  if(elem === true){
    console.log("click search element");
    $browser.findElement($driver.By.id("search-start")).click();
  }
  else {
    console.log("loaded google site");
  }
});
return deferred.promise;
}

var result = randomInt(1,10);
console.log("Random Int was "+result);

getSite(result)
.then(checkSite());

FORESEE Popup Workaround