首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Protractor:测试特定的ajax调用

Protractor:测试特定的ajax调用
EN

Stack Overflow用户
提问于 2016-04-05 02:42:46
回答 1查看 664关注 0票数 2

在我的量角器测试中,我还想验证是否进行了正确的ajax调用。例如,如果我导航到/chapters,我希望确保存在对/chapters/documents?page=1的ajax调用

用量角器能做到这一点吗?

我发现this帖子有一些有趣的解决方案,但我仍然不知道如何使用它,以便在我的it(...)中可以实际测试一些东西。例如。browser.addMockModule听起来很有前途,但同样,我应该在我的beforeEach中做什么,以及我如何验证任何东西。如果能帮上忙,我们将不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2017-02-10 01:19:55

您可以尝试下面这样的方法。在您的测试中添加一个模拟模块:

代码语言:javascript
运行
复制
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');
  });
});

然后,您可以在测试中编写断言,如下所示:

代码语言:javascript
运行
复制
browser.controlFlow().execute(function () {
    browser.executeScript(function () {
      return window.handledRequests;
    }).then(function (handledRequests) {
        // assert content of handledRequests
    });
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36410402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档