cypress - Cypress:有没有办法编写一个可以使用 UI 检查复选框的命令

目前,我正在关注下面的 cypress 示例,它非常适用于 type 命令。但是,我有太多的命令,我试图压缩我能压缩的那些。在这种情况下,我只需要能够在某些测试中选中一个框,但不确定我将如何去做。任何提示/提示/建议将不胜感激。 :)

Cypress.Commands.add('typeLogin', (user) => {

  cy.get('input[name=email]').type(user.email)
  cy.get('input[name=password]').type(user.password)
  cy.get('input[name=checkbox]').check(user.checkbox)?

})

在测试中:

const user = { email: 'fake@email.com', password: 'Secret1' }';

    cy.typeLogin(user ) => {...

回答1

您可以在数据模型中提供要使用的复选框(或是否选中/不选中复选框)。从那里,您有几个不同的选项,具体取决于您的应用程序的设置方式。如果您只有一个应选中或不选中的复选框,则可以使用布尔值(下面的 useCheckbox)。如果有多个 values 可供选择,则可以传入一个要使用的字符串(这可以是选择器,如果有不同的选择器,也可以是 value,如果它是具有不同 values 的相同选择器)。

const user = { email: 'foo@foo.com', password: 'foo', useCheckbox: true, checkbox: 'bar' }
...
Cypress.Commands.add('typeLogin', (user) => {
  cy.get('input[name=email]').type(user.email)
  cy.get('input[name=password]').type(user.password)
  // we could use either `useCheckbox` to evaluate a boolean
  if (user.useCheckbox) {
    cy.get('input[name=checkbox]').check()
  }

  // or we could use the the `checkbox` field if we always to to check, we're just unsure of the value.
  // in the first case, `user.checkbox` is the selector to use
  cy.get(user.checkbox).check()
  // in this second case, `user.checkbox` is the value to select
  cy.get('input[name=checkbox]').check(user.checkbox)
})

相似文章

随机推荐

最新文章