Test for the number of elements using Jasmine in A

2019-06-27 12:21发布

I am writing an end to end test using Jasmine for AngularJS. I am using Protractor to run the test. I have the following markup

<ul class="phone-thumbs">
  <li ng-repeat="img in phone.images">
    <img ng-src="{{img}}">
  </li>
</ul>

And I want to test that for a particular page instance I have four of these images. Here's what I have so far

describe('Phone detail view', function() {

    beforeEach(function() {
      browser.get('app/index.html#/phones/nexus-s');
    });

    it('should display four thumbnails on the nexus-s page', function() {
      expect(element(by.css('.phone-thumbs li img')).length()).toBe(4);
    });
  });

Problem is that I get an error saying

TypeError: Object #<Object> has no method 'length'

Where am I going wrong?

2条回答
男人必须洒脱
2楼-- · 2019-06-27 12:35

For anyone who needs to know this. Here's how I did it.

it('should display four thumbnails on the nexus-s page', function() {
      var images = element.all(by.repeater('img in phone.images')).count();
      expect(images).toBe(4);
    });
查看更多
Juvenile、少年°
3楼-- · 2019-06-27 12:49

This question is part of the Experiments section in tutorial 8 of the AngularJS tutorial.

Here is how I solved it. I took the different road of counting the images rendered rather than testing the repeater/model directly.

it('should display four thumbnails of the nexus-s', function() {
  var imgsCount = element.all(by.css('.phone-thumbs li img')).count();
  expect(imgsCount).toBe(4);
});
查看更多
登录 后发表回答