Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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
Protractor 试图将e2e代码移动到函数中,但没有成功_Protractor_E2e Testing - Fatal编程技术网

Protractor 试图将e2e代码移动到函数中,但没有成功

Protractor 试图将e2e代码移动到函数中,但没有成功,protractor,e2e-testing,Protractor,E2e Testing,问题是由于promise解析,我为要搜索的所有元素传递了一个xPath和一个要搜索的字符串,我希望返回元素。代码如下: export class Library { static findListItem(xPath: string, findItem: string): any { let z = 0; const allItemsXPath = xPath.split('[X]'); const itemXPath = xPath.split('X');

问题是由于promise解析,我为要搜索的所有元素传递了一个xPath和一个要搜索的字符串,我希望返回元素。代码如下:

export class Library {
  static findListItem(xPath: string, findItem: string): any {
    let z = 0;
    const allItemsXPath = xPath.split('[X]');
    const itemXPath = xPath.split('X');
    console.log(xPath + ' : ' + findItem);
    const itemList = element.all(by.xpath(allItemsXPath[0] + allItemsXPath[1])).map(function (item) {
      return item.getText();
    });
    itemList.then(function (itemText) {
      console.log(itemText.length);
      for (let k = 0; k < itemText.length; k++) {
        itemFound = true;
        console.log(itemText[k] + ' : ' + findItem);
        if (itemText[k] === findItem) {
          z = k + 1;
          console.log('found ' + z);
        }
      }
    }).then(() => {
      console.log(itemXPath[0] + z + itemXPath[1]);
      // element(by.xpath(itemXPath[0] + z + itemXPath[1])).click();
      return element(by.xpath(itemXPath[0] + z + itemXPath[1]));
    });
  };
导出类库{
静态findListItem(xPath:string,findItem:string):任意{
设z=0;
const allItemsXPath=xPath.split(“[X]”);
const itemXPath=xPath.split('X');
log(xPath+':'+findItem);
const itemList=element.all(by.xpath(allItemsXPath[0]+allItemsXPath[1])).map(函数(项){
return item.getText();
});
itemList.then(函数(itemText){
console.log(itemText.length);
for(设k=0;k{
log(itemXPath[0]+z+itemXPath[1]);
//元素(by.xpath(itemXPath[0]+z+itemXPath[1])。单击();
返回元素(by.xpath(itemXPath[0]+z+itemXPath[1]);
});
};
用于单击的注释行起作用。它可以看到元素并单击它。我想将元素返回给调用方,然后调用方单击


如果返回不在.then节中,则返回发生得太快,z=0。按照现在的方式,不会返回任何内容。

您的函数
findListItem
无返回值,请在
itemList之前添加
return

static findListItem(xPath: string, findItem: string): any {
   ...

   return itemList.then(function (itemText) {
      ...
   });
}

Library.findListItem(xxx, yyy).then(function(item){
   return item.click();
})
我对下面的代码有一个问题,要查找匹配的项,请在每次迭代中将
itemText
的一个字符与整个字符串
findItem
进行比较。您确定可以找到具有下面代码的匹配项吗

 for (let k = 0; k < itemText.length; k++) {
    itemFound = true;
    console.log(itemText[k] + ' : ' + findItem);
    if (itemText[k] === findItem) {
      z = k + 1;
      console.log('found ' + z);
    }
  }
for(设k=0;k
函数使用函数标题中的“any”返回值。itemlist的情况是,它上面定义的行获取下拉列表中的所有值,并使用.map将承诺转换为文本数组。itemlist.then会让它等待承诺解析,因此不会返回。itemList是一个字符串数组,因此itemList[k]是一个字符串,而不是一个字符。希望这有意义您确实声明了函数的返回值类型,但没有为函数给出任何显式的
return
语句,请仔细检查。因此,当您调用
findListItem()时
,它返回
未定义的
。谢谢,这帮了大忙。虽然它在使用const obj=调用函数后返回了一个承诺,但我必须使用obj。然后(函数(elem){elem.click();});有没有办法在承诺解决后返还obj?如果没有,这将起作用并再次感谢你!在你的情况下,无法返还不承诺。我理解并再次感谢你。你已经解决了我的问题。