【问题标题】:Test for the number of elements using Jasmine in AngularJS在 AngularJS 中使用 Jasmine 测试元素的数量
【发布时间】:2014-05-10 16:15:44
【问题描述】:

我正在使用 Jasmine 为 AngularJS 编写端到端测试。我正在使用量角器来运行测试。我有以下标记

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

我想测试一个特定的页面实例,我有四个这样的图像。这是我目前所拥有的

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

问题是我收到一个错误提示

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

我哪里出错了?

【问题讨论】:

    标签: javascript jquery angularjs jasmine protractor


    【解决方案1】:

    这个问题是 AngularJS 教程tutorial 8 中“实验”部分的一部分。

    这是我解决它的方法。我采取了不同的方式来计算渲染的图像,而不是直接测试转发器/模型。

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

    【讨论】:

      【解决方案2】:

      对于任何需要知道这一点的人。以下是我的做法。

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

      【讨论】:

      • 只是想了解您的解决方案。 element 的值是多少?
      • @alljamin, element 是 ElementHelper 的量角器别名
      猜你喜欢
      • 2013-08-29
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      • 2018-01-06
      • 1970-01-01
      • 2016-07-18
      • 1970-01-01
      相关资源
      最近更新 更多