赛普拉斯应该有 class 断言不起作用

cypress should have class assertion not working

以下测试工作正常

  it('button has "contact-next-disabled" class', () => {
    cy.get('a[rel="next"]')
  })

  it('button has "contact-next-disabled" class', () => {
    cy.get('.contact-next-disabled')
  })

但是如果我将它们与 should 断言结合起来

  it('button has "contact-next-disabled" class', () => {
    cy.get('a[rel="next"]').should('have.class', '.contact-next-disabled')
  })

Cypress 给我一个断言错误

Timed out retrying after 4000ms: expected '<a.contact-next-disabled>' to have class '.contact-next-disabled'

为什么会这样?

您必须从 class 名称中删除 .,它应该可以工作。

cy.get('a[rel="next"]').should('have.class', 'contact-next-disabled')