从页面上的多个元素输出文本-量角器,Javascript

从页面上的多个元素输出文本-量角器,Javascript,javascript,protractor,Javascript,Protractor,我正在研究量角器自动化框架。我想从页面上的多个元素输出文本值 所有元素都有一个我想在span标记中输出的文本。因此,我为自己制作了这个助手: static getAllSpansContainText (text: string) { return this.getAllSpanByText(text, true); } 我在这里使用上述方法: static get jobStasuses(){ return CommonPage.getAllSpansContainText('A

我正在研究量角器自动化框架。我想从页面上的多个元素输出文本值

所有元素都有一个我想在span标记中输出的文本。因此,我为自己制作了这个助手:

static getAllSpansContainText (text: string) {
  return this.getAllSpanByText(text, true);
}
我在这里使用上述方法:

static get jobStasuses(){
    return CommonPage.getAllSpansContainText('ACCEPTED');    
}
在我的测试类中,我使用以下方法:

console.log('Job Statuses: '+ffcPage.jobStasuses);
我希望这会打印出文本“接受”的次数,它出现在页面上的次数。但我得到:

Job Statuses: [object Object]

我是新手,所以如果这个问题看起来很傻,请接受。

你需要解决承诺。element.all返回一个承诺,这就是您获得[object]的原因。因此,您需要使用.thenfunctionresolved\u对象;因为您有多个元素,所以需要像下面的代码中所示通过保持元素来循环它。您还需要解析currentElement.getText,因为它也会返回一个承诺。下面是代码片段。希望能有帮助

var elementBySpan = element.all(by.xpath('//span')); // returns a promise and not the array objects. you need to resolve it by using .then

  elementBySpan.then(function(varEleBySpan){
     for(i=0; i<varEleBySpan.length; i++){
       (function(currentElement){
          currentElement.getText().then(function(txtValue){
            console.log("Job Statuses: " + txtValue) // this will display the text value of each span in the page
        });   
       })(varEleBySpan[i]);
     }
  });

除非另有设置,否则Object.toString始终为[Object Object]请显示函数:getAllSpanByText@yong提到了这个功能above@UzIT,我要求函数getAllSpanByText,而不是GetAllSpanSContaintText