循环遍历量角器地图功能量角器、遍历、功能、地图

2023-09-14 00:11:42 作者:突然好想你

我有一个角度应用程序,我有表事件的日期,相应的事件描述的时间表。这是HTML源$ C ​​$ C。

I have an angular application where I have a timeline with list event dates and the respective event description. This is the Html source code.

  <!-- timeline -->
  <h4 class="font-thin m-t-lg m-b-lg text-primary-lt">Historical Timeline</h4>
  <p></p>
  <div id="timeline"class="timeline m-l-sm m-r-sm b-info b-l">
    <div  ng-repeat = "timeline in formattedTimelineData | orderBy : '-eventDate'">
      <div class = "tl-item">
        <i class="pull-left timeline-badge {{timeline.class}} "></i>
        <div class="m-l-lg">
          <div id="eventDate{{$index}}" class="timeline-title">{{timeline.eventDate}}</div>
          <p id="eventDescription{{$index}}" class="timeline-body">{{timeline.description}}</p>
        </div>
      </div>
    </div>
  </div>
  <!-- / timeline -->

现在我基本上试图利用量角器,以确保正确的事件日期事件描述相匹配。所以我决定使用地图功能。问题是我想有一个变量x这会告诉我有多少事件有。例如可以有2个事件,6事件等事件是动态动态生成的,你可以通过在HTML code找还会告诉。这里是code为我的测试我写的。

Now I am basically trying to make use protractor to ensure that the correct event date matches the event description. So i decided to use a map function. The issue is I would have a variable x which would tell me how many events there are . For example there can be 2 events, 6 events, etc. Events are dynamically generated dynamically as you can tell by looking at html code also. Here is the code for my test I wrote.

it('FOO TEST', function(){


    var x = 0;
    while(x<4){
   var timeline = element.all(by.css('#timeline')).map(function (timeline) {
       return {
          date: timeline.element(by.css('#eventDate'+x)).getText(),
          events: timeline.element(by.css('#eventDescription'+x)).getText()
     }

   });
      x++
  }

   timeline.then(function (Value) {
    console.log(Value);  
  });

});

问题是,由于某种原因,在命令行中只打印的最后一个事件出来5事件。它不打印的其他事件。我肯定做错了什么。我是全新的诺言所以这里的建议是AP preciated。是的,我想要做像在时间轴每个事件单独的测试。

The issue is that for some reason in command line it only prints the last event out of 5 events. It does not print other events. I am definitely doing something wrong. I am brand new to promises so any suggestion here is appreciated. And yes i want to do like a individual test for each event in the timeline.

推荐答案

问题是在时间表定位: #timeline时间时间轴容器,而你所需要的内部但却难免重复时间表块匹配。这里是你如何能与它们匹配:

The problem is in the timeline locator: #timeline matches the timeline container while you need the inner repetative timeline blocks. Here is how you can match them:

var timeline = element.all(by.repeater('timeline in formattedTimelineData')).map(function (timeline) {
    return {
        date: timeline.element(by.binding('timeline.eventDate')).getText(),
        events: timeline.element(by.binding('timeline.description')).getText()
    }
});

timeline.then(function (timeline) {
    console.log(timeline);
});

然后,您可以遍历像这样的项目:

You can then loop over items like this:

timeline.then(function (timeline) {
    for (var i = 0; i < timeline.length; ++i) {
        // do smth with timeline[i]
    }
});

或者,您可以断言完整的时间表变量,它是一种承诺,可以通过隐式解决的期望进入对象的数组,例如:

Or, you can assert the complete timeline variable which is a promise and can be implicitly resolved by expect into an array of objects, for instance:

expect(timeline).toEqual([
    {
        date: "First date",
        events: "Nothing happened"
    },
    {
        date: "Second date",
        events: "First base"
    },
    {
        date: "Third date",
        events: "Second base"
    }, 
]);