Velvet Star Monitor

Standout celebrity highlights with iconic style.

updates

How to extract and use a string value returned from cy.wrap()

Writer Mia Lopez

Cypress sees my returned strings as objects, so I'm trying to use cy.wrap() to resolve the value as string.

I have a cypress custom command, like so:

Cypress.Commands.add('emailAddress', () => { var emailAddress = 'testEmail-' + Math.random().toString(36).substr(2, 16) + '@mail.com'; return cy.wrap(emailAddress);
})

That I need the return value as a sting in my test:

beforeEach(() => { var user = cy.emailAddress().then(value => cy.log(value)); // [email protected] logonView.login(user) // object{5} })

How do I use the string value for my login and elsewhere in my test?

Something like: logonView.login(user.value)... but this doesn't work?

1 Answer

In Cypress, you cannot return value like this

var user = cy.emailAddress().then(value => cy.log(value));

Instead, you get the return value in the then .then callback:

cy.emailAddress().then((value) => { logonView.login(user)
});

So, for you test, you can instead do the following:

describe("My test", () => { beforeEach(() => { cy.emailAddress().then((value) => { logonView.login(user) }); }); it("should have logged into the App", () => { // Write your test here });
});

Or use a variable in the before each block, and access it later in the test:

describe("element-one", () => { let user; beforeEach(() => { cy.emailAddress().then((value) => (user = value)); }); it("it should have user value", () => { expect(user).to.includes("testEmail"); });
});
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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.