-2

What I am trying to do:

I have a custom command, inside which i am declaring few variables(boolean). I am setting the values for the variables as per my code. I wanna use the variables to assert in another part of the test. Example. Custom Command:

Cypress.Commands.add('XYZ', () => {
    var a,b,c,d
    a=b=c=d=false
    if(some condition){
      //some cy commands
      a=c=true
    }
    return a,b,c,d

})

Spec file:

it("A test", function () {
    cy.XYZ()    
    if(a){
    //do something  
    }
    else if(b){
    //do something 
    }
    ...
})

Cypress suggests to wrap variables and assign an alias to re use it in the test. But that looks good for a single variable. I wanna work with more than 10 variables in the custom command, and then use the variables in the test later down.

Is there any other solution to return multiple variables from a custom command and use them in the test?

1 Answer 1

2

First, this question really isn't about Cypress, but how JavaScript works.

Unfortunately, you can't return multiple values in JavaScript like how you're doing it. What you can do is add your variables to an array or an object. This way, you are returning one type with multiple values. Then when you need to use the returned values, you can read the array index/object keys, or using array/object destructuring.

1
  • 1
    Thanks Lee. Learnt something new about JS from your answer. Would like to point out...Cypress throws an error if you are returning any variable from a custom command. I went ahead and warped the variables and assigned an alias, to be used anywhere in the test. Apr 7, 2021 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.