Dennis Ferguson Dennis Ferguson - 4 months ago 38
AngularJS Question

jasmine print getText output outside function

I am writing a protractor spec which uses angularJS and jasmine to get text from a page which I later want to convert from a string to integer. My first problem is accessing the getText value outside of the function. For example in the code below:

var post_count = element.all(by.binding('InboxItem.Count')).get(0);
post_count.getText().then (function (text){
console.log('mytext_inside_function = ' + text);
return text;
console.log('mytext_outside_function = ' + post_count);

I can see that the code is executing asynchronously and the console.log my text_outside_function is running before the my_text_inside_function and thusly returning "object object" instead of the number 7( which is text and not a number)

ᐅ protractor protractor.conf.js --suite inbox
[10:56:34] I/local - Starting selenium standalone server...
[10:56:35] I/launcher - Running 1 instances of WebDriver
mytext_outside_function = [object Object]
mytext_inside_function = 7
1 spec, 0 failures
Finished in 0.938 seconds

I am a new to jasmine promises and not sure how to correct this. I added the .then (function... to my test, but didn't seem to resolve the promise.
My ultimate goal is to get the text ( 7) and then convert it from the current string to a number so I can do a calculation(addition) to it.

Thanks for the suggestion Nick. I guess I am doing this wrong. I added the mytext variable which I am thinking is outside the function( probably am wrong). see below..

var post_count = element.all(by.binding('InboxItem.Count')).get(0);
var mytext = post_count.getText().then (function (text){
console.log('mytext_inside_function = ' + text);
return text;
console.log('mytext_outside_function = ' + my text);

I get this error.

mytext_outside_function = ManagedPromise::810 {[[PromiseStatus]]: "pending"}


Either call console.log in the resolution of the Promise :

var result = element(by.binding('InboxItem.Count')).getText().then(parseFloat);

result.then((value) => console.log("count: ", value));

Or push a new function in the execution flow to display the result:

var count = 0;
element(by.binding('InboxItem.Count')).getText().then((text) => {
  count = parseFloat(text);

browser.controlFlow().execute(() => {
  console.log("count: ", count);