Synthetic to fail if it matches a text within a page

Synthetics question template

Hi NR Techies,

I am looking to create synthetics for a page like

https://status.cloud.google.com/regional/asia
or
https://www.aramex.com.au/our-services/service-updates/

and see if it match the red icon. First i tried to match the xpath but unable they use different xpath on each icon and it does not works and thus I am thinking of matching if there is a red icon within the page

/**
 * Feel free to explore, or check out the full documentation
 * https://docs.newrelic.com/docs/synthetics/new-relic-synthetics/scripting-monitors/writing-api-tests
 * for details.
 */

var assert = require('assert');

$http.post('https://www.aramex.com.au/our-services/service-updates/',

//Define expected results using callback function.
function callback(error, response, body) {
    //Log status code to Synthetics console.
    console.log(response.statusCode + " status code")
    //Verify endpoint returns 200 (OK) response code.
    assert.ok(response.statusCode == 200, 'Expected 200 OK response');
    //Verify that `body` contains icon-red`, it should fail.
    assert.ok(body.success == false, 'icon-red' + body.success);
    //Log end of script.
    console.log("End reached");
});

I am unsure how to match a text within the output of a page and fail if it matches. Please advice.

Thanks in advance,
Amit

Hi, @amit.sharma4: Your script looks like an API test; to find elements on a web page, I recommend that you use a scripted browser instead. Try something like this:

var assert = require('assert');

// Get the page
$webDriver.get('https://www.aramex.com.au/our-services/service-updates/')
  .then(function() {
    // Get the number of `img` tags whose `src` attribute contains `icon-red`
    return $webDriver.findElements($selenium.By.css('img[src*="icon-red"'));
  })
  .then(function(elements) {
    // If the number of found elements is > 0, fail the script
    console.log('Red icons found = ' + elements.length);
    assert.ok(elements.length == 0, "Unexpected red icons found");
  });
2 Likes

Works like a charm.

Legend philweber.

Appreciate your response. Many thanks :slight_smile: