stocky-insurance-27750
03/07/2023, 4:23 PMcy.window().then(win => {
win.addEventListener('beforeunload', () => {
I'm getting errors with promises when trying to set the cookie using datalayer.find(event => event.dpdItem === 'header-2') or if I manage to get to the click - then the cookie is null. Even when I don't try capturing anything and just use a string 'hello' - the cookie is null.
Anyone got this working?wonderful-match-15836
03/07/2023, 5:30 PMcy.on('window:before:unload'), not the ChatGPT listener way. Can you share some code that shows what you are trying to do and explain what happens.
I'm not sure there is enough detail here so far for somebody to help you.stocky-insurance-27750
03/07/2023, 6:15 PMstocky-insurance-27750
03/07/2023, 6:15 PMit('Logo DPD works on click', () => {
cy.on('window:before:unload', () => {
cy.window().its('dataLayer').then(dataLayer => {
cy.setCookie('preRedirectDataLayer', JSON.stringify(dataLayer.filter(value => value.dpdItem === 'header-2')))
})
})
element.click()
cy.getCookie('preRedirectDataLayer').then(cookie => {
const event = JSON.parse(cookie.value) expect(event.event).to.equal('myeventname')
})
})stocky-insurance-27750
03/07/2023, 6:16 PMAssertionError: expected undefined to equal 'myeventname'stocky-insurance-27750
03/07/2023, 6:19 PMcy.on('window:unload', () => {
done()
})stocky-insurance-27750
03/08/2023, 10:19 AMit('test', () => {
let dataLayer
cy.window()
.its('dataLayer')
.then(window => {
dataLayer = window.dataLayer
})
cy.on('window:before:unload', () => {
cy.wrap(dataLayer)
.should('not.be.null')
.then(() => {
const dpdEventData = JSON.stringify(
dataLayer.filter(value => value.dpdItem === 'header-2')
)
cy.setCookie('dpd-event', dpdEventData)
})
})
element.click()
cy.getCookie('dpd-event').should('exist')
})
gives me the error: Cypress detected that you returned a promise from a command while also invoking one or more cy commands in that promise.
The command that returned the promise was:
> cy.click()
The cy command you invoked inside the promise was:
> cy.wrap()gray-kilobyte-89541
03/08/2023, 11:39 AMgray-kilobyte-89541
03/08/2023, 11:39 AMstocky-insurance-27750
03/08/2023, 12:16 PMdpdEventData = JSON.stringify(
dataLayer.filter(value => value.dpdItem === 'header-2')
)
document.cookie = 'dpd-event=' + dpdEventData
and that got me to the next problem 👍