Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 量角器:don';不要等待元素出现在Dom中_Angularjs_Testing_Protractor - Fatal编程技术网

Angularjs 量角器:don';不要等待元素出现在Dom中

Angularjs 量角器:don';不要等待元素出现在Dom中,angularjs,testing,protractor,Angularjs,Testing,Protractor,我使用量角器进行端到端测试。启动测试时,我会看到此消息 混淆应用程序E2E测试菜单0项应显示第一个注释作者 信息: 失败:使用定位器by.model(“FiltText”)找不到元素 如何使量角器等待元素出现在DOM中 相应的量角器配置代码为: exports.config = { allScriptsTimeout:11000, specs: [ 'e2e/*.js' ], capabilities: { 'browserName':

我使用量角器进行端到端测试。启动测试时,我会看到此消息

混淆应用程序E2E测试菜单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
 }
};
包含e2e测试的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.
由于量角器使用控制流,因此在
filtext
可见或时间大于超时之前,此命令不会完成

更多信息请点击此处:

您可以使用预期条件:

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.
由于量角器使用控制流,因此在
filtext
可见或时间大于超时之前,此命令不会完成

更多信息请点击此处:

实现这一点的方法有很多,例如:

  • 使用

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

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

    实现这一点的方法有很多,例如:

  • 使用

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

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

    错误中测试名称的可能重复项不在您提供的规范中。代码在哪里?我添加了测试代码。请检查它。错误中可能重复的测试名称不在您提供的规范中。代码在哪里?我添加了测试代码。请检查一下。我照你的建议做了。但没用。在我看来,这条消息是:失败:等待在5001msI实际添加代码之后超时在此代码之前:元素(by.model('filtext')).sendKeys('author');因为当我把它放在后面的时候,第一个问题再次出现,你被超时了,因为要么你的元素需要5秒以上才能繁殖,要么你的元素根本没有繁殖。您必须测量
    filtext
    在屏幕上显示所需的时间。如果需要10秒,则会出现超时错误我如何测量FiltText所需的时间?打开页面时与打开测试时一样。只需键入url,打开页面并计算大约经过了多少秒。或者将超时时间增加到60秒。如果你得到超时,那就意味着你的元素可能根本没有加载。我按照你的建议做了。但是没有起作用。在我看来,这条消息是:失败:等待在5001msI实际添加代码之后超时在此代码之前:元素(by.model('filtext')).sendKeys('author');因为当我把它放在后面的时候,第一个问题再次出现,你被超时了,因为要么你的元素需要5秒以上才能繁殖,要么你的元素根本没有繁殖。您必须测量
    filtext
    在屏幕上显示所需的时间。如果需要10秒,则会出现超时错误我如何测量FiltText所需的时间?打开页面时与打开测试时一样。只需键入url,打开页面并计算大约经过了多少秒。或者将超时时间增加到60秒。如果您得到超时,则意味着您的元素可能根本不加载。