javascript 如何使用量角器在 e2e 测试中预期元素的动态计数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21685161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:28:59  来源:igfitidea点击:

How to expect dynamic count of elements in e2e tests using Protractor

javascriptangularjsprotractor

提问by glepretre

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

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

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

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

appE2ESpec.js

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,

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

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:

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

Failures:

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

What I've tried:

我试过的:

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 expectinto the then, I thought about that too.

但是最后得到了同样的结果......我不能把它expect放进去then,我也想过。

  • I searched into Protractor GitHub repository issues, StackOverflow and Google AngularJs group.
  • 我搜索了量角器 GitHub 存储库问题、StackOverflow 和 Google AngularJs 组。

Appendix:

附录:

console.log(startCount)outputs this :

console.log(startCount)输出这个:

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

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.

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

Any idea? Thanks in advance!!

任何的想法?提前致谢!!

回答by Andres D

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:

量角器将解析您传递给 expect() 的承诺,但它不能向承诺添加数字。你需要先解析promise的值:

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

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

回答by ciekawy

While using chaifor assertions there is chai-as-promisedthat allows to cope with promises in more readable way. To have it work you need first to declare:

chai用于断言时chai-as-promised,允许以更易读的方式处理承诺。要让它工作,您首先需要声明:

var chai = require('chai');
var chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);

and then in the code:

然后在代码中:

// here is modified remaind of the code from the original question:
it('should display a new item in list', function() {
  addItemButton.click();

  expect(items.count()).should.eventually.equal(startCount+1);
});