How to load a webpage repeatedly using grease-monkey and check for an element?
Basically, I want to repeatedly load a URL say http://xyz.co.in and then check for the value of a particular element, just like a testing process and monitor server logs for that flow.
I'm trying to mi开发者_StackOverflow中文版mic a part of production traffic by hitting the same host repeatedly to do further processing. How best can I go about this ?
Flow
Load a webpage -> Monitor Server Logs -> Monitor certain element values on frontend -> Repeat again.
Greasemonkey is not the best tool for load testing web pages/servers/apps.
But here is a script that loads a page repeatedly and checks for an element:
// ==UserScript==
// @include http://xyz.co.in/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js
// ==/UserScript==
$(document).ready (Greasemonkey_main);
function Greasemonkey_main ()
{
do
{
var TargetNode = $("#TargetNode"); //-- Look for node with id, "TargetNode".
if (TargetNode && TargetNode.length)
{
//--- Maybe also check contents:
if (/Node contents to search for/i.test (TargetNode.text () ) )
{
alert ("We found what we're looking for");
break;
}
}
//--- Failsafe check on number of reloads
var NumReloads = parseInt (document.location.search.replace (/.*num_gm_reloads=(\d+).*/, "$1") )
if (NumReloads > 2)
{
alert ("After 2 reloads, we still didn't find what we were looking for.");
break;
}
//--- We neither found the stop code nor exhausted our retries, so reload the page.
if (NumReloads)
NumReloads++;
else
NumReloads = 1;
var TargetURL = window.location.href;
//--- Strip old URL param, if any. Note that it will always be at the end.
TargetURL = TargetURL.replace ( /(.*?)(?:\?|&)?num_gm_reloads=\d+(.*)/, "$1$2" );
var ParamSep = /\?/.test (TargetURL) ? "&" : "?";
TargetURL = TargetURL + ParamSep + 'num_gm_reloads=' + NumReloads;
window.location.href = TargetURL; //-- Reload the page.
} while (0)
}
I figured out that Selenium is the best testing tool for such scenarios.
Install Selenium IDE as FF addon and try below commands in series:-
Command | Target | Value
1) open | _url_ | _blank_ |
2) waitForElementPresent | css=_selector_ or xpath=_selector_ | _time in ms_ |
3) verifyElementPresent | css=_selector_ or xpath=_selector_ | _blank_ |
You can skip step 2 if the element to be verified is available on page load than on a deferred AJAX call.
If any of above steps fail, then its a failure else success. You can schedule this to run 'n' number of times.
精彩评论