Monday, May 13, 2024
 Popular · Latest · Hot · Upcoming
168
rated 0 times [  173] [ 5]  / answers: 1 / hits: 22016  / 11 Years ago, mon, december 16, 2013, 12:00:00

Is it possible to access angular within your protractor tests like you do in unit testing?



Use case is that I have a service that transforms text and I want to access that service to transform some data within the actual test script. I know there is the addMockModule method in protractor but I cannot figure out how to use it for this purpose.



Would appreciate any help!


More From » angularjs

 Answers
25

There is a function called evaluate(). Find an element in the dom and then run the expression.



For example. If you want to count the number of todos in the http://angularjs.org/ website (under Add Some Control), do this:



Open 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)
});


It should give you a 2



You can also use executeAsyncScript



browser.executeAsyncScript(function(callback) {
// Here we use document.body, but your app may live under a different
// element.
var service = angular.element(document.body)
.injector()
.get('myService');
service.query({}, function(data) {
callback(data);
});
}).then(function (output) {
console.log(output);
});


See an example: https://github.com/andresdominguez/protractor-meetup/blob/master/test/e2e/api-helper.js


[#73711] Friday, December 13, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kylanalis

Total Points: 438
Total Questions: 85
Total Answers: 102

Location: Barbados
Member since Sun, Nov 27, 2022
1 Year ago
kylanalis questions
Sat, Oct 2, 21, 00:00, 3 Years ago
Tue, Oct 13, 20, 00:00, 4 Years ago
Thu, Feb 13, 20, 00:00, 4 Years ago
Tue, Jan 7, 20, 00:00, 4 Years ago
;