4

What I am trying: Pass multiple arguments in cy.task() command and print those argument values declared in function mentioned in plugins/index.js file

Issue: The function print prints only the first argument value and undefined for a second argument

Code:

//test file with cy.task() command

class LoginPage {
    let site = abc
    let userDetails = xyz
    openPage(env, site, userDetails) {
        cy.task('loadUserAccountDetails', site, userDetails)
    }
}

module.exports = LoginPage

// plugins/index.js file where the event is registered with declared function

const validUserDetails = (site, userDetails) => {
  console.log('--->' + site) // This prints abc
  console.log('--->' + userDetails) // This prints undefined
}

module.exports = (on, config) => {
  // `on` is used to hook into various events Cypress emits
  // `config` is the resolved Cypress config

  on('task', {
    loadUserAccountDetails: validUserDetails
  })
}

Kindly help.

p2018
  • 125
  • 1
  • 12

2 Answers2

2

Looks like only one param is handled. But you can always pass in an object with the vars as properties.

 on("task", {
    async "rename"({var1, var2, var2}) {

 }

and in the .spec call it as

cy.task('rename', {var1: 'val1', var2:'val2', var3: 'val3'}, ()=>{
            console.log('renamed');
       })
nimo
  • 499
  • 4
  • 6
1

This worked by passing arguments on task registered at index.js file.

on('task', {
    loadUserAccountDetails(site, userDetails): validUserDetails(site, userDetails)
})
p2018
  • 125
  • 1
  • 12
  • 1
    Now Cypress has been improved and you can pass multiple elements through an object: https://docs.cypress.io/api/commands/task#Arguments – Don Diego May 27 '22 at 15:44