Created
May 15, 2011 15:05
-
-
Save joeriks/973226 to your computer and use it in GitHub Desktop.
Google Sheet script: test urls
This file contains 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
var sendEmailTo = "me@mysite.com"; | |
var emailErrorSubject = "ERROR on your web site"; | |
function isCellEmpty(cellData) { | |
return typeof(cellData) == "string" && cellData == ""; | |
} | |
function autotest_hourly() { | |
checkUrls("Autotest"); | |
} | |
function autoRun_Daily() { | |
checkUrls("Autorun_Daily"); | |
} | |
function checkUrls(sheetName) { | |
// The code below iterates over rows in a sheet and uses the value in | |
// column A as an url, requests it and puts the response code in column B | |
// and the request string as the | |
var doc = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName); | |
var cell = doc.getRange('a1'); | |
var mailMessage = ""; | |
// leftmost resultcolumn | |
var resultColumn = 3; | |
cell.offset(0,resultColumn).setValue(new Date()); | |
cell.offset(0,resultColumn+1).setValue("Content type"); | |
cell.offset(0,resultColumn+2).setValue("Success?"); | |
cell.offset(0,resultColumn+3).setValue("Seconds"); | |
cell.offset(0,resultColumn+4).setValue("Comment"); | |
var historyResultColumn = resultColumn+4; | |
while (!cell.offset(0,historyResultColumn).isBlank() & historyResultColumn<256) | |
{ | |
historyResultColumn +=1; | |
} | |
cell.offset(0,historyResultColumn).setValue(new Date()); | |
cell.offset(0,historyResultColumn+1).setValue("Success?"); | |
for (var i = 1; i < 256; ++i) | |
{ | |
var cellData = cell.offset(i,0).getValue(); | |
if (!isCellEmpty(cellData)) | |
{ | |
var command = cell.offset(i,1).getValue(); | |
var optionData = cell.offset(i,2).getValue(); | |
if (optionData=="") optionData="{}"; | |
var options = Utilities.jsonParse(optionData); | |
var hasError = false; | |
var startTime = new Date(); | |
if (command=="" | command=="GET") | |
{ | |
var responseCode = 404; | |
var requestContentText = ""; | |
var results = ""; | |
var headers; | |
var requestType =""; | |
var contentType = ""; | |
var expectedResponseCode = 200; | |
if (options["response-code"]!=undefined) | |
expectedResponseCode = options["response-code"]; | |
try | |
{ | |
var response = UrlFetchApp.fetch(cellData); | |
responseCode = response.getResponseCode(); | |
requestContentText = response.getContentText(); | |
headers = response.getHeaders(); | |
if (headers!=undefined) | |
contentType=headers["Content-Type"].toLowerCase(); | |
} | |
catch (e) | |
{ | |
requestContentText = e.message; | |
} | |
cell.offset(i,resultColumn).setValue(responseCode); | |
cell.offset(i,historyResultColumn).setValue(responseCode); | |
if (responseCode!=expectedResponseCode) | |
{ | |
hasError = true; | |
results += "Expected response code: " + expectedResponseCode + ". "; | |
} | |
if (contentType.indexOf("html")!=-1) | |
cell.offset(i,resultColumn).setComment(requestContentText); | |
else | |
cell.offset(i,resultColumn).setComment(""); | |
cell.offset(i,resultColumn+1).setValue(contentType); | |
// print results in column | |
var colOffset = resultColumn+4; | |
// not contain the word ERROR | |
var containsError = (requestContentText.toLowerCase().indexOf("error") != -1); | |
//cell.offset(i,colOffset).setValue("Error: " + containsError); | |
if (containsError) | |
{ | |
results += "Error found. "; | |
hasError = true; | |
} | |
if (options["should-contain"]!=undefined) | |
{ | |
// not contain the word ERROR | |
var shouldContain = options["should-contain"].toLowerCase(); | |
var doesContain = (requestContentText.toLowerCase().indexOf(shouldContain) != -1); | |
if (!doesContain) | |
{ | |
results += "Not found: " + options["should-contain"] + ". "; | |
hasError = true; | |
} | |
} | |
if (options["should-not-contain"]!=undefined) | |
{ | |
var shouldNotContain = options["should-not-contain"].toLowerCase(); | |
var doesContain = (requestContentText.toLowerCase().indexOf(shouldNotContain) != -1); | |
if (doesContain) | |
{ | |
results += "Found: " + options["should-not-contain"] + ". "; | |
hasError = true; | |
} | |
} | |
cell.offset(i,colOffset).setValue(results); | |
} | |
// timer | |
var endTime = new Date(); | |
var timeDiff = endTime-startTime; | |
// success? (no errors) | |
cell.offset(i,resultColumn+2).setValue(!hasError); | |
cell.offset(i,historyResultColumn+1).setValue(!hasError); | |
if (hasError) | |
{ | |
cell.offset(i,resultColumn+2).setBackgroundColor("red"); | |
cell.offset(i,historyResultColumn+1).setBackgroundColor("red"); | |
mailMessage += "FEL på " + cellData + ":" + results; | |
} | |
else | |
{ | |
cell.offset(i,resultColumn+2).setBackgroundColor("green"); | |
cell.offset(i,historyResultColumn+1).setBackgroundColor("green"); | |
} | |
// time spent (in seconds) | |
cell.offset(i,resultColumn+3).setValue(timeDiff/1000); | |
} | |
else | |
{ | |
break; | |
} | |
} | |
if (mailMessage!="") | |
{ | |
MailApp.sendEmail(sendEmailTo, emailErrorSubject, mailMessage); | |
} | |
} | |
function getTime() | |
{ | |
var startTime = new Date(); | |
Browser.msgBox(startTime); | |
var endTime = new Date(); | |
var timeDiff = endTime-startTime; | |
Browser.msgBox(timeDiff); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment