Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript element.all无法在量角器测试中分割数据_Javascript_Typescript_Protractor - Fatal编程技术网

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

Javascript element.all无法在量角器测试中分割数据,javascript,typescript,protractor,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(' '); }); }

在执行上述代码时,我将得到如下输出

实际结果:

comparePopup() {
      element.all(by.xpath("//div[@class='My Private vDiv']//label//span[1]")).getText().then(function (Data) {
          console.log(Data);
          //Data.sort();
          Data.split(' ');
      });
    }
我需要删除每个值的前3个字符

预期结果:

[ '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)' ]
运行量角器测试时出现的错误是

失败:Data.split不是函数

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

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

相反,试试看

[ '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)' ]
哪个会输出

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)" ]
comparePopup(){
元素.all(by.xpath(“//div[@class='My Private vDiv']//label//span[1]”)。getText()。然后(函数(数据){

for(i=0;igetting error-类型“string”上不存在属性“map”。@dhrusoni我已更新了答案。问题是
getText
返回一个字符串,但您需要一个字符串数组。@z11i我相信一旦
ElementArrayFinder
被解析,使用。然后,它将成为一个常规数组。同时调用
getText()
在ElementArrayFinder上不应返回编辑中所述的字符串,而应返回字符串数组,因此我认为您的初始答案更为正确。不确定OP收到该错误的原因。我在此页面上成功执行了下面的
元素。all(by.xpath(“//ol[@class='nav-links']//li”)。getText()。然后(函数(数据){Data=Data.map(eachString=>eachString.split(“”)[1]);console.log(Data);});
@z11i获取此错误属性“split”在类型“{}”上不存在“.您使用的是typescript吗?
Data
在您的代码中是否有其他含义,像这样的参数使用大写变量名是不常见的?您可以尝试将
Data
更改为
allTxt
或类似的内容吗?类型“String”中的索引签名只允许读取.ts(2542)@dhrusoni可以完全更新实现和错误。
    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)
           }
          });
        }