llacey
llacey

Reputation: 25

Protractor - How to Get Value from an Object to Manipulate Value Outside the Promise

Here is the issue I am trying to get value from object inside a promise. I am trying to get the 1st element from a column (Kendo grid). Need to manipulate the value.

Here is the code:

currentAmountDue =  element(((helper.getGridValue(businessPaymentsPage.colAmountDue(), 0)).getText().then(function (value) {
              x = value;
                console.log('x: ', x);
                console.log('Value : ', value);
                return value;
            })));
console.log('x outside : ', x); 

Here are the results:

x outside :  0

x:  $9,750.75

Value :  $9,750.75

I am new to Protractor and Promises.

Upvotes: 0

Views: 1593

Answers (1)

P.T.
P.T.

Reputation: 25177

The code inside the then block will be executed later. getText() is an asynchronous method that returns a promise that will be resolved when the text is actually fetched (which will happen later).

In general, with promises, you need to chain dependent code off the code it depends on with then, so make your "outside" code run inside a then. In practice, most of the top-level protractor methods register their promises with a ControlFlow that makes sure the promises are resolved in order. This minimizes the number of explicit then chains, but does make the code a bit more magical. (The controlflow infrastructure is part of WebDriverJS that Protractor builds on.)

Read https://github.com/angular/protractor/blob/master/docs/control-flow.md and https://github.com/SeleniumHQ/selenium/wiki/WebDriverJs#control-flows

Upvotes: 1

Related Questions