2

I am using the following before my test to try and get a cookie value from the request

cy.request({
            method:'GET',
            url:`https://test:test!@mf-test-magento19.test.com/meat/meat-chicken.html`
            })
            .then((response) => {
                cy.getCookie('frontend').should('exist');
            })

However, it only returns the value null as it is not looking at the request only whats in the browser and to which I have not set the baseurl or have a cy.visit() currently. How can I get the value of a cookie in that request?

2 Answers 2

2

Cookies are in headers, so you need to get them there:

cy.request({
    method:'GET',
    url:`https://test:test!@mf-test-magento19.test.com/meat/meat-chicken.html`
})
.then((response) => {
    const cookies = response.headers['set-cookie'];
    cookies.forEach(cookie => {
        // parse cookies and do what you need with them
    });
})

Something more about this to read:

1
  • 1
    This helped a lot thank you! Also led me to understand what parsing is when I tried to read that cookie value thanks! Nov 20, 2020 at 9:43
0

Just started exploring cypress for my React application and pretty new to java script. I am stuck and unable to proceed, any suggestions please?

I have an array of cookie objects from which I want to retrieve and store the K/V pair (In my case it's -> "name: value") for the 4 arrays in the cookies. I am totally clueless how to fetch and store this value in a variable so i can use it in next request.

mycookies

code:

cy.request({ method: 'GET', url: https://myaplicationurl/login

}).then((response) => {


    expect(response.status).to.eq(200)
    const sessioncookies = cy.getCookies()
        .should('have.length', 4)
        .then((cookies) => {
         
            for (let key of Object.keys(cookies)) {

                var keys = key.values
                alert(key);
            }

            for (let value of Object.values(cookies)) {

                alert(value);
            }

            var valueAtIndex = cookies[0];
            const indexvalue = cy.log(valueAtIndex)


        })



})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.