如何指望用量角器端到端测试元素动态计 [英] How to expect dynamic count of elements in e2e tests using Protractor

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

问题描述

我目前正在写与量角器愚见角应用中的一些端到端的测试。

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

我的应用程序工作正常,单元测试通过了所有,用于端到端太......直到这一个:

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()返回一个承诺,的我知道的,但我不能设法迫使量角器来解决它。所以,我得到这样的:

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

但得到在年底同样的结果......我不能把期望然后 ,我想到了这一点。

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


  • 我搜索到量角器的GitHub库的问题,计算器和谷歌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!!

推荐答案

您需要解决的承诺,然后做断言。

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

量角器将解决您传递给期望()的承诺,但它不能将号码添加到一个承诺。您需要首先解决的承诺的值:

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

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

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