I have created a custom command cy.createUser("userCreate") in commands.js that creates a "userID"
Cypress.Commands.add("createUser", (payload) => {
cy.makeUserviaAPI(payload).then((userID) => {
cy.openUserProfile(userID);
})
});
I want to use this usedID in other methods also in my test.cy.js file. i.e. I want to pass userID as a variable in new method cy.updpateUser(userID)
beforeEach(() => {
cy.createUser("userCreate")
cy.updpateUser(userID)
});
I have tried following modifications:
Cypress.Commands.add("createUser", (payload) => {
cy.makeUserviaAPI(payload)
.then((userID) => {
cy.openUserProfile(userID);
})
.then((userID) => {
return userID;
})
});
beforeEach(() => {
let uid = cy.createUser("userCreate")
cy.updpateUser(uid)
});
But I am getting userID as null or with some windows object. Can someone please please guide me on how to pass return variable "userID" and use it in new function as updateUser(userID) in test.cy.js. Sorry I am new with JS and needs guidance.
NOTE: I can’t move updateUser() inside createUser() as they needs to be independent of each other.
Also there will be multiple functions in future that will be needing userID as primary parameter to operate.
2
Answers
Since you specify one spec file
test.cy.js
, it is possible to use an alias to wrap the value ofuserID
.Here I have simple versions of your commands to show the value
abc
is persisted in the right places:You can achieve same by adding a .then block at end and wrapping the variable that you need as a parameter in test.cy.js function.
});
test.cy.js
});