Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding variables together in expect statement

I have some strings that I have extracted using protractor/jasmine/angularJS and converted into integers. I am now trying to add these together and compare in an expect statement. But I am getting some promise errors in doing so.

var result0 = element.all(by.binding('Inboxes.Inbox.Count')).first().getText().then(parseFloat);
    result0.then((value) => console.log("count: ", value));

    var result1 = element.all(by.binding('InboxItem.Count')).get(0).getText().then(parseFloat);
    result1.then((value) => console.log("count: ", value));

    var result2 = element.all(by.binding('InboxItem.Count')).get(1).getText().then(parseFloat);
    result2.then((value) => console.log("count: ", value));

    var result3 = element.all(by.binding('InboxItem.Count')).get(2).getText().then(parseFloat);
    result3.then((value) => console.log("count: ", value)).then(expect(result1 + result2 + result3).toEqual(result0));

    //compare badge counts to Inbox badge count
    expect(result1 + result2 + result3).toEqual(result0);
  });
 });
}); 

I am getting the following promise errors. I thought that since the promises had already been satisfied and the counts below print out (41, 7, 14 and 20) that I could add the bottom 3(reulst1-3) together and compare to result0 which is the total of result1-3. I am having a time with these promises since I am new to this and don't quite understand them very well.

Started
count:  41
count:  7
count:  14
count:  20
F

Failures:
1) Workflow Application When selecting Alerts panel should expand the Inbox panel and Postings
  Message:
  Expected 'ManagedPromise::859 {[[PromiseStatus]]:  "pending"}ManagedPromise::896 {[[PromiseStatus]]: "pending"}ManagedPromise::933 {[[PromiseStatus]]: "pending"}' to equal ManagedPromise::822 {[[PromiseStatus]]: "pending"}.
like image 934
Dennis Ferguson Avatar asked Jun 06 '16 19:06

Dennis Ferguson


2 Answers

You are trying to add promises together, not actual resolved values.

In this case, I would solve that using the protractor.promise.all() to resolve all the promises needed to make the expectation, at once:

protractor.promise.all([result0, result1, result2, result3]).then(function (values) {
    expect(values[1] + values[2] + values[3]).toEqual(values[0]);
});

And, to simplify that, you can spread() the resolved values into "then" function arguments:

protractor.promise.all([result0, result1, result2, result3]).then(spread(function (value0, value1, value2, value3) {
    expect(value1 + value2 + value3).toEqual(value0);
}));

Note that, unlike q, protractor.promise does not have the spread() built-in and you have to have a custom one.

like image 132
alecxe Avatar answered Oct 05 '22 23:10

alecxe


I would use .map to keep it simple and efficient:

var total = element(by.binding('Inboxes.Inbox.Count'))
    .getText()
    .then(parseInt);

element.all(by.binding('InboxItem.Count'))        // get the elements
    .map((e, i) => e.getText().then(parseInt))    // parse each text
    .then((values) => {                           // assert the values 
        expect(total).toEqual(values[0] + values[1] + values[2]);  
    });
like image 26
Florent B. Avatar answered Oct 06 '22 00:10

Florent B.