1

I have written my first acceptance test for angular 2 using protractor + cucumber and it seems when i call table.filter the browser hangs and times out till the timeout time.

I am using chai-as-promised for expectations:

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

Below is the code which doesn't work.

var grid = $$(".table tr");
grid.filter(function (row) {
    var cells = row.$$('td');
    expect(cells.get(0).getText()).to.eventually.equal(factoryName).and.notify(done);

I have managed to make my scenario pass by replacing above code with:

var grid = $$(".table tr");
for (var i = 1; i < grid.count() ; i++) {
    var cells = grid.get(i).$$('td');
    expect(cells.get(0).getText()).to.eventually.equal(factoryName).and.notify(done);
}

I am struggling to find the reason why table.filter is causing timeouts.

Can anyone also help me with article/post, how to convert these steps.js into TypeScript which is a proper way of testing in angular2?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Kamran Pervaiz
  • 1,861
  • 4
  • 22
  • 42

1 Answers1

1

.filter() expects a filter function to have something returned from it (a boolean value, or a promise resolving to a boolean value, to decide whether to skip an element in the array or not).

From what I understand, you meant:

var grid = $$(".table tr");
var desiredRow = grid.filter(function (row) {
    var cells = row.$$('td');
    return cells.first().getText().then(function(cellText) {
        return cellText === factoryName;
    });
}).first();
// do something with the desiredRow

Or, if you meant to check the first cell of every row to be equal to factoryName, you should/could have used each():

var grid = $$(".table tr");
grid.each(function (row) {
    var cells = row.$$('td');
    expect(cells.first().getText()).to.eventually.equal(factoryName);
});

Or, you could've located the first cell of every row using first-of-type CSS selector pseudo-class and then assert all elements of the array have the factoryName value:

var cells = $$(".table tr td:first-of-type");

// example for 2 rows
// for N rows, http://stackoverflow.com/questions/14832603/check-if-all-values-of-array-are-equal
expect(cells.getText()).toEqual([factoryName, factoryName]);
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • I tried using each funtcion but it gives error that each is not a function then i found that filter is alternative to each. my requirement is to check the first cell to contain the factoryName. – Kamran Pervaiz Jul 22 '16 at 10:01
  • is it possible to get the first cell value using filter when it is matched by factory name or have expect() in filter function – Kamran Pervaiz Jul 22 '16 at 10:08
  • @KamranPervaiz about `each()`..that's weird..what version of protractor are you using? – alecxe Jul 22 '16 at 12:13
  • i used your .each function but it also timeout, i am not sure why. I am using protractro "3.3.0". my requirement is to only get the first cell value and check factoryName exists or not. if it exists call the callback funtion done – Kamran Pervaiz Jul 23 '16 at 13:29