Skip to content

Instantly share code, notes, and snippets.

@rotten77
Last active September 16, 2020 11:02
Show Gist options
  • Save rotten77/876d0093cf52a99393384cb88da78a83 to your computer and use it in GitHub Desktop.
Save rotten77/876d0093cf52a99393384cb88da78a83 to your computer and use it in GitHub Desktop.
Measure time between click and expected result (defined by Xpath query)
// -- SETUP -----------
// demo:
// 1) go to https://ztlkl.cz/
// 2) open browser console, paste script (press Enter key to run it)
// 3) click on "#testování" link
// click on starter to start measure
var starter = '//a[@href="#f=stitek:testovani"]';
// define expected elements - XPath and count (0 means any)
var result = '//div[@class="project-description-meta"]';
var resultCount = 2;
var resultTimeout = 10;
// -- EXECUTION -------
var refreshInterval;
var startTime;
var starterXpath = document.evaluate(starter, document, null, XPathResult.ANY_TYPE, null );
var starterElement;
while(sEl = starterXpath.iterateNext()) {
starterElement = sEl;
starterElement.addEventListener('click', startStopwatch);
}
var nodesActual = 0;
if(refreshInterval) window.clearInterval(refreshInterval);
console.clear();
function startStopwatch() {
refreshInterval = setInterval(checkFunction,100);
starterElement.removeEventListener('click', startStopwatch);
startTime = performance.now();
console.log('%c0.000s: Action fired ('+startTime+')', 'color:blue');
}
function checkFunction() {
var resultElements = document.evaluate(result, document, null, XPathResult.ANY_TYPE, null );
var node, nodes = [];
while(node = resultElements.iterateNext()) nodes.push(node);
var timeElapsed = ((performance.now() - startTime) / 1000).toFixed(3);
if(typeof nodes !== 'undefined') {
if(nodesActual!=nodes.length) {
nodesActual = nodes.length;
console.log('%c'+timeElapsed + 's: Nodes:' + nodesActual + ' ('+performance.now()+')', 'color:gray');
}
if(nodes.length == resultCount || (resultCount == 0 && nodes.length>0)) {
window.clearInterval(refreshInterval);
console.log('%c' + timeElapsed + 's: Expected nodes ('+(resultCount>0 ? resultCount : '0+')+') presented ('+performance.now()+')', 'color:green');
}
}
if(timeElapsed>resultTimeout) {console.log('%c' + timeElapsed + 's: Timeout expired', 'color:red');window.clearInterval(refreshInterval);}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment