Test failing with " testRegistration.forEach is not a function "

Viewed 56

I am getting below error in cypress for the below test. I am not familiar with JS and Cypress.Is there any mistake in the code?

*The following error originated from your test code, not from Cypress.

testRegistration.forEach is not a function*

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()

}

I am trying to shorten the above code with 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);

});
1 Answers

I think you want to .forEach() on the incoming parameter properties

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