【发布时间】:2019-08-27 17:10:36
【问题描述】:
我正在尝试测试我的google tag manager 'productClick' event 是否在在点击之后但在在页面导航之前注册。这是我所拥有的:
describe("Test Product Listing Page", () => {
beforeEach(function() {
cy.visit("/productlisting");
});
...(other tests)...
it("Test GTM productClick", function() {
cy.window().then(win => {
// listen to GTM's dataLayer.push method
const dlp = cy.spy(win.dataLayer, "push");
// extract SKUID from first product on the page
cy.get('[data-js="productlisting"] [data-product-skuid]')
.first()
.invoke("attr", "data-product-skuid")
.then(skuid => {
// click on the first link in the item's container
cy.get(`[data-product-skuid="${skuid}"] a`)
.first()
.click()
.then(() => {
// passes in interactive mode but not CI mode!
expect(dlp).to.be.calledOnce;
// test that argument passed is the item clicked
expect(dlp.args[0][0].ecommerce.click.products[0].id).to.equal(skuid);
// test that GTM returns true
expect(dlp.returnValues[0]).to.be.true;
});
});
});
});
});
cy.spy() 似乎是我需要的,实际上它在交互模式下效果很好。但在 CI 模式下,它会失败:
AssertionError: expected push to have been called exactly once, but it was called twice
The following calls were made:
push(Object{3}) => true at Array.proxy [as push] (https://my.site.com/__cypress/runner/cypress_runner.js:62335:22)
push(Object{4}) => true at Array.proxy [as push] (https://my.site.com/__cypress/runner/cypress_runner.js:62335:22)
但只有在规范中有其他测试时!如果我将测试更改为it.only,它将以 CI 模式通过。其他测试正在测试与谷歌标签管理器无关的东西,但窗口中发生了 GTM push 调用。这几乎就像 cy.spy 在我告诉它之前就开始监视了。
我很困惑。我在这里错过了什么吗?有没有更好的方法来测试点击后导航前的内容?
【问题讨论】:
标签: javascript cypress end-to-end