Byte Surfer Byte Surfer - 5 months ago 28
Javascript Question

How to return text from an element into variable in Protractor

Here's the deal - getText() method returns promise, it's okay in case when you are using it inside of expect(), but when I'm trying to return

value of an element into variable, it returns promise. Here is the code:

var text = mainPage.counter().getText().then((text) => {
return text;

Is there any way to get the text and assign it to a variable?


A promise’s value could be resolved only by another promise, you should probably check this post : How do I return the response from an asynchronous call?

so in your case -

 var data = mainPage.counter().getText().then((text) => {
    return text;
 // at this point data is still a managed promise!
 data.then((text) => {
   console.log(text); // your text would be printed!