protractor icon indicating copy to clipboard operation
protractor copied to clipboard

Protractor-getText() return object instead of string

Open Saurabh06 opened this issue 10 years ago • 2 comments

getText() written object instead of string so i have used below function and make it as generic .

getElementText=function(elementLocator){ elementLocator.getText().then(function(text)
{ console.log(text);   //it prints correct text
return text; });
};

After that i am calling above function whenever i need text but in some case i need to split the returning value so i am doing like:

var abc= getElementText(<x-path of locator>);
name= abc.split(",");
console.log(name[0]+"and"+name[1]);

but i am getting value abc as undefined and i am not able to perform any function like split or replace on that. So help me how to perform any operation on returned value from getElementText() method like split/replace because when i try to perfrom any function on variable abc it is saying that abc is undefined.

Saurabh06 avatar Jun 04 '16 12:06 Saurabh06

Hi there!

getText is asynchronous, so even though your getElementText function is properly waiting for the promise with then it will not return anything itself so abc will be undefined.

Because of it's usage and debugging nature, your question is better suited for StackOverflow or Gitter. Please ask a question there with the 'protractor' tag or post in the Gitter Channel to get help.

From the the getting help section of the README:

Please ask usage and debugging questions on StackOverflow (use the "protractor" tag) or in the Angular discussion group. (Please do not ask support questions here on Github.)

Thanks!

NickTomlin avatar Jun 04 '16 14:06 NickTomlin

var name; getElementText().then((textReturnedAfterPromiseResolves) => { name = textReturnedAfterPromiseResolves.split(","); console.log(name[0]+"and"+name[1]); })

Because you are assigning abc with your function that returns a promise you first must wait for it to resolve before you can use it.

pieterjoubert6 avatar Jun 13 '18 12:06 pieterjoubert6