【问题标题】:Freeze redirect in cypress在柏树中冻结重定向
【发布时间】:2022-06-11 09:42:24
【问题描述】:

我想测试一个 GA4 产品点击事件。为此,我在数据层中写入了所需的数据,并且我想查看数据层中是否有正确的数据。但是,当我在 Cypress 中单击产品时,重定向速度比测试读取数据层的速度要快。有什么方法可以暂停或冻结重定向?

这里是Datalayer中的期望数据:

                select_item: {
                    event: 'select_item',
                    ecommerce: {
                        item_name: 'Artiklename',
                        item_id: '000000',
                        price: 1.19,
                        currency: 'EUR',
                        item_brand: 'Brand',
                        item_category: 'category',
                        item_category2: 'category2',
                        item_category3: 'category3',
                        item_category4: 'category4',
                        index: 1,
                        quantity: 1,
                        item_list_name: "List Name"
                    },
                },

这里是实际测试:

context('Google Analytics 4: should track select_item event', function () {
    it('should track select item on search page', function () {
        cy.getTrackingData('expressShippingArticle', 'select_item').then(
            (expectedSelectItemEvent) => {

                // act
                cy.visitWithBasicAuth(
                    routes.category.resultList(
                        '000/SomeArticle'
                    )
                )
                
                //assert
                cy.getSpecificEventFromDataLayer('select_item').then(
                    (event) => {
                        cy.wrap(event).should('not.exist')
                    }
                )
                
                // act
                cy.get(selectors.resultList.productInResultList)
                    .first()
                    .click()
                
                cy.getSpecificEventFromDataLayer('select_item').then(
                    (actualSelectItemEvent) => {
                        cy.wrap(actualSelectItemEvent, { timeout: 0 }).should(
                            spok(expectedSelectItemEvent)
                        )
                    }
                )
            }
        )
    })    
})

【问题讨论】:

    标签: testing cypress end-to-end google-analytics-4


    【解决方案1】:

    不确定这是否适用于 GA4,但如果您想延迟网络请求/响应,请添加带有延迟选项的拦截

    cy.intercept(redirect-url, (req) => {
      req.reply(res => {
        res.setDelay(2000)
      })
    })
    
    // act
    cy.get(selectors.resultList.productInResultList).first().click()
    ...
    

    【讨论】:

      猜你喜欢
      • 2018-11-04
      • 2020-08-10
      • 2021-01-27
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 1970-01-01
      相关资源
      最近更新 更多