Protractor: test for specific ajax calls

时间:2016-04-04 18:42:46

标签: javascript rest protractor angularjs-e2e

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!

1 个答案:

答案 0 :(得分:2)

您可以尝试以下内容。在测试中添加模拟模块:

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

然后你可以在你的测试中写下断言:

browser.controlFlow().execute(function () {
    browser.executeScript(function () {
      return window.handledRequests;
    }).then(function (handledRequests) {
        // assert content of handledRequests
    });
});