0

I call the getText() function like so and then try to resolve the promise but cant get the string value later.

var textFromElement = someElement.getText().then(function(text){return text})
var splittedText = textFromElement.split(" ")

How can I get the text for later use?

purplePanda
  • 169
  • 1
  • 1
  • 10
  • 1
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Roamer-1888 Jun 02 '17 at 18:37
  • 1
    Not really as this question is specific to the protractor API – purplePanda Jun 02 '17 at 18:41
  • 1
    Take a look [here](https://spin.atomicobject.com/2014/12/17/asynchronous-testing-protractor-angular/) and find the paragraph starting "Behind the scenes of WebDriver, ...". Now note how that paragraph ends, "... (unless we specifically want the result returned by the call)". Unless I'm completely misreading your intention, that proviso is key to my belief that my "possible duplicate ..." is appropriate here. – Roamer-1888 Jun 02 '17 at 19:07
  • I agree that this is an asynchronous call issue. However protractor is handling most of the asynchronous magic for you. So a beginning protractor developer is less likely to recognize it, until they understand the protractor promise flow. – Chuck Brown Jun 02 '17 at 20:37

2 Answers2

2

What you don't understand is how the javascript compiler deals with promises.

This is how the compiler looks at your code;

var textFromElement = someElement.getText().then(function(text){return text})
var splittedText = textFromElement.split(" ")

1 - All variable are created at the top of the function scope regardless of where you assign it.

var textFromElement; (= undefined)
var splittedText; (= undefined)

2 - Does the minimum amount of work it can get away with for each line and moves to the next line.

testFromElement = {promise element object};
splittedText = {promise element object}.split(" "); (= undefined)//This what you don't want.

3 - Starts at the top and does more minimal work on unresolved lines.

testFromElement = {promise getText object};

4 - Starts at the top and does more minimal work on unresolved lines.

testFromElement = "text text";

In short it assigns splittedText three step before you want it to.

Good example:

var splittedText;
it("should get split text", function(done) {
  someElement.getText().then(function(textFromElement){
    splittedText = textFromElement.split(" ");
    done();
  })
})

1 - All variable are created at the top of the function scope regardless of where you assign it.

var splittedText; (= undefined)

2 - Only work is done inside this function until done() is called

it("should get split text", function(done) {

3 - Does the minimum amount of work it can get away with for each line and moves to the next line.

someElement = {promise element object};

4 - Starts at the top of the function and does more minimal work on unresolved lines.

someElement.getText() = {promise getText object};

5 - Starts at the top of the function and does more minimal work on unresolved lines.

textFromElement = "text text";
splittedText = textFromElement.split(" "); (["text","text"]);
done();  //the compiler can now to work outside this function
Chuck Brown
  • 353
  • 1
  • 5
0

Your problem is that you are assigning the resolution of .then() method to your variable, not callback which you are providing as a parameter to it.

All in all protractor/jasmine comes with mechanism for asyncronous tests.

Here you got ES6 example.

it('some description', (done) => {
    someElement.getText().then(text => {
        var splittedText = text.split(" ");
        done();
    });
});

And ES5:

it('some description', function(done) => {
    someElement.getText().then(function(text){
        var splittedText = text.split(" ");
    }).finally(done);
});
Maciej Treder
  • 11,866
  • 5
  • 51
  • 74
  • My code runs in a function called by an it function, so I dont know how your example would apply. If I try to use splittedText outside I got undefined – purplePanda Jun 02 '17 at 19:27
  • And you will always get undefined, as long as you will be trying to reach values from asnychronize code in synchronized section. Promise is resolved as async one, so if you want to manipulate value, you must do it inside promise callback. I just gave you an example how can you make your test wait for async code. – Maciej Treder Jun 02 '17 at 19:50
  • Maybe you would provide more details/code. Then I will see context of your problem. – Maciej Treder Jun 02 '17 at 19:50