赛普拉斯-如何检查在加载新页面之前触发的Google事件

问题描述

这是场景:

  • 用户访问index.com
  • 点击X按钮
  • 在此触发了Google Analytic事件
  • 用户被重定向到新页面

这是我的代码:

describe("Desktop - Listing details - Google Analytics",() => {
  slugs.forEach((slug,index) => {
    context(`For ${slugs[index]}`,() => {
      beforeEach(() => {
        const ga = cy.stub().as("ga");
        cy.on("window:before:load",win => {
          Object.defineProperty(win,"ga",{
            configurable: false,get: () => ga,// always return the stub
            set: () => {} // don't allow actual google analytics to overwrite this property
          });
        });
        cy.visit(`/url/a/${slug}/`);
      });

      context("Navigation tab",() => {
        it("Pricing tab",function() {
          cy.get(`[class="nav-links"] > li`)
            .eq(1)
            .click(); // Here a new page loads and the events are not longer present
          cy.get("@ga").should(`be.calledWith`,`send`,{
            hitType: "event",eventCategory: "navigation-bar",eventAction: "go-to_product_pricing",eventLabel: "navigation-tab"
          });
        });
      });
    });
  });
});

第二次尝试:

Cypress.on('window:before:load',(win) => {
  // because this is called before any scripts
  // have loaded - the ga function is undefined
  // so we need to create it.
  win.ga = cy.stub().as('ga')
})

describe("Desktop - Listing details - Google Analytics",() => {
      beforeEach(() => {
             cy.visit(`/url/a/${slug}/`);
      });

      context("Navigation tab",eventLabel: "navigation-tab"
          });
        });
      });
    });
  });
});

问题是赛普拉斯正在“新建”页面中声明,而我要检查的事件不再存在。

我该如何测试?老实说,我尝试了一切。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...