Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypress: cy.task() with multiple arguments

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.

like image 928
p2018 Avatar asked Oct 18 '25 18:10

p2018


1 Answers

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');
       })
like image 163
nimo Avatar answered Oct 21 '25 07:10

nimo