angularjs - 在 Protractor 测试中等待服务器响应(和检查值)

标签 angularjs server protractor angularjs-e2e e2e-testing

我的站点中有一个页面向服务器发出请求,然后使用结果来填充表。

我该如何为此编写 e2e 测试?我在计算如何等待服务器响应以及如何从响应中获取数据时遇到了麻烦,因此我可以根据表内容对其进行检查。

这是我的 Angular Controller 的一个简化示例:

angular.module("myApp").controller("myCtrl", function(MyService) {

   MyService.getData()
        .then(function (response) {
            $scope.data = response;
        });
});

然后 html 页面循环遍历 $scope.data 中的项目以填充表格。

谢谢你的帮助!

最佳答案

通常,您不必担心这个,因为 protractor知道 angular 何时准备就绪并加载页面。换句话说,it syncs with Angular itself :

You no longer need to add waits and sleeps to your test. Protractor can automatically execute the next step in your test the moment the webpage finishes pending tasks, so you don’t have to worry about waiting for your test and webpage to sync.



但在某些情况下,您需要通过 browser.wait() 明确等待某些预期条件得到满足。 .例如,您可以等到特定元素出现:
var EC = protractor.ExpectedConditions;
var elm = element(by.css("#myid"));

browser.wait(EC.presenceOf(elm), 10000);

有一大套built-in Expected Conditions , 一探究竟。

在您的情况下究竟要等待什么取决于浏览器收到该服​​务器响应时浏览器中发生的事情 - 可能有一些表格出现,或者有额外的表格行添加到现有表格等。

关于angularjs - 在 Protractor 测试中等待服务器响应(和检查值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32773234/

相关文章:

javascript - 我们应该将 jQuery 与 AngularJS 一起使用吗?

reactjs - npm start 如何指定index.js以外的启动文件

node.js - 为什么 Protractor 调试器范围内没有任何内容?

angularjs - Browser.manage().addCookie() 返回错误丢失 'name'

javascript - 从 Angularjs 中的 Controller 获取元素 id

javascript - 如何使用 Angular 计算子项总数并在父项中显示

angularjs - Protractor - 选择当前元素的下一个兄弟

linux - 在专用主机上安装 Apache 服务器

java - 使用 ObjectInputStream 检查 Socket 中是否有可用对象

javascript - 等待一个或另一个 Promise