Created
November 23, 2014 18:09
-
-
Save bregenspan/e874f413bdcb62933a80 to your computer and use it in GitHub Desktop.
WD.js JS condition asserter that retries on-error.
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
/* Version of wd.asserters.jsCondition that retries on-error. | |
This is useful for cases where you need to assert a condition at a time when the page might | |
be reloading -- in such a case, the assertion could reach the browser at a time when the document | |
is not yet ready, triggering an error. This catches the error and retries. | |
Usage: | |
> browser.waitFor(jsConditionWithRetries('window.FOOBAR'), 5000, 500); | |
(Requires Selenium Server >= 2.44.0) | |
*/ | |
function jsConditionWithRetries(jsConditionExpr, retryCount, retryInterval) { | |
retryCount = retryCount || 10; // number of times to retry on command failure | |
retryInterval = retryInterval || 500; // ms to wait before retries | |
return new asserters.Asserter(function (browser, cb) { | |
function runConditionWithRetries(retries) { | |
browser.safeEval.apply(browser, [jsConditionExpr, function (err, res) { | |
if (err) { | |
if (retries) { | |
console.warn('Encountered an issue making a JS assertion in the browser, retrying in ' + retryInterval + 'ms'); | |
setTimeout(function () { | |
runConditionWithRetries(retries - 1); | |
}, retryInterval); | |
} else { | |
return cb(err); | |
} | |
} else { | |
cb(null, res, res); | |
} | |
}]); | |
} | |
runConditionWithRetries(retryCount); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment