【问题标题】:Test failing with " testRegistration.forEach is not a function "测试失败并显示“testRegistration.forEach 不是一个函数”
【发布时间】:2023-01-14 18:29:56
【问题描述】:

对于以下测试,我在柏树中遇到以下错误。

*以下错误源自您的测试代码,而非赛普拉斯。

testRegistration.forEach 不是函数*

export const testRegistration = ({firstName, lastName, email, password, confirmPassword}) ={

  cy.get('input[name="firstName"]').type(firstName)

  cy.get('input[name="lastName"]').type(lastName)
  
  cy.get('input[name="email"]').type(email)
 

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

  cy.get('input[name="confirmPassword"]').type(confirmPassword)

  return cy.contains('button', 'Register').click()

}

我试图用 foreach 缩短上面的代码,

export const testRegistration = ({firstName, lastName, email, password, confirmPassword}) => {
      
  return cy.contains('button', 'Register').click()
        
}

testRegistration.forEach(function (argument, index) {

  cy.get(`input[name= "${argument}"]`).type(argument);

});

【问题讨论】:

    标签: javascript cypress


    【解决方案1】:

    我想你想在传入参数属性上 .forEach()

    export const testRegistration = (options, buttonText) => {
          
      Object.keys(options).forEach(key => {
        cy.get(`input[name="${key}"]`).type(options[key])
      })
      return cy.contains('button', buttonText).click()
    }
    

    【讨论】:

    • 感谢您的回答,它正在工作。在使用此答案进行测试时,首先出现语法错误。将末尾的')'移动到返回之前,然后它就可以工作了
    • 干得好,我错放了那个支架。
    • 我需要为所有测试动态传递按钮值,例如按钮文本将根据注册更改,登录 => "return cy.contains('button', 'Register').click() ",我可以通过选项传递它吗?
    • 最好将它作为第二个参数传递,这样它就不会与类型命令混淆。
    猜你喜欢
    • 1970-01-01
    • 2010-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 2017-02-11
    • 1970-01-01
    相关资源
    最近更新 更多