使用each()中的invoke()访问href属性 Cypress
P粉231112437
P粉231112437 2023-12-12 09:41:00
0
2
442

我是 Cypress 的新手,我尝试使用 invoke() 从组中访问每个 div 标签的 href 属性,但它给出了错误。有人可以建议你如何做吗?

cy.get('.bms-scoreboard__game-tile--mls').each(($el,index,$list) => {
            $el.get('a')
                .invoke('attr','href')
                .then(href => {
                    cy.request(href)
                        .its('status')
                        .should('eq',200)
                })
        })

P粉231112437
P粉231112437

全部回复(2)
P粉276577460

$el 是一个 JQuery 元素,而不是它本身在 Cypress 链中。您需要使用 cy.wrap() 在 Cypress 链中使用它。

cy.get('.bms-scoreboard__game-tile--mls').each(($el,index,$list) => {
            cy.wrap($el)
                .get('a')
                .invoke('attr','href')
                .then(href => {
                    cy.request(href)
                        .its('status')
                        .should('eq',200)
                })
        })
P粉359850827

我认为 .get() 不合适 - 它仅适用于 <body>,不适用于每个 '.bms-scoreboard__game-tile--mls'

尝试使用 .find() 代替

使用 jQuery 运算符

cy.get('.bms-scoreboard__game-tile--mls')
  .each(($el,index,$list) => {
    const href = $el.find('a').attr('href')
    cy.request(href)
      .its('status')
      .should('eq', 200)
  })
})

或与赛普拉斯运营商合作

cy.get('.bms-scoreboard__game-tile--mls')
  .each(($el,index,$list) => {
    cy.wrap($el).find('a')
      .invoke('attr','href')
      .then(href => {
        cy.request(href)
           .its('status')
           .should('eq',200)
      })
  })
})

或将“查找”移至第一个选择器

cy.get('.bms-scoreboard__game-tile--mls a')
  .each($a => {
    const href = $a.attr('href')
    cy.request(href)
      .its('status')
      .should('eq', 200)
  })
})
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!