简体   繁体   English

如何在量角器测试中使用lodash _.find?

[英]How can I use lodash _.find in my Protractor test?

I want to use the Lodash find function to make my Protractor tests more robust. 我想使用Lodash find功能使我的量角器测试更可靠。

Instead of 代替

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

Something like 就像是

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

My reasoning is so that, if the order of items in the page changes, the test does not break as it can still find the element with the data it is looking for. 我的理由是,如果页面中项目的顺序发生变化,则测试不会中断,因为它仍然可以找到具有所要查找数据的元素。

You almost got it: 您几乎明白了:

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

Which can be read as: Find me the first topic in topics that has a name property which equals 'Maths'. 可以读为:在主题中找到名称属性等于“ Maths”的第一个主题。

Did you try using filter ? 您是否尝试使用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'
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM