value of a function in JavaScript(protractor) returns as undefined -


im using protractor retrieve text.

one of function is

//get impersonation id this.getusersessionid = function(){     //get whole text     userimpersonatetextelement.gettext().then(function(text) {         var temptext = text;         var startstring = 'session id ';         var sessionid = temptext.substring(temptext.lastindexof(startstring)+startstring.length,temptext.length);         console.log('sessionid is:'+sessionid);         return sessionid;     }); }; 

and im calling function in js(where imported above js) file

var getuserimpersonationid = impersonationsuccesspage.getusersessionid(); 

and when try

console.log('user impersonation id is:'+getuserimpersonationid); 

i undefined value.

but console.log('sessionid is:'+sessionid); in function displays proper value.

can suggest im doing wrong here?

the internal call text returns promise. go ahead , return promise , can chain then in call getusersessionid. example

this.getusersessionid = function(){ //get whole text     return userimpersonatetextelement.gettext().then(function(text) {     var temptext = text;     var startstring = 'session id ';     var sessionid = temptext.substring(temptext.lastindexof(startstring)+startstring.length,temptext.length);     console.log('sessionid is:'+sessionid);     return sessionid;    }); }; 

in call you'd do:

getusersessionid().then(function(sessionid){   console.log('you session id ',sessionid); }) 

or, since expect suppose resolve promise can check if have id expect:

expect(this.getusersessionid()).not.tobenull(); 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -