Cypress.io 页面对象导致 'cy.click() failed because it requires a DOM element.' 错误

Cypress.io page objects cause 'cy.click() failed because it requires a DOM element.' error

cypress 的新手,但在 Protractor 和 TestCafe 中做过几个项目。

我知道在 cypress 中使用 PO 存在争议,但由于我们应用程序的复杂性/性质,我们将继续使用它。

重构测试以删除 PO 并包含应用 ID 的作品。对于页面对象,我们得到 'requires a DOM element' 错误。

// myPo.js

class LoginPage {
    loginPageCon() {
      return cy.get('#page-login');
    }
    forgotPasswordLnk() {
      return cy.get('#forgotPassword');
    }
    emailTxt() {
      return cy.get('#email');
    }
    forgotPasswordCon() {
      return cy.get('#page-forgot-password');
   }  
  }
  export default LoginPage;

// myTest.spec.js

import loginPage from '../pageObjects/myPo.js';
const loginPage = new LoginPage();

describe('Authorization', () => {
  it('can direct to the azure instance', () => {
    cy.visitHome();
    cy.get(loginPage.loginPageCon);
  });

describe('Forgot Password', () => {
  it('clicking forgot password sends you to the correct screen', () => {
      cy.get(loginPage.forgotPasswordLnk).click();
      cy.get(loginPage.forgotPasswordCon);
    });
  });
});

当您调用 cy.get(loginPage.forgotPasswordLink) 时,您将返回对 cy.get() 的函数引用。 将其更改为:

loginPage.forgotPasswordLink().click()

您的页面对象已经返回一个可链接的 cy.get()