Conditional Check for Element in Synthetics 5.0+

Hello, I saw some other posts trying to solve for this problem but none were exactly what I needed. I was able to combine the tips I found into a solution that worked for me, I thought I’d post it up here in case someone else has the same need.

We have an overlay that pops up after releases to show changes in the current release to customers. This element has an ID associated with the element that is not present when the overlay isn’t enabled. The overlay has a button to close it I needed to click to continue the test but I needed the script to continue if no overlay was found.

This version is a bit shorter than the others I saw and allows you to specify the step in the script so it is recorded correctly in the logger without having to edit the param in the logger function.

Hopefully others can make use of this snippet in their Synthetic Monitors.

var Q = require('q')

function checkOverlay (step){
  var deferred = Q.defer();
  var element = $browser.findElements("ID of overlay")).then(function(elem){
    if(elem.length > 0){
      logger.log(step, "post release overlay found!")
      $browser.findElement(By.xpath('Xpath to close button')).click();
    else {
      logger.log(step, "no post release overlay found, continuing")
  return deferred.promise;


Great work @atittle - Thank you SO MUCH for sharing.

1 Like

Hey @atittle! I was able to use this solution, which appears to work without issue in my code. Thank you for sharing this! I did have a couple questions I’m hoping you can help me understand, and these are probably dumb questions (I’m still learning the basics).

I’m not sure I fully understand the purpose of var Q (and what the “require” means in the variable declaration). I see it is used in the function with var deferred = Q.defer(), but I’m not sure what this is doing. Can you give some insight into this?

Can you clarify the purpose of var element? I know we are using it to find the element on the page, but then we use the function(elem) to evaluate the length. I didn’t see it used elsewhere so I was curious to know its purpose (again, I’m sure it’s needed and the reason is simple, but just trying to understand).

I have seen the if(elem.length > 0) in several posts as well as a solution for finding/evaluating elements. Can you describe what specifically is the element that it is evaluating the length? If you changed this to less than 0, would this effectively turn this into a statement that says “if element is not found?”

I’m still learning promises. What is the return deferred.promise doing in this case?

Again, great code and thank you so much for sharing! I appreciate any additional information and guidance you can provide :slight_smile:

1 Like

Hi @larsonb7, i’d be happy to clarify some of your questions.

Q is actually a Promise library for NodeJS, the require function is telling the script to load in the Q module so that the classes in that module can be used. Q.defer() is telling the script to access the defer() function within the Q module. More info on Q below:

With var element = anyFunction() you are basically assigning the result of that function to the variable element, so whatever you return within that function will be the variables value.

elem is the result of findElements(). It will return an Array of results containing all the elements it found, in this case with that id. The Array will look something like this:

['elem1', 'elem2', 'elem3']

If the function does not find any elements then it will return an empty Array [], elem.length is then checking there are elements in the Array, if the length is 0 no elements were found so anything > 0 has elements.

I hope that helps, let me know if you have any other questions :slight_smile:


Thank you so much, @rdouglas, for the great information! This helps immensely :slight_smile:


Shout-out to @atittle for this perfection of a script.

I don’t think there’s any better way to carry out an if function to check if an element exists.

EDIT: To re-iterate, for me, it looks a little like this.

let target = $browser.findElements('identifier')).then(function (e) {
  if (e.length > 0) {
    console.log('✔ Found element');
  } else {
    console.log('❌ No match found');