javascript - Protractor :WAITING一个元素,然后执行操作

标签 javascript selenium protractor

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

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 无法找到该元素并对其执行操作。有错误的地方请指出。

最佳答案

将等待函数与元素返回分开:

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));
}; 

关于javascript - Protractor :WAITING一个元素,然后执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34993176/

相关文章:

javascript - 解析字符串以通过 Google Apps 脚本在字符串中查找电子邮件

javascript - Selenium:如何编写同步 Selenium 自动化?

javascript - AngularFire 0.9 解决了 UI 路由器问题

javascript - 为多个按钮添加事件监听器

python - Python/Selenium 中的 by.cssContainingText

javascript - 使用 Webdriverjs Selenium 加载默认的 chrome 配置文件

javascript - 设置 Protractor 的conf文件以使用自定义报告器

angularjs - 使用 Protractor 从桌面拖放

javascript - Protractor 和 promise

javascript - 将 HTML 转换为 php 数组