赛普拉斯,如何遍历元素?

Cypress, how to iterate through elements?

cy.get('div.infoTextCarousel').find('a.ProductInfoAnchor').should('have.attr', 'href', url)

有许多同名的 div 'div.infoTextCarousel',每个 div 中都有 'a.ProductInfoAnchor' 其中一个包含匹配的 href。所以我想要的是 cypress 一直在寻找匹配的 href 直到找到它,但问题是它只检查第一个 'div.infoTextCarousel' 和 'a.ProductInfoAnchor' 当它找不到匹配的 href 时它失败了。

如果您需要一些更复杂的行为,您可以将回调函数传递给 should()。在下面的代码中,我正在提取 href 属性,并期望属性列表包含特定的 url:

const url = '/something'
cy.get('div.infoTextCarousel')
 .find('a')
 .should($a => {
     let hrefs = $a.map((i, el) => {
      return Cypress.$(el).attr('href')})

      expect(hrefs.get()).to.contain(url)
  })

希望对您有所帮助。