gpt4 book ai didi

testing - 如何在 Testcafe 中记录 Google Analytics 调用?

转载 作者:行者123 更新时间:2023-11-28 20:19:44 25 4
gpt4 key购买 nike

我正在尝试自动测试跟踪代码,我正在使用 the RequestLogger from Testcafé .我成功拦截了对 example.comlocalhost 的调用,但没有拦截对 https://www.google-analytics.com/ 的调用。可能是什么原因?

预计

这个测试应该是绿色的

测试代码

import { RequestLogger } from 'testcafe';
const logger_ga = RequestLogger('https://www.google-analytics.com/');

fixture `localhost`
.page('http://localhost:8000')

test
.requestHooks(logger_ga)
('logs calls to Google Analytics', async t => {
await t.click("#ga-button");
console.log(logger_ga.requests); // is empty due to timing
await t.expect(logger_ga.contains(record => record.response.statusCode === 200)).ok();
});

测试夹具

我正在通过 python -m SimpleHTTPServer 8000

提供以下 index.html 页面
<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>Test page</title>
</head>

<body>
<p>Hello world!</p>

<!-- Google Analytics: change UA-XXXXX-Y to be your site's ID. -->
<script>
window.ga = function () { ga.q.push(arguments) }; ga.q = []; ga.l = +new Date;
ga('create', 'UA-XXXXX-Y', 'auto'); ga('send', 'pageview')
</script>
<script src="https://www.google-analytics.com/analytics.js" async defer></script>

<a onclick="ga('send', 'event', 'my_event_category', 'my_event_action', 'my_event_label');" href="#" id="ga-button">Google Analytics</a>
</body>

</html>

观察到

上面的测试是红色的

Google Analytics call interception is red

然而,这些测试是绿色的

import { RequestLogger } from 'testcafe';

const logger = RequestLogger('http://example.com');

fixture `example`
.page('http://example.com');

test
.requestHooks(logger)
('logs calls to example.com', async t => {
await t.expect(logger.contains(record => record.response.statusCode === 200)).ok(); // green
});


const logger_localhost = RequestLogger('http://localhost:8000');

fixture `localhost`
.page('http://localhost:8000');

test
.requestHooks(logger_localhost)
('logs calls to localhost', async t => {
await t.expect(logger_localhost.contains(record => record.response.statusCode === 200)).ok(); // green
});

如何成功拦截对 Google Analytics 的调用?

最佳答案

正如 Marion 所说,这可能是时间问题。以下代码有效:

import { Selector, RequestLogger } from 'testcafe';

const gaCollect = 'https://www.google-analytics.com/collect';

const gaLogger = RequestLogger({gaCollect}, {
logRequestHeaders: true,
logRequestBody: true,
});


fixture `Fixture`
.page('http://localhost:8000')
.requestHooks(gaLogger);

test('Log Google Analytics call', async t => {
await t.click('#ga-button')

await t.expect(gaLogger.contains(record =>
record.request.url.match(/ec=my_event_category&ea=my_event_action&el=my_event_label/))).ok();

for(let r of gaLogger.requests) {
console.log("*** logger url: ", r.request.url);
}

});

enter image description here

@Marion 提到的时间因素似乎起到了作用。将前面的代码段与以下代码段及其输出进行比较。在这里,我们没有看到记录到 https://google-analytics.com/collect 的调用.

fixture `Fixture`
.page('http://localhost:8000')
.requestHooks(gaLogger);

test('Log Google Analytics call', async t => {
await t.click('#ga-button')

for(let r of gaLogger.requests) {
console.log("*** logger url: ", r.request.url);
}

await t.expect(gaLogger.contains(record =>
record.request.url.match(/ec=my_event_category&ea=my_event_action&el=my_event_label/))).ok();
});

enter image description here

关于testing - 如何在 Testcafe 中记录 Google Analytics 调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51250340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com