javascript - 使用 testCafe 'requestLogger' 检索 chrome 性能日志失败

标签 javascript google-chrome automated-tests e2e-testing testcafe

这是此线程的延续: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.com 并打开开发人员工具> 网络选项卡。然后我点击了商店链接并捕获了日志。我尝试收集的请求 URL 已突出显示。我可以获取所有网址,然后过滤到我需要的网址。

enter image description here

下面的,我已经试过了

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

如果有人能指出正确的方向,我将不胜感激?

最佳答案

您在测试页面 ('https://appURL.com/app/home/students') 和记录器 ('https://studenthome.com') 中使用了不同的 url。这可能是原因。 您的请求记录器仅记录对“https://studenthome.com”的请求。

在您的屏幕截图中,我看到 url“http://store.google.com”,它与记录器 url 不同,因此记录器不会处理它。

您可以将正则表达式作为 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));
    });

关于javascript - 使用 testCafe 'requestLogger' 检索 chrome 性能日志失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55700475/

相关文章:

javascript - 使用 jQuery 更改元素的不透明度

css - 如何停止文本转换 : capitalize from capitalizing (s)?

javascript - Google Chrome 中的源映射是否会推送到 Error.stack

javascript - 退格键在 Safari 和 Chrome 中不起作用

php - 如何在一定时间后采取行动(因用户而异)?

javascript - 手机号码的正则表达式使括号作为国家代码的可选,破折号、点和空格是可选的

testing - 是否可以操纵 TestCafe 在测试运行结束时总结结果的方式?

build-automation - 学习高级批处理文件用法的最佳免费资源?

windows - 如何在 Windows 环境中模拟磁盘已满错误?

javascript - 从标题中删除 CloudFlare 脚本?