I have the following bit of code for a cypress custom command, which I use to generate a user session cookie:
Cypress.Commands.add('getSession', (email, password) => {
return cy.request({
url: 'xxx',
method: 'POST',
body: {
email,
password,
}
})
.then((response) => {
let body = (response.body);
expect(response.status).to.eq(200);
cy.log('Id - ' + body.customerId);
let session = "SESSIONID=" + body.Cookies.SESSIONID + ";" ...
cy.log('raw session data - ' + session)
//Base64 encode
cy.writeFile('tmp/rawStr.txt', session, 'utf8');
cy.readFile('tmp/rawStr.txt', 'base64').then((cookie) => {
cy.log('base64 string - ' + cookie);
});
})
});
I would like to re-use the value in 'cookie' in another custom command, but whenever I run the test it says cookie is undefined. All I'm doing in that test is creating a Cookie header which includes the base64 string I create in the above step.
The custom command in question looks like this:
Cypress.Commands.add('createThing', (name) => {
return cy.request({
url: 'xxx',
method: 'POST',
headers: {
'Cookie' : 'client_token=' + cookie,
'Content-Type' : 'application/json'
},
body: {
'name' : name
}
})
.then((response) => {
let body = (response.body);
expect(response.status).to.eq(200);
cy.log(body);
});
});
How do I get the custom commands to share that cookie value?
My spec file calls the custom commands like so:
/// <reference types="Cypress" />
describe('deletes a user thing', function() {
it('create a thing via the api', function() {
cy.getSession('xxxx', 'xxxxx')
cy.createThing('thing')
})
})