Created
August 31, 2015 22:19
-
-
Save drorm/1baf721569c78fed33a0 to your computer and use it in GitHub Desktop.
Casper function to reload a page until a condition is met
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
/** | |
* Reload a page until a selector satisfies a condition | |
* is changed to a different value. | |
* | |
* @param String selector The CSS selector we're looking at | |
* @param Number delay How long to wait till we reload the page | |
* @param Function testFunc A function to determine if we found what we're looking for | |
* @param String result The text found by the selector that is passed to the above function | |
* @return Does not return anything | |
*/ | |
function reloadUntil(selector, delay, testFunc) { | |
casper.then(function() { | |
this.reload(function() { | |
var result = this.fetchText(selector); | |
if(!testFunc(result)) { | |
this.wait(delay, function() { | |
reloadUntil(selector, delay, testFunc); | |
}); | |
} | |
}); | |
}); | |
} | |
/* | |
* Example use: | |
*/ | |
casper.then(function(){ | |
reloadUntil('#foo', 2500, function(param){ | |
console.log(param); | |
return (param > 50); //done when #foo is > than 50, | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment