javascript - Cypress: cy.task() with multiple arguments - Stack Overflow

admin2025-04-19  0

What I am trying: Pass multiple arguments in cy.task() mand 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() mand

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.

What I am trying: Pass multiple arguments in cy.task() mand 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() mand

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.

Share Improve this question asked Jan 2, 2021 at 9:06 p2018p2018 1252 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 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');
       })

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

on('task', {
    loadUserAccountDetails(site, userDetails): validUserDetails(site, userDetails)
})
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745021650a280417.html

最新回复(0)