Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/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
Jasmine 量角器,如何获取href,多个,值_Jasmine_Protractor - Fatal编程技术网

Jasmine 量角器,如何获取href,多个,值

Jasmine 量角器,如何获取href,多个,值,jasmine,protractor,Jasmine,Protractor,如何获得每个“a”的href值 要仅获取所有href元素的所有a值,可以执行以下操作: element.all(by.tagName('a')).map((function (el) { return el.getAtrribute('href'); }); 您可能要检查页面上是否存在一组链接。您可以通过以下方式实现这一点: // the urls you are expecting to be on the page var expectedUrls = ['/home', '/s

如何获得每个“a”的href值


要仅获取所有
href
元素的所有
a
值,可以执行以下操作:

element.all(by.tagName('a')).map((function (el) {
  return el.getAtrribute('href');   
});
您可能要检查页面上是否存在一组链接。您可以通过以下方式实现这一点:

// the urls you are expecting to be on the page
var expectedUrls = ['/home', '/sequence', '/other'];
element.all(by.tagName('a')).map((function (el) {
    return el.getAttribute('href');
})).then(function (urls) {
  urls.forEach(function (url) {
    expect(expectedUrls.some(function (expUrl) {
      return expUrl === url;
    })).toBeTruthy();
  });
});
说明:
element.all(按.tagName('a'))
收集所有a元素

.map()
将所有元素转换为其
href

array.some()
检查是否至少有一个元素已满

=>


如果您有任何问题或提供的代码不起作用,请告诉我。(没有测试)

我用了一种稍微不同的方法。我喜欢我可以轻松地添加更多的验证,并期望在循环中的每个元素上进行验证;希望其他人会觉得这很有帮助。任何反馈都将不胜感激

// Ideally you'd be using POM
const tempElements = element.all(by.css('a'));

// Check for '/' in url and print list of hrefs to console.
async function getElementsLinks(elements: ElementArrayFinder){
  const tempList = [];
  for (let i = 0; i < await elements.count(); i++) {
    const temp = await elements.get(i).getAttribute('href');
    expect(temp).toContain('/');
    tempList.push(temp);
  }
  console.log(tempList);
}

// Call the function on an ElementArrayFinder object
await getElementsLinks(tempElements);
//理想情况下,您应该使用POM
const tempElements=element.all(by.css('a'));
//检查url中的“/”并将HREF列表打印到控制台。
异步函数getElementsLinks(元素:ElementArrayFinder){
圣堂武士=[];
for(设i=0;i
您需要参考i。否则,元素(by.css('a')将不断重复获取第一个元素。但是使用.each()可能更干净。此外,我认为by.tagName('a')可能不太可能匹配您不想要的内容(尽管它可能是等效的)“-失败:未定义expectedUrls”expectedUrl或expectedUrl未被识别。答案正确。谢谢,它可以工作。我只需要在数组中放置完整的URL。const expectedUrl=['','','','','','','','','';是的,
expectedUrl
的内容只是虚拟内容。
element.all(by.tagName('a')).map((function (el) {
  return el.getAtrribute('href');   
});
// the urls you are expecting to be on the page
var expectedUrls = ['/home', '/sequence', '/other'];
element.all(by.tagName('a')).map((function (el) {
    return el.getAttribute('href');
})).then(function (urls) {
  urls.forEach(function (url) {
    expect(expectedUrls.some(function (expUrl) {
      return expUrl === url;
    })).toBeTruthy();
  });
});
// Ideally you'd be using POM
const tempElements = element.all(by.css('a'));

// Check for '/' in url and print list of hrefs to console.
async function getElementsLinks(elements: ElementArrayFinder){
  const tempList = [];
  for (let i = 0; i < await elements.count(); i++) {
    const temp = await elements.get(i).getAttribute('href');
    expect(temp).toContain('/');
    tempList.push(temp);
  }
  console.log(tempList);
}

// Call the function on an ElementArrayFinder object
await getElementsLinks(tempElements);