I have created a custom command cy.createUser("userCreate") in commands.js that creates a userID variable.
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.
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.
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
abcis persisted in the right places: