问题
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?
回答1:
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);
});
来源:https://stackoverflow.com/questions/29396338/cannot-return-string-from-value-of-element-in-protractor-test