Evaluating Angular Expressions in Protractor Tests

Reading Time: 1 minute

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

As you know, in Angular different elements are bound to different $scopes. In Protractor, you can actually ask for the evaluated value of an expression within the context of a particular element.

For example,

Say we had a place in our javascript where we’d specified:

.controller('MyController', function($scope) {
  $scope.bar = "Nate";

And somewhere we had an element

<div ng-controller="MyController">
  <div class="foo"><!-- stuff in here --></div>

We could try to grab the value in our Protractor test like this:


would return whatever {{bar}} is in the scope of the element with class foo, in this case "Nate".

This is useful because we’re able to use Protractor to test the value of data within a particular concept rather than always relying on a particular css or XPath.

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!