最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Cypress, how to iterate through elements? - Stack Overflow

programmeradmin2浏览0评论
cy.get('div.infoTextCarousel').find('a.ProductInfoAnchor').should('have.attr', 'href', url)

There are many divs with the same name 'div.infoTextCarousel' and within each there is 'a.ProductInfoAnchor' one of them contains matching href. So what I want is that cypress keep looking for the matching href until it'll be found, but the problem is that it only checks first 'div.infoTextCarousel' and within 'a.ProductInfoAnchor' when it's not able to found the matching href it fails.

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

There are many divs with the same name 'div.infoTextCarousel' and within each there is 'a.ProductInfoAnchor' one of them contains matching href. So what I want is that cypress keep looking for the matching href until it'll be found, but the problem is that it only checks first 'div.infoTextCarousel' and within 'a.ProductInfoAnchor' when it's not able to found the matching href it fails.

Share Improve this question asked Jul 17, 2019 at 9:34 JunaidJunaid 6747 gold badges18 silver badges37 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 2

You can pass a callback function to a should() in case you need some more sophisticated behaviour. In the below code I am extracting the href attributes, and expecting the list of attributes to contain a specific 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)
  })

Hope this helps.

发布评论

评论列表(0)

  1. 暂无评论