Question

I am writing end to end test cases for my angular application. I need to check for dates that are in correct format. So I want to inject the 'datefilter', so that I can use it in protractor.

When i 'googled' i found that i can use the browser.executeAsyncScript to get the injector and then use

angular.injector(["ng"]).get("dateFilter"); 

to get the datefilter.

But i am still not able to put them all together in code. Any heads-up would be enough to get me going with the code.

Was it helpful?

Solution

Theoretically you can access your services like this:

browser.executeAsyncScript(function(callback) {
  var service = angular.injector(['MyModule']).get('myService');
  service.query({}, function(data) {
    callback(data);
  });
}).then(function (output) {
  console.log(output);
});

And there is an example:

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

OTHER TIPS

@Lajos Veres, your answer was helpful. Finally used that to get a correct implementation.

browser.executeScript(function() {
  var myDateFilter = angular.injector(["ng"]).get('dateFilter');
  var dateString = myDateFilter(new Date(), 'EEE, dd MMM yy');
  return dateString;
})
.then(function(dateStr) {
    .... //use dateStr for whatever required.
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top