Selenium wait for digest loop to complete

Digest loop is the mechanism that angular uses to check for changes to the model and update the view accordingly. This is asynchronous and there by causes a problem when writing end to end selenium tests.

Selenium has come up with protractor for testing angular applications. I have not given it a shot. We solved the problem of having to wait for atleast one digest loop to complete after the page loads by using selenium’s wait for async script hook.

 ((JavascriptExecutor) webDriver).executeAsyncScript(
"angular.element([DOM element]).injector().get('$timeout')(arguments[arguments.length - 1]);"
, ""
);

Selenium waits till the Javascript executes a callback that it provides which will be the last element of the arguments passed to the script.

The timeout service of angular executes the call back passed to it after the completion of atleast one digest cycle if you don’t pass a time out, which is the case here.

Together these ensure that angular had a chance to process the current route atleast once before you do assertions on the elements using selenium webdriver.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s