Testing AngularJS with Selenium

0
0

I have SPA application on on stack ASP MVC + AngularJS and i’d like to test UI.
For a now i’m trying for Selenium with PhantomJS and WebKit drivers.

Testing page – view with single element – the list of <li> which loads dynamically from server and bind by Angular.

<div id="items">
<li>text</li>
<li>text2</li>
</div>

I’m trying to pass test

_driver.FindElements(By.TagName('li'))

The problem is that at this moment there is no elements loaded, and _driver.PageSource doesn’t contain elements too.

How can i wait for items loaded? Please do not suggest Thread.Sleep()

  • You must to post comments
0
0

This will wait for page loads / jquery.ajax (if present) and $http calls, and any accompanying digest/render cycle, throw it in a utility function and wait away.

/* C# Example
var pageLoadWait = new WebDriverWait(WebDriver, TimeSpan.FromSeconds(timeout));
pageLoadWait.Until<bool>(
(driver) =>
{
return (bool)JS.ExecuteScript(
@"*/

try {
if (document.readyState !== 'complete') {
return false; // Page not loaded yet
}
if (window.jQuery) {
if (window.jQuery.active) {
return false;
} else if (window.jQuery.ajax && window.jQuery.ajax.active) {
return false;
}
}
if (window.angular) {
if (!window.qa) {
// Used to track the render cycle finish after loading is complete
window
.qa = {
doneRendering
: false
};
}
// Get the angular injector for this app (change element if necessary)
var injector = window.angular.element('body').injector();
// Store providers to use for these checks
var $rootScope = injector.get('$rootScope');
var $http = injector.get('$http');
var $timeout = injector.get('$timeout');
// Check if digest
if ($rootScope.$$phase === '$apply' || $rootScope.$$phase === '$digest' || $http.pendingRequests.length !== 0) {
window
.qa.doneRendering = false;
return false; // Angular digesting or loading data
}
if (!window.qa.doneRendering) {
// Set timeout to mark angular rendering as finished
$timeout
(function() {
window
.qa.doneRendering = true;
}, 0);
return false;
}
}
return true;
} catch (ex) {
return false;
}
/*");
});*/
  • You must to post comments
Showing 1 result
Your Answer
Post as a guest by filling out the fields below or if you already have an account.
Name*
E-mail*
Website