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

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

  • I searched into Protractor GitHub repository issues, StackOverflow and Google AngularJs group.

Appendix:

console.log(startCount) outputs this :

{ 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.

Any idea? Thanks in advance!!

Answers


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

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

Need Your Help

How to access mysqli connection in another class on another page?

php mysql oop mysqli

How can I open a database connection in user class , where I can do database operation?

How do I trigger the browser context menu on an element. (As if the user right-clicked)

javascript contextmenu

I have the need to trigger the opening of the browser (IE, Firefox, Safari, etc) context-menu via javascript. The problem I am trying to solve, is when an overlaid element is right-clicked, the ele...