element.all无法在量角器测试中拆分数据

时间:2019-06-14 05:25:21

标签: javascript typescript protractor

comparePopup() {
      element.all(by.xpath("//div[@class='My Private vDiv']//label//span[1]")).getText().then(function (Data) {
          console.log(Data);
          //Data.sort();
          Data.split(' ');
      });
    }

在执行上面的代码时,我将得到类似下面的输出。

实际结果:

[ 'Dr Testing1 Hill (Testing)',
  'Dr Testing2 Hill (Testing)',
  'Dr Testing3 Hill (Testing)',
  'Mr Testing1 Hill (Testing)',
  'Mr Testing2 Hill (Testing)',
  'Mr Testing3 Hill (Testing Testing)',
  'Mr Testing Hill (Testing)',
  'Mr Testing Hill (Testing)',
  'Mr Testing Hill (Testing)',
  'Dr Testing Hill (Testing)' ]

我需要删除每个值的前3个字符。

预期结果:

[ 'Testing1 Hill (Testing)',
  'Testing2 Hill (Testing)',
  'Testing3 Hill (Testing)',
  'Testing1 Hill (Testing)',
  'Testing2 Hill (Testing)',
  'Testing3 Hill (Testing Testing)',
  'Testing Hill (Testing)',
  'Testing Hill (Testing)',
  'Testing Hill (Testing)',
  'Testing Hill (Testing)' ]

我在进行量角器测试时遇到的错误是

  

失败:Data.split不是函数

我需要对数据进行排序和拆分。

2 个答案:

答案 0 :(得分:1)

尝试以下方法。这里的Data是一个字符串数组。因此我们需要将所有值迭代到

    comparePopup() {
          element.all(by.xpath("//div[@class='My Private vDiv']//label//span[1]")).getText().then(function (Data) {
              for(i=0;i<Data.length-1;i++){  //To iterate into the array
                 Data[i] = Data[i].subString(3);  //Now we get Testing1 Hill (Testing)
           }
          });
        }

希望它对您有帮助

答案 1 :(得分:0)

element.all返回一个ElementArrayFinder,它基本上是一个数组。数组没有方法split,这就是您的错误所说的。

相反,尝试

comparePopup() {
      element.all(by.xpath("//div[@class='My Private vDiv']//label//span[1]")).map(function(elm) => {
              return elm.getText();
          })
          .then(function (texts) {
              // for each string in the array, split by whitespace,
              // discard the first word, and join the rest by whitespace
              // then sort the resultant array
              texts.map(v => v.split(' ').splice(1).join(' ')).sort();
      });
    }

将输出

[ "Testing Hill (Testing)",
  "Testing Hill (Testing)",
  "Testing Hill (Testing)",
  "Testing Hill (Testing)",
  "Testing1 Hill (Testing)",
  "Testing1 Hill (Testing)",
  "Testing2 Hill (Testing)",
  "Testing2 Hill (Testing)",
  "Testing3 Hill (Testing Testing)",
  "Testing3 Hill (Testing)" ]