https://cypress.io logo
Join Discord
Powered by
# e2e-testing
  • a

    acceptable-solstice-90676

    06/16/2022, 3:35 PM
    Hey guys, do you know if
    cy.clearCookie('sessionID')
    works in the
    before
    or
    beforeEach
    hooks? I have this in the
    e2e
    file:
    Copy code
    Cypress.Cookies.defaults({
        preserve: 'sessionID'
      })
    However, there are some spec.js file where I do not want to use the
    sessionID
  • r

    red-toddler-79937

    06/16/2022, 5:20 PM
    ```ts function login() { cy.intercept({ method: 'POST', url: '/api/auth/login', }).as('loginResponse'); cy.visitPage('login', 'foodeli-auth-template'); cy.fillField('email', 'info@foodeli.nl'); cy.fillField('password', 'password'); cy.clickButton('submit'); cy.wait('@loginResponse').its('response.statusCode').should('equal', 200); cy.get('@loginResponse').then((response) => { return response; // Contains the token. }); } ```Does anyone know how I can store the response property in a variable? I cannot access any property because it's a JQueryHTML something.
  • n

    nutritious-honey-65632

    06/16/2022, 6:23 PM
    create an alias. Try
    cy.wrap(response).as('someResponse')
  • g

    gray-kilobyte-89541

    06/16/2022, 6:56 PM
    studying Cypress Network Testing course would be useful https://cypress.tips/courses
  • r

    red-toddler-79937

    06/16/2022, 10:29 PM
    Hey I tried this but didn't work
  • r

    red-toddler-79937

    06/16/2022, 10:29 PM
    Hey cool suggestion! but i for now just want something to fix the current problem
  • g

    gray-kilobyte-89541

    06/16/2022, 10:55 PM
    sure, you can store the response (an object) or one of its properties like the token using an alias, or use it via
    cy.then
    . See for example https://slides.com/bahmutov/flexible-cypress-data
  • r

    red-toddler-79937

    06/16/2022, 11:01 PM
    yep, i found a way meanwhile, thanks!!
  • r

    red-toddler-79937

    06/16/2022, 11:01 PM
    😄
  • r

    red-toddler-79937

    06/16/2022, 11:12 PM
    Copy code
    cy.get('@loginResponse').its('response.body.data').then((token) => {
        setToggleState(1, token);
      })
    I did this
  • r

    red-toddler-79937

    06/16/2022, 11:12 PM
    works now
  • r

    red-toddler-79937

    06/16/2022, 11:15 PM
    Copy code
    cy.get('@loginResponse').its('response.body.data').as('token');
    I also tried this but I cannot access the value outside of the method
  • n

    nutritious-honey-65632

    06/16/2022, 11:20 PM
    cy.get('@token')
    should yield data obj then you need
    then
    again to pass it to some function
  • r

    red-toddler-79937

    06/16/2022, 11:30 PM
    woahhhh that works!!! thanks this is very valuable information!!
  • r

    red-toddler-79937

    06/16/2022, 11:30 PM
    😄
  • r

    red-toddler-79937

    06/16/2022, 11:30 PM
    this is better than my original code
  • r

    red-toddler-79937

    06/16/2022, 11:39 PM
    Hey what would happen if you put both before and beforeach in the same describe? would it execute them from top to bottom or something? or will it pick one of the two?
  • r

    red-toddler-79937

    06/16/2022, 11:40 PM
    i know that foreach runs before for each test
  • r

    red-toddler-79937

    06/16/2022, 11:40 PM
    and that before runs before the first test
  • n

    nutritious-honey-65632

    06/16/2022, 11:46 PM
    before
    should run once before all tests in the describe and
    beforeEach
    should run before each test
  • r

    red-toddler-79937

    06/16/2022, 11:47 PM
    i know but what if i have both in the same testing block?
  • n

    nutritious-honey-65632

    06/16/2022, 11:49 PM
    before
    will run then
    beforeEeach
    then
    test1
    then
    beforeEach
    then
    test2
    then
    beforeEach
    then
    test3
  • r

    red-toddler-79937

    06/16/2022, 11:51 PM
    oww i got it thx!!
  • r

    red-toddler-79937

    06/16/2022, 11:52 PM
    does
    cy.get('@token')
    disappear from memory or something? Because I tried to access it again and the 2nd time it returned: ``cy.get() could not find a registered alias for: @token.`
  • n

    nutritious-honey-65632

    06/16/2022, 11:58 PM
    you should
    intercept
    only once and
    cy.get
    how many times you want. If you
    intercept
    twice it will search for second request which never happens in your case
  • r

    red-toddler-79937

    06/17/2022, 12:00 AM
    i only intercept once
  • r

    red-toddler-79937

    06/17/2022, 12:00 AM
    Copy code
    ts
    function login() {
      cy.intercept({
        method: 'POST',
        url: '/api/auth/login',
      }).as('loginResponse');
    
      cy.visitPage('login', 'auth-template');
      cy.fillField('email', 'email@email.com');
      cy.fillField('password', 'password');
      cy.clickButton('submit');
    
      cy.wait('@loginResponse').its('response.statusCode').should('equal', 200);
      cy.get('@loginResponse').its('response.body.data').as('token');
    }
  • r

    red-toddler-79937

    06/17/2022, 12:00 AM
    this function is only called one time in the before()
  • n

    nutritious-honey-65632

    06/17/2022, 12:09 AM
    doc says
    All intercepts are automatically cleared before every test.
    you should test how it works, not sure what this means if it is called in before for example
  • r

    red-toddler-79937

    06/17/2022, 12:19 AM
    Copy code
    ts
    function setToggleState(productId: number, state: boolean) {
        cy.get('@token').then((token) => {
            cy.request({
                method: "PUT",
                url:`/api/products/${productId}/update-availability`,
                headers: { Authorization: `Bearer ${token}` },
                body: { isAvailable: state }
            });
          });
    }
    Uhm, the second time i trigger this function i get the error: > "cy.get() could not find a registered alias for: @token. You have not aliased anything yet."
1...525354...192Latest