如何使用量角器在 e2e 测试中预期元素的动态计数 [英] How to expect dynamic count of elements in e2e tests using Protractor

查看:13
本文介绍了如何使用量角器在 e2e 测试中预期元素的动态计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用 Protractor 为我不起眼的 Angular 应用程序编写一些 e2e 测试.

I'm currently writing some e2e tests for my humble Angular app with Protractor.

我的应用程序运行良好,单元测试通过了所有测试,也使用了 e2e...直到这个:

My app works fine, unit tests passes all, e2e used too... until this one:

appE2ESpec.js

describe('adding an item', function() {
  var items,
      addItemButton,
      startCount;

  beforeEach(function() {
    items = element.all(by.css('li.item'));
    addItemButton = element(by.id('addItemButton'));
    startCount = items.count();
  });

  it('should display a new item in list', function() {
    addItemButton.click();

    expect(items.count()).toEqual(startCount+1);
  });
});

这就是我编写测试的方式,但是,

This is how I would have written my test but,

问题是: items.count() 返回一个承诺,我知道,但我无法强制 Protractor 解决它.所以我明白了:

The problem is: that items.count() returns a promise, I know that, but I can't manage to force Protractor to resolve it. So I get this:

Failures:

1) myApp adding an item should display a new item in list
  Message:
    Expected 6 to equal '[object Object]1'.

我的尝试:

items.count().then(function(count) {
  startCount = count;
  //console.log(startCount) --> "6" Perfect!
});

但是最后得到了同样的结果……我不能把expect放到then里面,我也想过.

But got the same result at the end... I can't put the expect into the then, I thought about that too.

  • 我搜索了 Protractor GitHub 存储库问题、StackOverflow 和 Google AngularJs 组.

附录:

console.log(startCount) 输出:

{ then: [Function: then],
  cancel: [Function: cancel],
  isPending: [Function: isPending] }

我本可以编写 .toEqual(6),但我不想在每次需要更改应用启动状态时都重写测试.

I could have written .toEqual(6) but I don't want to rewrite my test each time I need to change my app startup state.

有什么想法吗?提前致谢!!

Any idea? Thanks in advance!!

推荐答案

你需要先解析promise,然后进行断言.

You need to resolve the promise and then do the assertion.

Protractor 将解析你传递给 expect() 的 Promise,但它不能在 Promise 中添加数字.你需要先解决promise的值:

Protractor will resolve the promise that you pass to expect(), but it cannot add a number to a promise. You need to resolve the value of the promise first:

beforeEach(function() {
  ...
  items.count().then(function(originalCount) {
    startCount = originalCount;
  });
});

it('should display a new item in list', function() {
  ...
  expect(items.count()).toEqual(startCount+1);
});

这篇关于如何使用量角器在 e2e 测试中预期元素的动态计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