Maintenance window workaround

While we await the Maintenance Window Maintenance Window I have considered a potential solution for Synthetic scripts. If you have a maintenance page that is displayed while a site is down, you can check for the url of the maintenance page in the script, set a flag and ignore the rest of the steps in the script.

Something along the lines of:

var inMaintenanceMode = false;

$browser.getCapabilities().then(function () { })

.then(function(){
  return $browser.get("http://www.mysite.com/homepage");
})
.then(function(){
  return $browser.getCurrentUrl()
    .then(function(currentUrl){
      inMaintenanceMode = (currentUrl == "http://www.mysite.com/maintenancepage");
   });
})
.then(function(){
  if(!inMaintenanceMode)
  {
    // perform normal actions for script
  }
  else
  {
    // log something to notify of maintenance mode
  }
})
1 Like

New and improved with all supporting code :sunglasses:

// Script-wide timeout for wait and waitAndFind functions (in ms)
var DefaultTimeout = 60000,
  startTime = new Date(),
  By = $driver.By,
  thisStep = 0;

var MaintenanceMessage = "Site is in maintenance mode";

var log = function(msg) {
  if(typeof message === "string")
  {
    var elapsedSecs = (new Date() - startTime) / 1000.0;
    console.log('Step ' + thisStep + ': ' + elapsedSecs.toFixed(2) + 's: ' + msg);
    thisStep++;
  }
};

log("Initialising ...");

var assert = require('assert');

var Initialise = function(siteUrl, maintenanceUrl){
  return $browser.getCapabilities()
  .then(function(){

    // User agent set to have monitoring appended to allow for stats filtering
    var UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36 monitoring";
		
    // Setting User Agent is not then-able, so we do this first (if defined and not default)
    if ((typeof UserAgent !== 'undefined') && (UserAgent != 'default')) {
      $browser.addHeader('User-Agent', UserAgent);
      log("Setting User-Agent to " + UserAgent);
    }
    return $browser.manage().window().maximize()
    .then(function(){
      log("Started");
      return $browser.get(siteUrl)
      .then(function(){
        if(typeof maintenanceUrl === "string" && maintenanceUrl.length > 0)
        {
          return $browser.getCurrentUrl()
          .then(function(currentUrl){
            assert.ok(currentUrl != maintenanceUrl, MaintenanceMessage);
          });
        }
      });
    });
  });
};

var completeProcessing = function()
{
//   put your normal processing in here, eg:
//   return $browser.waitForAndFindElement($driver.By.id('Element'), DefaultTimeout)
//   .then(function(el){
//     el.click();
//   });
		
  log("Completing processing");
};

Initialise('www.sitelandingurl/default.asp', 'www.sitelandingurl/maintenance.asp')
.then(function(){
  return completeProcessing();
})
.thenCatch(function(ex){
  if(ex.message != MaintenanceMessage)
  {
    throw ex;
  }
  else
  {
    log(ex.message);
  }
});

Edited to provide correct promise chaining.

2 Likes

@stefan_garnham thanks for helping the community! :smiley:

After a back end release this weekend I discovered that some sites don’t have holding pages but write text in the home page so the url doesn’t actually change. Here is an update version to allow you to check either the url of the text of the page or an element on the page.

// Script-wide timeout for wait and waitAndFind functions (in ms)
var DefaultTimeout = 60000,
  startTime = new Date(),
  By = $driver.By,
  thisStep = 0,
  MaintenanceMessage = "Site is in maintenance mode";

var log = function(msg) {
  if(typeof msg === "string")
  {
    var elapsedSecs = (new Date() - startTime) / 1000.0;
    console.log('Step ' + thisStep + ': ' + elapsedSecs.toFixed(2) + 's: ' + msg);
    thisStep++;
  }
};

log("Initialising ...");

var assert = require('assert');

