Google analytics Cypress-如何检查在加载新页面之前触发的Google事件
这是一个场景:Google analytics Cypress-如何检查在加载新页面之前触发的Google事件,google-analytics,cypress,qa,stub,stubbing,Google Analytics,Cypress,Qa,Stub,Stubbing,这是一个场景: 用户访问index.com 单击X按钮 这里触发了一个谷歌分析事件搜索这个问题的答案。你找到解决办法了吗? describe("Desktop - Listing details - Google Analytics", () => { slugs.forEach((slug, index) => { context(`For ${slugs[index]}`, () => { beforeEach(() =>
- 用户访问
index.com
- 单击X按钮
- 这里触发了一个谷歌分析事件搜索这个问题的答案。你找到解决办法了吗?
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", () => { slugs.forEach((slug, index) => { context(`For ${slugs[index]}`, () => { beforeEach(() => { 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" }); }); }); }); }); });