Skip to content Skip to sidebar Skip to footer

How To Expect Dynamic Count Of Elements In E2e Tests Using Protractor

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

Solution 1:

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

Solution 2:

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

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

Post a Comment for "How To Expect Dynamic Count Of Elements In E2e Tests Using Protractor"