如何在出现错误时不间断地检查 cypress 中的所有链接

How to check all links in cypress without stopping in case of an error

所以基本上我必须在很多网站上测试很多很多 link,所以我想写一个非常通用的代码。在这种情况下,它只是故意检查主要部分。

it('check all links in main', () => {

    cy.visit('/')
    cy.get("main").within(() => {
        cy.get("a").each(page => {
            cy.request(page.prop('href'))
        })
    })
});

这将检查所有 link,但前提是没有错误。我希望它 运行 遍历整个页面,并在每个 link 是否有效时注销。它不应该在第一个错误后停止。

尝试将您的请求放入.then()实例中。

it('check all links in main', () => {

    cy.visit('/')
    cy.get("main").within(() => {
        cy.get("a").each(a => {
           cy.get(a).then((page) => {
           cy.request(page.prop('href'), {failOnStatusCode: false})
           cy.request({url: page.prop('href'), failOnStatusCode:false, })
           })
        })
    })
})

您似乎在寻找 failOnStatusCode 选项

cy.get("a").each(page => {
  const link = page.prop('href')
  cy.request({
    url: link,
    failOnStatusCode: false  // allow good and bad response to pass into then
  }).then(response => {
    Cypress.log({
      name: link,
      message: response.status
    })
  })
})