1

I want to preserve variable outside getText() function, so that I can match it to the values latter in the tests. Example:

Page object file:

this.numberOfAllLines = element(by.id('all_lines'));
this.tableCell = element(by.css('.table_results_lines'));
this.sumOfAllLinesText = element(by.id('sum_lines'));

Spec file:

var numberOfLines = '';
var newNumberOfLines = '';

describe(...
    it(...
        page.numberOfAllLines.getText().then(function(num) {
            numberOfLines = num;
        });

        newNumberOfLines = numberOfLines + 10;

        expect(page.tableCell.getText()).toEqual(newNumberOfLines);

        // doing some clicks

        expect(page.sumOfAllLinesText.getText()).toEqual(newNumberOfLines);
    });
});

This is not answered in How do I return the response from an asynchronous call?.

Community
  • 1
  • 1
jurijk
  • 309
  • 8
  • 22

1 Answers1

1

This piece of code newNumberOfLines = numberOfLines + 10 is async and will execute before even the assignment of numberOfLines = num; happens.

What you are doing is correct. Having a global variable which hold the value to be compared. But have the assignment code inside the chained promise of getText().

it(...
        page.numberOfAllLines.getText().then(function(num) {
            numberOfLines = num;
            newNumberOfLines = numberOfLines + 10;
        });  

        expect(page.tableCell.getText()).toEqual(newNumberOfLines);
AdityaReddy
  • 3,625
  • 12
  • 25
  • I'm still such a noob. Thanks, this solved my problem! – jurijk Mar 08 '17 at 08:50
  • Do you know how can I can multiply values from getText() functions in this example: `page.numberOfAllLines.getText().then(function(num) { numberOfLines = num; }); page.winPrice.getText().then(function(price) { cost = price; }); priceSum = numberOfLines * cost; expect(page.tableCell.getText()).toEqual(priceSum);` Is this even possible due to async nature of Protractor? – jurijk Mar 08 '17 at 09:28
  • Put this - `priceSum = numberOfLines * cost` inside the second `getText().then()` – AdityaReddy Mar 08 '17 at 09:55