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 scripting to handle when site is under maintenance


#1

We have a site that is monitored with a New Relic “Scripted Browser” Synthetic.

The site occasionally enters “maintenance mode” for a short (unscheduled) window of time, typically late at night and/or on weekends. When you visit the site during those times, the entire site is replaced with a simple HTML maintenance page.

I would like to modify our script to detect the presence of this page, probably by searching for text in the page body like… “currently being updated” and short-circuit the script without triggering an alert. Note that we still need to be able to detect when the site is down or otherwise non-functional according to our normal script flow.

  1. maintenance mode (never alert)
  2. normal site - test scripted flow (alert if failure or site is down)

I can’t figure out how to do this. Any thoughts?


#2

Hi @william.krick - I had the same issue and wrote a script which you can use as a template.


#3

I’m having trouble getting this to work because of the way that promises need to be chained.

Also, our maintenance page has the same URL as the real page, if it matters. We have to detect the maintenance page by searching the body text for a specific string.

EDIT. I managed to get it working using your example as a guide. Thank you.


#4

Thanks for confirming you got this working :smiley:


#5

Well… I got it working for a really simple example, but for my real script I’m still having trouble. I’ll keep you posted.


#6

Oh ok! Sorry for misunderstanding - please do keep us up to date with your more complex script example. :slight_smile:


#7

OK, I managed to sort out my maintenance page situation. The trick was to do the $browser.get() twice. Once to look for the maintenance text, and then again if it is not found. Here’s a working example…

var assert = require('assert');

var inMaintenanceMode = false;
// this is the string to search for to identify the maintenance page
var maintenanceText = ("Example Domain").toLowerCase();

$browser.get('http://example.com').then(function(){

  // search body for maintenance mode text 
  return $browser.findElement($driver.By.tagName("html")).getText().then(function(body){
    inMaintenanceMode = (body.toLowerCase().indexOf(maintenanceText) != -1);
  }).then(function() {
    
    // return early if site is in maintenance mode
    if (inMaintenanceMode) {
      console.log("site is in maintenance mode");
      return true;
    }

    // If we got here, then site is not in maintenance mode. Run through normal script...

    // #### BEGIN NORMAL SCRIPT ####
    return $browser.get('http://example.com').then(function(){
      // Check the H1 title matches "Example Domain"
      return $browser.findElement($driver.By.css('h1')).then(function(element){
        return element.getText().then(function(text){
          assert.equal('Example Domain', text, 'Page H1 title did not match');
        });
      });
    }).then(function(){
      // Check that the external link matches "http://www.iana.org/domains/example"
      return $browser.findElement($driver.By.css('div > p > a')).then(function(element){
        return element.getAttribute('href').then(function(link){
          assert.equal('http://www.iana.org/domains/example', link, 'More information link did not match');
        });
      });
    });
    // #### END NORMAL SCRIPT ####

  });
});