I am writing a Protractor test that needs to wait for an element attribute to have a non-empty value, and then I want to return that value to the caller function. It turned out to be harder to write than I expected!
I can correctly schedule the browser.wait() command to wait for the element attribute to have a non-empty value, and I confirmed that this value is actually what I expect to get inside the callback function, but for some reason I cannot return this value outside the callback function and to the rest of the test code.
This is what my code looks like:
function test() { var item = getItem(); console.log(item); } function getItem() { var item; browser.wait(function() { return element(by.id('element-id')).getAttribute('attribute-name').then(function(value) { item = value;
I can say that the execution order is not what I expect, because when I uncomment the console.log() call inside the callback function, I see that the expected value is being printed. However, the same call to test() prints "undefined".
What's going on here? What am I missing? How can I get the attribute value from the callback function correctly?
I appreciate your help.
source share