【问题标题】:Protractor: not able to get all elements using protractor量角器:无法使用量角器获取所有元素
【发布时间】:2016-01-25 23:35:17
【问题描述】:

我无法使用 css 或中继器获取所有元素。

this.clickRandomEmployee = function(){      
        employees = elementController.getAllElements('css','#employee-list li');
        numberOfEmployees = employees.length;
        console.log(numberOfEmployees); 
        var numRand = this.getRandomNumber(numberOfEmployees);
        console.log(numRand);
        elementController.doubleClick(employees.get(numRand));
    }

this.getAllElements = function(locatorType,value){
        var emps;       
        console.log(locatorType);
        console.log(value);
        if(locatorType=='css'){
            emps = element.all(by.css(value));
        }
        else if(locatorType=='repeater'){
            emps = element.all(by.repeater(value));
        }   
        return emps;
    };

上面的代码是从测试脚本中调用来查找所有元素的,但它返回未定义。请推荐!!

【问题讨论】:

    标签: javascript selenium protractor


    【解决方案1】:

    为什么不去掉 getAllElements 函数,只使用以下简单的行:

    employees = element.all(by.css('#employee-list li'))
    

    employees = element.all(by.repeater(value))
    

    完成此操作后,您可能应该使用 then 语句来确保在继续之前返回转发器的值。

    employees = element.all(by.css('.items li')).then(function(returnedList) {
        numberOfEmployees = returnedList.length;
    ...
    })
    

    【讨论】:

      【解决方案2】:

      getAllElements 返回一个承诺,它将解析为一个元素数组。承诺中没有 length 属性。相反,请使用count():

      employees = elementController.getAllElements('css','#employee-list li');
      employees.count().then(function (numberOfEmployees) {
          var numRand = this.getRandomNumber(numberOfEmployees);
          console.log(numRand);
          elementController.doubleClick(employees.get(numRand));
      });
      

      另见:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多