angularjs - Protractor :don't wait for elements to appears in Dom

标签 angularjs testing protractor

我使用 Protractor 进行端到端测试。当我开始测试时,我会看到这条消息

conFusion App E2E Testing menu 0 item should show the first comment author as Message: Failed: No element found using locator: by.model("FiltText")

如何让 Protractor 等到元素出现在 DOM 中?

对应的 Protractor 配置代码为:

  exports.config = {
    allScriptsTimeout:11000,

    specs: [
      'e2e/*.js'
      ],
  capabilities: {
     'browserName': 'chrome'
   },

baseUrl: 'http://localhost:3001/',

 framework: 'jasmine',
 directConnect: true,

 jasmineNodeOpts: {
    showColors: true,
    defaultTimeoutInterval: 30000
 }
};

包含端到端测试的scenarios.js代码

 describe('menu 0 item', function() {
beforeEach(function() {

  browser.get('index.html#/menu/0');


});

it('should have a name', function() {
      var name = element(by.binding('dish.name'));
      expect(name.getText()).
         toEqual('Uthapizza Hot $4.99');
});

it('should show the number of comments as', function() {

    expect(element.all(by.repeater('comment in dish.comments'))
        .count()).toEqual(5);


});

it('should show the first comment author as', function() {
      element(by.model('FiltText')).sendKeys('author');

      expect(element.all(by.repeater('comment in dish.comments'))
        .count()).toEqual(5);

      var author = element.all(by.repeater('comment in dish.comments'))
                  .first().element(by.binding('comment.author'));

      expect(author.getText()).toContain('25 Cent');

}); 
  }); 

最佳答案

您可以使用 ExpectedConditions:

var EC = protractor.ExpectedConditions;
var timeout = 5000; // in miliseconds
// Waits for the element to be present on the dom.
browser.wait(EC.presenceOf(element(by.model("FiltText"))), timeout);
// Here you can safely use your element, because here it is guaranted to be present.

因为 Protractor 使用控制流,此命令将不会完成,直到 FiltText 可见或时间大于超时。

更多信息在这里: http://www.protractortest.org/#/api?view=ProtractorExpectedConditions

关于angularjs - Protractor :don't wait for elements to appears in Dom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43307515/

相关文章:

c# - 使用 Protractor 的 C# 单元测试中的无效操作异常

javascript - 使用 ngMock 对外部站点进行无后端测试

angularjs - 如何从 Protractor 中的元素获取父级中的类?

javascript - 如何将动态文件名传递给 ng-include

javascript - Angularjs 因素不起作用

angularjs - Ionic 2 中的 static get parameters() 目的是什么?

angularjs - 在ionic上的iframe中单击youtube图标时,防止重新加载/滚动到顶部

maven - 如何使用 'mvn test' 运行 Cucumber 测试

javascript - Jasmine 的错误方法http测试( Angular 7)

unit-testing - Nodejs 单元测试网址