5

So I'm trying to get a string value to be returned from the value of an element on the resolution of this promise. I want to pass a raw string value to another function that I'm building inside a protractor test.

This is the element:

<div style='hidden' >
    <input id="group-sendgrid-hidden-input" ng-model='groupCode' value='dangyo' >
</div>

I'm looking for a way to get at either the model value or the attribute value (either will work). Model value might even be better.

This is my attempt to resolve the a promise here and return a result:

// first get the element driver object
var groupCode = element(by.id('group-sendgrid-hidden-input'));

// next resolve a promise provided by this element
groupCode.getAttribute('value').then(function(value){

    console.log( 'should be a string: ' + value);

    return value;
});

Here the console.log( 'should be a string: ' + value); always returns null for the value and nothing I can do seems to resolve this. I'm sure I'm doing something wrong because I am new to protractor and this seems simple. Does anyone else experience this behavior?

usumoio
  • 3,500
  • 6
  • 31
  • 57
  • 3
    Just a sanity check - there is only one element with `id="group-sendgrid-hidden-input"` on the page? – alecxe Apr 01 '15 at 16:49
  • Yup, there is only one. Just ran a search. – usumoio Apr 01 '15 at 16:53
  • Could it have something to do with the div being hidden? Just another sanity check, since it would obviously return an 'Element is not visible' error. – Tom Nijs Apr 01 '15 at 16:58

1 Answers1

4

It's too big for a comment and would still be a guess, but how about making a custom "Expected Condition" and wait for the input element's value attribute value not to be null:

var hasNotNullValue = function(elementFinder) {
    return function() {
        return elementFinder.getAttribute("value").then(function(value) {
            return !!value;  // is not null
        });
    }; 
};

var groupCode = element(by.id('group-sendgrid-hidden-input'));
browser.wait(hasNotNullValue(groupCode), 10000);

groupCode.getAttribute('value').then(function(value){
    console.log('should be a string: ' + value);
});

You can also use evaluate() to retrieve the model value:

groupCode.evaluate('groupCode').then(function(value) {
    console.log(value);
});
Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Okay, This is excellent. This works. In my other question I'm going to update that the answer is here. Thank you. – usumoio Apr 02 '15 at 03:36