0

Sometimes I get weird errors in my tests and I want to debug it. I try and do this with things like getAttribute() or getText() but these functions return a promise. I find it hard to resolve the promise without having to do an expect. Take this example:

console.log(servicePage.services.get(0).getAttribute('class'));

I was hoping that it would just output the value of the class but a promise is returned. How can I get this promise resolved? I tried using the then(function(elem)... function but what to do next? When I perform an action like getText() on elem, I am again stuck with a promise.

Thanks in advance! Regards

Homewrecker
  • 1,076
  • 1
  • 15
  • 38
  • You could always make a helper function that logs for you: `function log(msg) { console.log(msg) };` which you could add to any promise with `.then(log)`. – GregL Feb 10 '16 at 08:50

1 Answers1

3
servicePage.services.get(0).getAttribute('class')).then(function(result){
    console.log(result);
});

//an example of a random function getting the text from a promise...
page.someElement.getText().then(function(txt){
    console.log(txt);
});
TesterPhi
  • 346
  • 1
  • 12
  • Works like a charm, thank you very much. Is result a pre-defined keyword that maps to the result of the promise? – Homewrecker Feb 10 '16 at 08:57
  • Nope, i just use result as the returned value from the promise. It is was text I was expecting, I would put something like "txt" in there... it just makes it easier for people to understand what it is your are console logging... The result of the getAttribute('class') promise in this case – TesterPhi Feb 10 '16 at 09:00
  • Ok, so it's purely the then() that resolves the promise? – Homewrecker Feb 10 '16 at 09:01
  • I would recommend reading jmcollins answer here, it helped me alot! http://stackoverflow.com/questions/29331499/when-should-we-use-then-with-protractor-promise – TesterPhi Feb 10 '16 at 09:44