-
-
Save jfountain/977ac50ce74b1b1cfcaa to your computer and use it in GitHub Desktop.
Actively wait for an element present and displayed up to specTimeoutMs ignoring useless webdriver errors like StaleElementError.
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
/** | |
* Actively wait for an element present and displayed up to specTimeoutMs | |
* ignoring useless webdriver errors like StaleElementError. | |
* | |
* Usage: | |
* Add `require('./waitReady.js');` in your onPrepare block or file. | |
* | |
* @example | |
* expect($('.some-html-class').waitReady()).toBeTruthy(); | |
*/ | |
"use strict"; | |
// Config | |
var specTimeoutMs = 10000; // 10 seconds | |
/** | |
* Current workaround until https://github.com/angular/protractor/issues/1102 | |
* @type {Function} | |
*/ | |
var ElementFinder = $('').constructor; | |
ElementFinder.prototype.waitReady = function(opt_optStr) { | |
var self = this; | |
var driverWaitIterations = 0; | |
var lastWebdriverError; | |
function _throwError() { | |
throw new Error("Expected '" + self.locator().toString() + | |
"' to be present and visible. " + | |
"After " + driverWaitIterations + " driverWaitIterations. " + | |
"Last webdriver error: " + lastWebdriverError); | |
}; | |
function _isPresentError(err) { | |
lastWebdriverError = (err != null) ? err.toString() : err; | |
return false; | |
}; | |
return browser.driver.wait(function() { | |
driverWaitIterations++; | |
if (opt_optStr === 'withRefresh') { | |
// Refresh page after more than some retries | |
if (driverWaitIterations > 7) { | |
_refreshPage(); | |
} | |
} | |
return self.isPresent().then(function(present) { | |
if (present) { | |
return self.isDisplayed().then(function(visible) { | |
lastWebdriverError = 'visible:' + visible; | |
return visible; | |
}, _isPresentError); | |
} else { | |
lastWebdriverError = 'present:' + present; | |
return false; | |
} | |
}, _isPresentError); | |
}, specTimeoutMs).then(function(waitResult) { | |
if (!waitResult) { _throwError() }; | |
return waitResult; | |
}, function(err) { | |
_isPresentError(err); | |
_throwError(); | |
return false; | |
}); | |
}; | |
// Helpers | |
function _refreshPage() { | |
// Swallow useless refresh page webdriver errors | |
browser.navigate().refresh().then(function(){}, function(e){}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment