Wes Thompson
Wes Thompson

Reputation: 473

getAttribute return unresolved

I'm just trying to save the attribute of an input value to a variable.

This is the code:

var sliderNumber = element.all(by.model('color.red')).get(1);
var firstNum = sliderNumber.getAttribute('value').then(function(value) {
    return value;
});
  //Some code that changes the  sliderNumber attribute      
expect(sliderNumber.getAttribute('value')).toEqual(firstNum + 1);

This gives me an error like this:

Expected '184' to equal Promise::433 ([[PromiseStatus]]: "pending")1.

I've also tried:

var firstNum = function() {
    return sliderNumber.getAttribute('value').then(function(value) {
        return value;
    });
}

That didn't help at all. How do I resolve this promise?

Upvotes: 1

Views: 471

Answers (1)

Agop
Agop

Reputation: 1917

Since .getAttribute() returns a promise, which will invoke your callback asynchronously, you need to put your test logic within the callback:

var sliderNumber = element.all(by.model('color.red')).get(1);

// get the initial value
sliderNumber.getAttribute('value').then(function(value) {
    // once we've got the initial value, store it, then proceed with your test
    var initialValue = parseInt(value, 10);

    //Some code that changes the  sliderNumber attribute  

    expect(sliderNumber.getAttribute('value')).toEqual((initialValue + 1).toString());
});

You can't simply get the return value outside of the callback, because that callback may or may not be called after the rest of your code.

Upvotes: 2

Related Questions