TypeSource
TypeSource

Reputation: 63

Protractor: Setting variables

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

Upvotes: 1

Views: 131

Answers (2)

nilesh
nilesh

Reputation: 14289

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

Upvotes: 1

ktom
ktom

Reputation: 228

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

Upvotes: 0

Related Questions