im using protractor to retrieve a text.
one of the function is
//get the Impersonation ID
this.getUserSessionID = function(){
//get whole text
UserImpersonateTextElement.getText().then(function(text) {
var tempText = text;
var startString = 'session ID is ';
var sessionID = tempText.substring(tempText.lastIndexOf(startString)+startString.length,tempText.length);
console.log('sessionID is:'+sessionID);
return sessionID;
});
};
and im calling the function in another js(where i imported the above js) file
var getUserImpersonationID = ImpersonationSuccessPage.getUserSessionID();
and when i try
console.log('User Impersonation ID is:'+getUserImpersonationID);
i get undefined as the value.
but the console.log('sessionID is:'+sessionID);
in the function displays proper value.
Can anyone suggest what im doing wrong here?
The internal call to get text returns a promise. Go ahead and return that promise and then you can chain a
then
in the call to getUserSessionID. ExampleIn the call you'd do:
Or, since expect is suppose to resolve that promise you can check if you have an ID with expect:
You don't
return
from thegetUserSessionID
function, you only return from thethen
callback. In fact, you cannot return a value, as your function is asynchronous. You need to return the promise.and then use it like this: