这是该线程的延续:Is there a way in TestCafe to validate Chrome network calls?
这是我的testCafe尝试从chrome检索所有网络日志(即开发人员工具中的“网络”标签)的方法。我在将任何东西打印在控制台上时遇到问题。
const logger = RequestLogger('https://studenthome.com',{
logRequestHeaders: true,
logRequestBody: true,
logResponseHeaders: true,
logResponseBody: true
});
test
('My test - Demo', async t => {
await t.navigateTo('https://appURL.com/app/home/students');//navigate to app launch
await page_students.click_studentNameLink();//click on student name
await t
.expect(await page_students.exists_ListPageHeader()).ok('do something async', { allowUnawaitedPromise: true }) //validate list header
await t
.addRequestHooks(logger) //start tracking requests
let url = await page_studentList.click_tab();//click on the tab for which requests need to be validated
let c = await logger.count; //check count of request. Should be 66
await console.log(c);
await console.log(logger.requests[2]); // get the url for 2nd request
});
I see this in console:
[Function: count]
undefined
以下是Google的图片,作为我要实现的目标的例证。我导航到google.com,并打开了开发者工具>网络标签。然后,我单击了商店链接并捕获了日志。我尝试收集的请求URL突出显示。我可以获取所有网址,然后过滤到所需的网址。
以下,我已经尝试过
await console.log(logger.requests); // undefined
await console.log(logger.requests[*]); // undefined
await console.log(logger.requests[0].response.headers); //undefined
await logger.count();//count not a function
如果有人能指出我正确的方向,我将不胜感激?
答案 0 :(得分:2)
您在测试页('https://appURL.com/app/home/students')和记录器('https://studenthome.com')中使用了不同的URL。这可能是原因。 您的请求记录器仅记录对“ https://studenthome.com”的请求。
在您的屏幕快照中,我看到了URL'http://store.google.com',它与记录器的URL不同,因此记录器不会对其进行处理。
您可以将RegExp作为RequestLogger构造函数的第一个参数传递给与您的RegExp匹配的所有请求。
我创建了一个示例:
import { RequestLogger } from 'testcafe';
const logger = RequestLogger(/google/, {
logRequestHeaders: true,
logRequestBody: true,
logResponseHeaders: true,
logResponseBody: true
});
fixture `test`
.page('http://google.com');
test('test', async t => {
await t.addRequestHooks(logger);
await t.typeText('input[name="q"]', 'test');
await t.typeText('input[name="q"]', '1');
await t.typeText('input[name="q"]', '2');
await t.pressKey('enter');
const logRecord = logger.requests.length;
console.log(logger.requests.map(r => r.request.url));
});