量角器:不要等待元素出现在Dom

时间:2017-04-09 13:53:26

标签: angularjs testing protractor

我使用量角器结束测试。当我启动测试时,此消息出现在我身上

  

conFusion App E2E Testing菜单0项应该显示第一个评论作者       信息:         失败:找不到使用定位器找到的元素:by.model(" FiltText")

如何让量角器等到元素出现在DOM中?

相应的量角器配置代码是:

  exports.config = {
    allScriptsTimeout:11000,

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

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

 framework: 'jasmine',
 directConnect: true,

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

scenario.js包含e2e测试的代码

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

}); 
  }); 

2 个答案:

答案 0 :(得分:2)

您可以使用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.

因为量角器使用控制流,所以在FiltText可见或时间大于超时之前,此命令不会完成。

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

答案 1 :(得分:0)

有很多方法可以实现这一目标,例如:

  1. 使用{{3}}

  2. 等待几秒钟以确保所有内容都已加载,以下是示例代码:

  3. <强> utils.js

    'use strict';
    
    /**
     * Navigate to an url and wait some seconds
     * @param {string} path The path
     * @param {seconds} [seconds] The number of seconds to wait for
     * @returns {Promise}
     * @see waitSomeSeconds
     */
    function navigateAndWait(path, seconds) {
      return browser.get(path)
        .then(function () {
          // Wait some seconds until page is fully loaded
          return waitSomeSeconds(seconds);
        });
    }
    
    /**
     * Wait some seconds (default is 3)
     * @param {int} [seconds]
     * @returns {Promise}
     */
    function waitSomeSeconds(seconds) {
      return browser.sleep((seconds || 3) * 1000);
    }
    
    
    module.exports = {
      navigateAndWait: navigateAndWait,
      waitSomeSeconds: waitSomeSeconds
    }
    

    然后你可以在测试中使用它:

    **sampleSpec.js**
    
    'use strict';
    
    var util = require('./utils');
    
    describe('Products', function () {
    
    it('should be redirected to products page after login', function (done) {
        util.waitSomeSeconds().then(function() {
          browser.getCurrentUrl().then(function (value) {
            var relativePath = value.substring(value.lastIndexOf('/'));
            expect(relativePath).toEqual('/products');
            done();
          });
        });
    
    it('should navigate to products list', function() {
        return util.navigateAndWait('/products');
      });
    
      it('should display title with correct data', function() {
        expect(element(by.css('h1')).getText()).toBe('Newest products');
      });
    
    });