Cannot return string from value of element in protractor test

痴心易碎 提交于 2020-02-02 03:44:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!