首页 > 解决方案 > 很多时候 cypress 无法访问 /auth 页面并且找不到 id 或 class

问题描述

我正在尝试/auth使用以下代码访问路径并使用 cypress 登录:

Cypress.Commands.add('login', (email, password) => {
  cy.get('.auth').find('.login').should(($login) => {
    expect($login).to.contain('auth.welcome')
  })

  cy.get('[name="email"]').type(email);
  cy.get('[name="password"]').type(password);

  cy.get('button.login')
    .click();
})

但赛普拉斯失败并显示以下错误消息:

AssertionError: Timed out retrying: Expected to find element: `.auth`, but never found it.

有时代码有效,有时失败。

我的登录页面 url 是 (http://localhost:3000/) 或 (http://localhost:3000/auth)

标签: javascriptvue.jscypress

解决方案


此命令将等待元素:

cy.get('.auth').should('be.visible');

在与之交互之前添加此命令。


推荐阅读