http://joeriks.wordpress.com/2011/05/17/coding-my-own-automated-web-page-tests-with-google-apps-script-and-a-spreadsheet/
function isCellEmpty(cellData) { return typeof(cellData) == "string" && cellData == ""; } function checkUrls() { // 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 comment var doc = SpreadsheetApp.getActiveSpreadsheet(); 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"); for (var i = 1; i < 20; ++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 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); 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); if (hasError) { cell.offset(i,resultColumn+2).setBackgroundColor("red"); mailMessage += "ERROR on " + cellData + ":" + results; } else cell.offset(i,resultColumn+2).setBackgroundColor("green"); // time spent (in seconds) cell.offset(i,resultColumn+3).setValue(timeDiff/1000); } else { break; } } if (mailMessage!="") { MailApp.sendEmail("me@myaddress.com", "ERROR on your web ", mailMessage); } } function getTime() { var startTime = new Date(); Browser.msgBox(startTime); var endTime = new Date(); var timeDiff = endTime-startTime; Browser.msgBox(timeDiff); }
My profiles:

Contact me:
jurijs.zarkovs@gmail.com
jzharkov
Get this email app! 
Contact me:
![]() | Latest tweet: Все рассуждения мужчин не стоят одного чувства женщины.Вольтер |



Комментариев нет:
Отправить комментарий