【问题标题】:Cypress.io page objects cause 'cy.click() failed because it requires a DOM element.' errorCypress.io 页面对象导致“cy.click() 失败,因为它需要 DOM 元素。”错误
【发布时间】:2021-06-07 15:35:07
【问题描述】:

刚接触 cypress,但在 Protractor 和 TestCafe 做过几个项目。

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

重构测试以删除 PO 并包含应用 ID 的作品。使用页面对象,我们会收到“需要 DOM 元素”错误。

// 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);
    });
  });
});

【问题讨论】:

    标签: cypress pageobjects


    【解决方案1】:

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

    loginPage.forgotPasswordLink().click()
    

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

    【讨论】:

    • 就是这样。感谢您的快速回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    相关资源
    最近更新 更多