Last active
October 13, 2021 15:59
-
-
Save sschwartzman/4736a61c403985e1eaa396de079c37cc to your computer and use it in GitHub Desktop.
New Relic Synthetics script to retrieve the latest version of New Relic Browser and its release dates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Script Name: {Browser - Check current release} | |
* | |
* Generated using New Relic Synthetics Formatter for Katalon | |
* | |
* Feel free to explore, or check out the full documentation | |
* https://docs.newrelic.com/docs/synthetics/new-relic-synthetics/scripting-monitors/writing-scripted-browsers | |
* for details. | |
*/ | |
/** CONFIGURATIONS **/ | |
// Theshold for duration of entire script - fails test if script lasts longer than X (in ms) | |
var ScriptTimeout = 180000; | |
// Script-wide timeout for all wait and waitAndFind functions (in ms) | |
var DefaultTimeout = 30000; | |
// Change to any User Agent you want to use. | |
// Leave as "default" or empty to use the Synthetics default. | |
var UserAgent = "default"; | |
var lvtext = "Latest Version"; | |
/** HELPER VARIABLES AND FUNCTIONS **/ | |
const assert = require('assert'), | |
By = $driver.By, | |
browser = $browser.manage() | |
/** BEGINNING OF SCRIPT **/ | |
console.log('Starting synthetics script: {Browser - Check current release}'); | |
console.log('Default timeout is set to ' + (DefaultTimeout / 1000) + ' seconds'); | |
// Setting User Agent is not then-able, so we do this first (if defined and not default) | |
if (UserAgent && (0 !== UserAgent.trim().length) && (UserAgent != 'default')) { | |
$browser.addHeader('User-Agent', UserAgent); | |
console.log('Setting User-Agent to ' + UserAgent); | |
} | |
// Get browser capabilities and do nothing with it, so that we start with a then-able command | |
$browser.getCapabilities().then(function() {}) | |
.then(() => { | |
logger.log(1, "https://docs.newrelic.com/docs/release-notes/new-relic-browser-release-notes/browser-agent-release-notes/"); | |
return $browser.get("https://docs.newrelic.com/docs/release-notes/new-relic-browser-release-notes/browser-agent-release-notes/"); | |
}) | |
.then(() => { | |
logger.log(2, "clickElement xpath=//div[2]/div/a"); | |
return $browser.waitForAndFindElement(By.xpath("//div[2]/div/a"), DefaultTimeout) | |
.then((el) => { | |
el.getText().then((text) => { | |
console.log(lvtext + " in Docs: " + text); | |
$util.insights.set(lvtext + " in Docs", Number(text.match(/\d+/)[0])); | |
}) | |
return el.click(); | |
}) | |
}) | |
.then(() => { | |
logger.log(3, "getText xpath=//main/div/span"); | |
return $browser.waitForAndFindElement(By.xpath("//main/div/span"), DefaultTimeout) | |
.then((el) => { | |
return el.getText() | |
.then((text) => { | |
console.log("Release Date in Docs: " + text); | |
$util.insights.set("Release Date in Docs", Date.parse(text.trim())); | |
}) | |
}) | |
}) | |
.then(() => { | |
logger.log(4, "https://github.com/newrelic/newrelic-browser-agent/blob/main/CHANGELOG.md"); | |
return $browser.get("https://github.com/newrelic/newrelic-browser-agent/blob/main/CHANGELOG.md"); | |
}) | |
.then(() => { | |
logger.log(5, "getText xpath=//div[@id='readme']/article/h2"); | |
return $browser.waitForAndFindElement(By.xpath("//div[@id='readme']/article/h2"), DefaultTimeout) | |
.then((el) => { | |
return el.getText() | |
.then((text) => { | |
console.log(lvtext + " : " + text); | |
$util.insights.set(lvtext, Number(text.match(/\d+/)[0])); | |
}) | |
}) | |
}) | |
.then(() => { | |
logger.log(6, "getText xpath=//div[@id='readme']/article/ul"); | |
return $browser.waitForAndFindElement(By.xpath("//div[@id='readme']/article/ul"), DefaultTimeout) | |
.then((el) => { | |
return el.getText() | |
.then((text) => { | |
console.log(lvtext + " Details:\n" + text); | |
var dateLines = text.match(/[^\r\n]+/g); | |
for (var dateLine of dateLines) { | |
var dateLineSplit = dateLine.split(":"); | |
var thisMilestone = dateLineSplit[0].trim(); | |
var thisDate = new Date(dateLineSplit[1].trim()).getTime(); | |
console.log(lvtext + " Detail: " + thisMilestone + " : " + thisDate); | |
$util.insights.set(thisMilestone, thisDate); | |
} | |
}) | |
}) | |
}) | |
.then(() => { | |
logger.end(); | |
console.log('Browser script execution SUCCEEDED.'); | |
}, function(err) { | |
logger.end(); | |
console.log('Browser script execution FAILED.'); | |
throw (err); | |
}); | |
//** Export Functions | |
const logger = (function(timeout = 3000, mode = 'production') { | |
var startTime = Date.now(), | |
stepStartTime = Date.now(), | |
prevMsg = '', | |
prevStep = 0; | |
if (typeof $util == 'undefined') { | |
$util = { | |
insights: { | |
set: (msg) => { | |
console.log(`dryRun: sending to Insights using ${msg}`) | |
} | |
} | |
} | |
} | |
function log(thisStep, thisMsg) { | |
if (thisStep > prevStep && prevStep != 0) { | |
end() | |
} | |
stepStartTime = Date.now() - startTime; | |
if (mode != "production") { | |
stepStartTime = 0 | |
} | |
console.log(`Step ${thisStep}: ${thisMsg} STARTED at ${stepStartTime}ms.`); | |
prevMsg = thisMsg; | |
prevStep = thisStep; | |
} | |
function end() { | |
var totalTimeElapsed = Date.now() - startTime; | |
var prevStepTimeElapsed = totalTimeElapsed - stepStartTime; | |
if (mode != 'production') { | |
prevStepTimeElapsed = 0 | |
totalTimeElapsed = 0 | |
} | |
console.log(`Step ${prevStep}: ${prevMsg} FINISHED. It took ${prevStepTimeElapsed}ms to complete.`); | |
$util.insights.set(`Step ${prevStep}: ${prevMsg}`, prevStepTimeElapsed); | |
if (timeout > 0 && totalTimeElapsed > timeout) { | |
throw new Error('Script timed out. ' + totalTimeElapsed + 'ms is longer than script timeout threshold of ' + timeout + 'ms.'); | |
} | |
} | |
return { | |
log, | |
end | |
} | |
})(ScriptTimeout) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment