Question

I'm using Angular.js Protractor to get check the values of cells in a grid. I'm able to get the values, but they are strings, and I'd like to do calculations with them.

When I try this:

ptor.findElements(protractor.By.className('ngCellText')).then(function(cells) {
  expect(parseFloat(cells[16].getText())).toEqual(parseFloat(cells[14].getText()) / parseFloat(cells[11].getText()));
});

I get the error: Expected NaN to equal NaN.

I read that promises are asynchronous, and you can't convert them with a synchronous function, so I tried this:

element.all(by.className('ngCellText')).then(function(text1) {
  element.all(by.className('ngCellText')).then(function (text2) {
    element.all(by.className('ngCellText')).then(function (text3) {
      expect(parseFloat(text1[16].getText())).toEqual(parseFloat(text2[14].getText()) + parseFloat(text3[11].getText()));
    });
  });
});

And I get the same error: Expected NaN to equal NaN.

If anyone could help me out, that'd be great!

Was it helpful?

Solution

Try this, now that we know that getText() returns a promise:

cells[16].getText().then(function (cellValue) {
    console.log(cellValue); // This should give you '1.40'

    var floatVal = parseFloat(cellValue);
    console.log(floatVal); // This should give you 1.4

    expect(floatVal).toBe(1.4);
});

OTHER TIPS

You can also assign it to a variable using the arrow function:

var someVal = $(elem).getText().then(value => +value);

Or use parseFloat() if you need specific int.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top