量角器:等待一个元素,然后执行操作

时间:2016-01-25 12:54:32

标签: javascript selenium protractor

我写了下面的代码来检查定位器类型,并根据元素是否可见,我返回元素。我在调用类型(locatorType,value,text)方法上收到错误,并带有适当的值。

this.type = function(locatorType,value,text){
        this.getElement(locatorType,value).sendKeys(text)
    };

this.getElement = function(locatorType,value){
        if(locatorType=='model'){
            console.log(locatorType)
            console.log(value)
            return this.waiterFunc(element(by.model(value)));
        }
        else if(locatorType=='xPath'){
            return this.waiterFunc(element(by.xPath(value)));
        }

        else if(locatorType=='buttonText'){
            return this.waiterFunc(element(by.buttonText(value)));
        }
    };

this.waiterFunc = function(element){
        console.log('In waiterfunc')
        //console.log(element.getText())
        browser.wait(function() {           
            return this.isVisible(element).then(function(){
                return element;
            })
        })
    };

this.isVisible = function(element){
        return EC.visibilityOf(element);
    };

以下是收到的错误: enter image description here

WebDriver无法找到该元素并对其执行操作。请说明我的错误。

1 个答案:

答案 0 :(得分:1)

将等待函数与元素return:

分开
this.getElement = function(locatorType, value) {
    var elm;

    if (locatorType == 'model') {
        elm = element(by.model(value));
        this.waiterFunc(elm);
    }
    else if (locatorType == 'xPath') {
        elm = element(by.xpath(value));  // also renamed xPath -> xpath
        this.waiterFunc(elm); 
    }

    else if (locatorType == 'buttonText') {
        elm = element(by.buttonText(value));
        this.waiterFunc(elm);
    }
    return elm;
};

在这种情况下,waiterFunc会变得更简单:

this.waiterFunc = function(element){
    browser.wait(this.isVisible(element));
};