Executing Async Javascript in Protractor

This post is part of a ten-part series on how to write better Protractor tests. You can find the rest of the series here.

Earlier we talked about how you can inject Angular modules for HTTP mocking. And last time we learned how to execute arbitrary javascript in our tests, but that javascript was sync code. What if we need to perform async code instead?

For that you’ll use the executeAsyncScript function:

The executeAsyncScript function takes one or more arguments.

  • The first argument is a function which will be called
  • The second+ arguments will be passed as arguments to the function in the first argument.

E.g. notice above we have person defined in the outermost scope. We pass person as the second argument to browser.executeAsyncScript and it gets passed as the first argument to the internal function. The last argument of the internal function is always the callback which you call with the result.

Learn Protractor in a few hours with our online course

Protractor is a one of the best ways to ensure your app is working correctly because you can write code that acts like a real user. But Protractor can also be confusing and difficult to work with when you're just starting out. We've put together an online course that will help you become a master at Protractor.

You'll save tons of time and frustration learning Protractor because we've worked out everything you need to know. Become a Protractor testing master today!