javascript - 在 AngularJS 中使用 Jasmine 测试元素数量

标签 javascript jquery angularjs jasmine protractor

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

<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'

我哪里错了?

最佳答案

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

我是这样解决的。我采取了不同的方式计算渲染的图像,而不是直接测试中继器/模型。

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

关于javascript - 在 AngularJS 中使用 Jasmine 测试元素数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23583408/

相关文章:

javascript - 如何从 iframe 中打印 pdf?

php - 访问控制允许来源 angularjs 到 php

javascript - 调整窗口大小时同位素包装中的元素之间的细线间隙

jquery - 检测 Chrome iPhone 6+

javascript - 在 auth0 中获取 accessToken

jquery - 如何在 Bootstrap 4 中悬停时激活导航下拉菜单?

angularjs - 延迟angular.js $ http服务

angularjs - UI Bootstrap 的 typeahead 拒绝遍历对象

javascript - 我如何测量 React 16 中浪费的渲染?

javascript - 是否可以在不使用 `<template>` 和模板槽的情况下在自定义元素中添加元素?