var Initialise = function(siteUrl){
  return $browser.getCapabilities()
  .then(function(){

    // User agent set to have monitoring appended to allow for stats filtering
    var UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36 monitoring";

    // Setting User Agent is not then-able, so we do this first (if defined and not default)
    if ((typeof UserAgent !== 'undefined') && (UserAgent != 'default')) {
      $browser.addHeader('User-Agent', UserAgent);
      log("Setting User-Agent to " + UserAgent);
    }
    return $browser.manage().window().maximize()
    .then(function(){
      log("Started");
      return $browser.get(siteUrl);
    });
  });
};

var checkMaintenanceByUrl = function(maintenanceUrl)
{
  if(typeof maintenanceUrl === "string" && maintenanceUrl.length > 0)
  {
    return $browser.getCurrentUrl()
    .then(function(currentUrl){
      log(currentUrl);
      assert.ok(currentUrl != maintenanceUrl, MaintenanceMessage);
    });
  }
};

var checkMaintenanceByText = function(maintenanceDriverBy, maintenanceText)
{
  if(typeof maintenanceDriverBy === "object" && typeof maintenanceText === "string" && maintenanceText.length > 0)
  {
    return $browser.isElementPresent(maintenanceDriverBy)
    .then(function(isPresent){
      return $browser.findElement(maintenanceDriverBy)
      .then(function(el){
        return el.getText()
        .then(function(elementText){
          assert.ok(elementText.indexOf(maintenanceText) == -1, MaintenanceMessage);
        });
      });
    })
    .thenCatch(function(ex){
      log(ex.message);
      if(ex.message.indexOf("no such element: Unable to locate element:") == -1)
      {
        throw ex;
      }
    });
  }  
};    
var completeProcessing = function()
{
//   put your normal processing in here, eg:
//   return $browser.waitForAndFindElement($driver.By.id('Element'), DefaultTimeout)
//   .then(function(el){
//     el.click();
//   });
		
  log("Completing processing");
};

Initialise('www.sitelandingurl/default.asp')
.then(function(){
  return checkMaintenanceByUrl('www.sitelandingurl/maintenance.asp');
})
.then(function(){
  return checkMaintenanceByText('enter your $driver.By function here', "enter the specific maintenance text here");
})
.then(function(){
  return completeProcessing();
})
.thenCatch(function(ex){
  if(ex.message != MaintenanceMessage)
  {
    throw ex;
  }
  else
  {
    log(ex.message);
  }
});
3 Likes

Wow! Thank you again @stefan_garnham - this is very cool and it’s awesome to see you building it out almost live. :smile:

BTW: do you want a custom title? I think I offered before - let me know!

Hi Stephen, I have a question here.
We have multiple sites such as
https://enterprise.telus.com/store/corp-gaceq/
https://enterprise.telus.com/store/corp-gaceq/
https://enterprise.telus.com/store/corp-gaceq/

And this is the Maintenance window screen:
https://enterprise.telus.com/WCM_EOF/StaticPages/outage/

I have written a script based on your above logic, can you please let me know if its correct?

var assert = require(‘assert’);
$browser.getCapabilities().then(function () { })
.then(function(){
return $browser.get(“https://enterprise.telus.com/store/”);
})
.then(function(){
return $browser.getCurrentUrl()
.then(function(currentUrl){
assert.equal(currentUrl, “https://enterprise.telus.com/WCM_EOF/StaticPages/outage/”);
});
})

Hi @Alwin_Anthony.Tiwari - Your script will load the store page and will fail if the url is not the maintenance page. Is that what you are after?

Hi Stefan, I am basically trying to monitor the store page and whenever I get maintenance page it should fail and should not record the uptime figures.

Please correct me if my script is wrong. Thanks.

In that case your assert line should be change to

assert.Ok(currentUrl != "https://enterprise.telus.com/WCM_EOF/StaticPages/outage/", "Site is currently in maintenance");

1 Like

Thanks a lot Stefan, it worked.

I will test this in production when the site is down during maintenance.

1 Like