1

Still fairly new to Protractor. I'm trying to set the variable text but it returns the empty string. Explanations and fixes are greatly appreciated!

I tried debugging by adding "listname " to be printed to the console. It works fine but the text is not printed to the console.

var listname = "";

selectList()
    .first()
    .element(by.css('.listname'))
    .getText()
    .then(function(text) { 
        listname = text;
        console.log(listname);
    });

console.log("listname " + listname);

From my understanding, I know that this should be asynchronous, but I thought the promise is fulfilled by the .then . Afterwards I should be able to retrieve the text.

The return output is:

listname 

List1

Whereas I expect:

List1

listname List1
vorant
  • 708
  • 5
  • 15
TypeSource
  • 63
  • 1
  • 2
  • 6
  • `.then()` sets a *callback* for when the asynchronous code is done. It does not fulfill/resolve the promise. – gen_Eric Feb 09 '16 at 20:27

2 Answers2

1

If it was blocking console.log then it wouldn't have been asynchronous. console.log gets executed before your promise is resolved. Therefore below gets printed first.

listname 

List1

Check fantastic answer here

Community
  • 1
  • 1
nilesh
  • 14,131
  • 7
  • 65
  • 79
0

It is asynchronous, so it is not sure to the first console.log will runs before the second console log.

ktom
  • 228
  • 1
  • 3
  • 11