Gestión de Cookies en Cypress: Métodos Tradicionales y Experimentales
Clase 3 de 29 • Curso de Cypress Avanzado
Contenido del curso
Clase 3 de 29 • Curso de Cypress Avanzado
Contenido del curso
Alberto Alejandro Foinquinos Benitez
Jorge Emilio Granados Guzmán
Micaela Orellano
Erick Vicentin
Alberto Alejandro Foinquinos Benitez
Alberto Alejandro Foinquinos Benitez
Iván Ceballos Vega
Jorge Emilio Granados Guzmán
Anderson David Chicaiza Tenesaca
Antonio Pineda
Gabriel Cayoja
Francisco Antonio Hernandez Sabatino
Cristian Urbano
Erick Vicentin
David steven Abril Pulecio
La mayor parte de esta clase esta obsoleta ya que en cypress 11 removieron la posiblidad de hacer varias cosas. mi version de cypress es la 12.17.1, les dejo mi codigo para quien le sirva
describe('Cookies', () => { beforeEach(() => { cy.session("Cookies",() => { cy.setCookie('nombre', 'Javier') }) }); it('Obtener las cookies', () => { cy.clearAllCookies() cy.visit("/") cy.getCookies().should('be.empty') }); it('Agregar una cookie', () => { cy.setCookie('nombre', 'Javier') cy.getCookies().should('have.length', 1) }); it('Obtener cookie especifica', () => { cy.getCookie('nombre').should('have.a.property', "value", "Javier"); }); });
GRACIAS POR TU APORTE
Una consulta, esto de cookies quedo deprecado en cypress 12 verdad?
Si, dejo algo que me sirvio para entender el uso de cy.session(), que vendria a ser el reemplazo de Cypress.Cookies: https://applitools.com/blog/whats-new-in-cypress-12
The more impactful change was the deprecation of Cypress.Cookies.default() and Cypress.Cookies.preserveOnce() APIs that were used for handling the behavior of clearing up and preserving cookies. With the introduction of cy.session(), these APIs didn’t fit well into the system. The migration from these commands to cy.session() might not seem as straightforward, but it is quite simple when you look at it.
For example, instead of using Cypress.Cookies.preserveOnce() function to prevent deletion of certain cookies you can use cy.session() like this:
beforeEach(() => { cy.session('importantCookies', () => { cy.setCookie('authentication', 'top_secret'); }) }); it('test A', () => { cy.visit('/'); }); it('test B', () => { cy.visit('/'); });
Also, instead of using Cypress.Cookies.defaults() to set up default cookies for your tests, you can go to your cypress/support/e2e.js support file and set up a global beforeEach() hook that will do the same as shown in the previous example.
Desde mi frustracion con el primer curso cypress con el bendito autocompletado por fin encontre como hacer que vscode haga sugerencias de codigo para cypress, dejo el aporte por aca: https://github.com/testing-library/cypress-testing-library Tal y como explican en la instalacion del enlace de github, simplemente instalan la libreria con node y crean un archivo .json en la raiz del proyecto (osea normalmente donde esta package.json) pegando el codigo que les dicen en el tutorial y listo, espero les sea de utilidad
En caso de que el jsconfig.json les de problemas, pueden evitar usarlo y simplemente importar la libreria añadiendo el import a la carpeta support en el archivo e2e.js
// Import commands.js using ES2015 syntax: import './commands' import "cypress-xpath" import '@testing-library/cypress/add-commands' // Alternatively you can use CommonJS syntax: // require('./commands')
La URL que está usando el profesor es https://pokedexpokemon.netlify.app
HTT ML
al final el profesor explica la forma como resolver el problema que se presenta con las nuevas versiones de cypress
Con las nuevas actualizaciones, ya no es necesario colocar: experimentalSessionAndOrigin: true
En el cypress.config.
SI quieren leer acerca del uso de cy.session para guardar un token la info esta en el siguiente link: https://docs.cypress.io/api/commands/session#Where-to-call-cysession
Cypress 15
describe('🍪 Pruebas Fundamentales de Cookies', () => { beforeEach(() => { // Visitamos la página antes de cada prueba en este contexto cy.visit('/'); }); it('✅ Debería iniciar sin cookies', () => { cy.getCookies().should('be.empty'); }); it('✅ Debería establecer una cookie correctamente', () => { // Establecemos la cookie solo para esta prueba cy.setCookie('nombre', 'Javier'); // Verificamos que se haya creado correctamente cy.getCookie('nombre') .should('exist') .and('have.property', 'value', 'Javier'); }); it('✅ Debería establecer y luego limpiar una cookie específica', () => { cy.setCookie('temporal', 'valorTemporal'); cy.getCookie('temporal').should('exist'); // Limpiamos la cookie dentro de la misma prueba cy.clearCookie('temporal'); cy.getCookie('temporal').should('not.exist'); }); });
Podrian agregar los recursos (repo/archivos) de este curso?
GitHub - platzi/curso-cypress-avanzado at 2da-clase-cookies
https://github.com/platzi/curso-cypress-avanzado/tree/2da-clase-cookies
Cod en ingles en Cypress Version 14:
describe('Cookies', () => { before(() => { cy.session('set-cookie', () => { cy.visit('/'); cy.setCookie('Name', 'Dave'); }); }); //ensures the test runs in a clean browser state on your page. beforeEach(() => { //Cypress clears all browser state between each test by default — including cookies, localStorage, and the page itself, thats why we have to visit the page again cy.visit('/'); }); it('should have the cookie set', () => { cy.getCookie('Name').should('have.property', 'value', 'Dave'); }); it('should clear the cookie', () => { cy.clearCookie('Name'); cy.getCookie('Name').should('be.null'); }); });