【问题标题】:How can I use lodash _.find in my Protractor test?如何在量角器测试中使用 lodash _.find?
【发布时间】:2015-05-10 18:03:57
【问题描述】:

我想使用 Lodash find 函数使我的 Protractor 测试更加稳健。

代替

    element.all (by.repeater ('topic in topics')).then (function (topics) {

        expect (topics[1].element (by.binding ('topic.name')).getText()).toEqual ('Maths');
        expect (topics[1].element (by.binding ('topic.description')).getText()).toEqual ('2 + 2 = 4');  
    });

类似

    element.all (by.repeater ('topic in topics')).then (function (topics) {

        var mathsTopic = _.find (topics, 'topic.name', 'Maths');
        expect (mathsTopic.element (by.binding ('topic.description')).getText()).toEqual ('2 + 2 = 4');    
    });

我的理由是,如果页面中项目的顺序发生变化,测试不会中断,因为它仍然可以找到包含它正在寻找的数据的元素。

【问题讨论】:

    标签: angularjs node.js protractor lodash


    【解决方案1】:

    您是否尝试过使用filter

    var topic = element.all(by.repeater('topic in topics'))
        .filter(function (row) {
          return row.element(by.binding('topic.name')).getText().then(function(name) {
            return name === 'Maths';
          });
        })
       .first()
       .map(function(row) {
         return {
           name: row.element(by.binding('topic.name')).getText()),
           description: row.element(by.binding('topic.description')).getText())
         };
       });
    
    expect(topic).toEqual({
      name: 'Maths',
      description: '2 + 2 = 4'
    });
    

    【讨论】:

      【解决方案2】:

      你几乎明白了:

      var mathsTopic = _.find(topics, { name: 'Maths' });
      

      可以理解为:在主题中找到第一个具有等于“数学”的名称属性的主题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-21
        • 2013-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多