Reputation: 1389
I try to build an test with cypress. I need to set a cookie and an custom header field for the test.
With curl, it's easy, like:
curl -H "aut: BeUser1" --cookie "aut=BeUser1" ....
But I don't know who to set header and cookie in cypress:
cy.setCookie("aut", "BeUser1")
cy.visit("/some/foo");
Upvotes: 8
Views: 44673
Reputation: 1
You can use cy.getAllCookie()
. It will return an array of objects of all cookies. Use then()
as it returns a promise.
Upvotes: 0
Reputation: 11961
Can you try your case as below? Create a login()
inside commands.js and use cy.request()
to login to system and add the headers. Also before
the test, I have run the set cookie as given cy.setCookie("cookie", "your cookie details here");
Cypress.Commands.add("login", () => {
cy.request({
method: 'POST',
form: true,
url: 'your-url-here',
headers: {
'Content-Type': 'text/html',
'aut' : 'BeUser1',
},
body: {
"email": "your email",
//"username": "your username", depends upon your system login you could use email or username
"password": "your password",
}
}).then(response => {
const target = response.body.email;
})
})
Later on inside the test I have used cy.getCookie('cookie')
to yield the cookie.
describe('Set header and cookie', function() {
before('set cookie',function(){
cy.setCookie("cookie", "add your your cookie here");
});
it.only('tests login', function() {
cy.login();
cy.getCookie('cookie')
.then((cookie) => {
console.log(cookie);
})
})
})
Upvotes: 8