angularjs - Protractor、AngularJS、Parse——Protractor 不等待 Angular 解析

标签 angularjs protractor angular-routing

我是 Protractor 和 AngularJS 的新手。我在后端使用 Parse。尝试做一个非常简单的测试:

describe('Test', function () {

beforeEach(function () {
  browser.get('index.html#/example')
});

it('should have a button', function () {

   expect(element(by.css('#test321')).isElementPresent()).toBe(true); //fails 

}); ...

测试失败。该元素在 template.html 中:

 ...
<body>
    <button id="test321">stuff</button>
...

它是由angular-route加载的。该路由还从后端加载数据:

...
config(['$routeProvider', function ($routeProvider) {
        $routeProvider.
            when('/example', {
                templateUrl: 'template.html',
                controller: 'templateCtrl',
                resolve: {
                    data: 'dataService' //Data is loaded from Parse. This line causes the problem
                }...

问题是由上面的“数据:”行引起的。如果我去掉那条线,或者让它返回静态结果,它就可以正常工作。此外,如果我移动此元素 index.html,它也能正常工作。

这似乎是一个时间问题。然而,根据文档 Protractor (或特别是 isElementPresent)在定位元素之前等待所有分辨率。

我晕了。非常感谢您的帮助。

更新:根据this这已在 Protractor 1.2 中解决,但我使用的是 1.4。很奇怪。

最佳答案

由于这是一个时间问题,解决方法是等待元素出现,然后再断言它存在:

describe('Test', function () {

beforeEach(function () {
  browser.get('index.html#/example')
});

it('should have a button', function () {
   browser.wait(function() {
       return $('#test321').isPresent(); // keeps waiting until this statement resolves to true
   }, timeToWaitInMilliseconds, 'message to log to console if element is not present after that time');
   expect($('#test321').isPresent()).toBe(true);  

}); ...

关于angularjs - Protractor、AngularJS、Parse——Protractor 不等待 Angular 解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27219335/

相关文章:

angularjs - 单击其他选项卡关闭弹出窗口

android - Ionic/Cordova : How to force app to refresh on start, 即使它在后台?

angularjs - bower-angular-bootstrap3 存储库移到了哪里?

angularjs - 服务器是否需要使用Polymer.dart和/或Angular.Dart运行某些CLIENT端DART应用程序?

Angular 路线 : prefix matching

selenium - restartBrowserBetweenTests 与 onPrepare() 之间的交互

angularjs - Protractor E2E 测试 - 异步 Rest API 调用

javascript - Protractor:如果一个场景失败,如何解决 promise 并停止跳过场景

Angular 路由自动重定向到默认路径

javascript - 无法从服务订阅数据到组件