cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.96k stars 3.18k forks source link

custom event of gtag does not display and can not be intercept/spy #23528

Closed orly-from closed 2 years ago

orly-from commented 2 years ago

Current behavior

I want to test a gtag click event, but when I'm running my simple cypress test- the event is not displayed and can not be intercept/spy.

There are no 'collect' network calls displayed after the click. Screenshot_1

Desired behavior

Intercept and test this event: image

Test code to reproduce

it('Check socialShareIcons - events tests', function(){

   cy.intercept({method: 'POST', url: '*/collect?*',query: {t: 'event',ec:'socialShareIcons'},},
   cy.spy().as('spy')).as('post');

  cy.visit('https://www.gov.il/he/service/boost-monthly-scholarship');
  cy.get('#WhatsappURL_Desktop').click();

  cy.wait('@post').then(interception => {    
    cy.get('@spy').its('callCount').should('equal', 1)
    expect(interception.response.statusCode).to.eq(200)
  })
})

Cypress Version

7.7.0

Node version

12.19.0

Operating System

Windows 10

Debug Logs

No response

Other

No response

rachelruderman commented 2 years ago

Hi @orly-from ! It looks like this issue was reported earlier in https://github.com/cypress-io/cypress/issues/5047.

I tried out the workaround described in the issue and it worked for me! Can you try it out?

it('Check socialShareIcons - events tests', function(){

  cy.visit('https://www.gov.il/he/service/boost-monthly-scholarship');
  cy.get('#WhatsappURL_Desktop').click();

  cy.window().then(win => {
    const gtagEvent = win.performance
      .getEntriesByType('resource')
      .filter(({name}) => {
        return (
          name.startsWith('https://www.google-analytics.com/collect?') &&
          name.includes('t=event') && 
          name.includes('ec=socialShareIcons')
          )
        })

    // note: you may need to wait here if test is flaky b/c 'expect' is sometimes reached too soon
    expect(gtagEvent, 'gtagEvent').to.have.lengthOf(1)
  })
})

Let me know how it goes 🙂

orly-from commented 2 years ago

Thank you Rachel!

It is working. I needed to add cy.wait(200) before cy.window().

orly-from commented 2 years ago

Hi, It seems like sometimes cy.window() includes the specific event I need and sometimes not. Do you have any idea how to solve this problem?  

chrisbreiding commented 2 years ago

@orly-from Issues in our GitHub repo are reserved for potential bugs or feature requests. We recommend questions relating to how to use Cypress be asked in our Discord server. Also try searching our existing GitHub issues, reading through our documentation, or searching Stack Overflow for relevant answers.

JayKishoreDuvvuri commented 2 years ago

Hi @rachelruderman,

Thanks for your reply here. Can you help me on how to do the same for adobe analytics please?

Cheers, Jay.

orly-from commented 1 year ago

I think that something that sometimes works and sometimes doesn't is a bug. Unless you tell me otherwise...