问题描述
这是场景:
- 用户访问
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 (将#修改为@)