One of the key features in Protractor is the evaluate() function, which allows you to find an element in the DOM and then execute a specified expression.
For instance, if you're looking to count the number of todos on the http://angularjs.org/ website (located under Add Some Control), you can follow these steps:
Begin by opening the element explorer in Protractor
./node_modules/protractor/bin/elementexplorer.js
browser.get('http://angularjs.org/')
element(by.model('todoText')).evaluate('todos.length').
then(function(count) {
console.log(count)
});
This will output a count of 2
Another option is to use executeAsyncScript
browser.executeAsyncScript(function(callback) {
// In this example, we're utilizing document.body, but your application may be nested within another element.
var service = angular.element(document.body)
.injector()
.get('myService');
service.query({}, function(data) {
callback(data);
});
}).then(function (output) {
console.log(output);
});
Check out an instance here: https://github.com/andresdominguez/protractor-meetup/blob/master/test/e2e/api-helper.js