2

In my protractor test I would also like to verify that the correct ajax calls are made. For example, if I navigate to /chapters I would like to make sure that there was an ajax call to /chapters/documents?page=1

Is it possible to do this with protractor ?

I found this post with some interesting solution, but still I don't see how I can use that such that in my it(...) I can actually test something. For example. browser.addMockModule sounds very promising, but again, what should I do in my beforeEach and how can I verify anything. Help would be appreciated!

Community
  • 1
  • 1
Jeanluca Scaljeri
  • 26,343
  • 56
  • 205
  • 333
  • 2
    Why do you want to test in this way? Why not assert against the results of the call since this is an integration test. This sounds like something better done at the unit level with HTTPBackend than in with protractor – Nick Tomlin Apr 04 '16 at 23:51
  • True, but still it would be nice I think to validate the endpoints called by the application to make sure the correct endpoint are called and no unnecessary calls are made. – Jeanluca Scaljeri Apr 05 '16 at 07:35

1 Answers1

2

You could try something like the following. Add a mock module in your test:

browser.addMockModule('httpInterceptor', function () {
  angular.module('httpInterceptor', []).factory('httpRequestInterceptor',function ($q) {
    return {
      request: function (request) {
        window.handledRequests = window.handledRequests || [];

        window.handledRequests.push({
          url: request.url,
          method: request.method,
          params: request.params,
          data: JSON.stringify(request.data)
        });

        return request || $q.when(request);
      }
    };
  }).config(function ($httpProvider) {
    $httpProvider.interceptors.push('httpInterceptor');
  });
});

Then you can write assertions in your test like this:

browser.controlFlow().execute(function () {
    browser.executeScript(function () {
      return window.handledRequests;
    }).then(function (handledRequests) {
        // assert content of handledRequests
    });
});
szmeti
  • 241
  • 1
  • 7