【问题标题】:Make a Real HTTP Request Using TestCafe使用 TestCafe 发出真正的 HTTP 请求
【发布时间】:2020-06-08 21:05:19
【问题描述】:

由于严格通过前端自动化我们工作流的某些部分的复杂性,我们需要在前端自动化测试运行之前发出 HTTP 请求以设置测试数据。

使用 TestCafe 文档,我尝试将一些东西拼凑在一起,当测试运行时,http 请求没有得到执行。这是我的代码:

import {Selector, ClientFunction, RequestHook, RequestLogger} from 'testcafe';
import https from 'https';


fixture `Call Create Test Move`
    .before(async ctx => {
        test('test', async t => {
            const executeRequest = () => {
                return new Promise(resolve => {
                    const options = {
                        method: 'POST',
                        uri: 'https://api.com/move/sample',
                        headers: {
                            "X-Company-Secret": "xxxxxxx",
                            "X-Permanent-Access-Token": "xxxxxxx"
                        },
                        body: {
                            companyKey: 'xxxxxx'
                        },
                        json: true
                    };

                    const req = https.request(options, res => {
                        console.log('statusCode:', res.statusCode);
                        console.log('headers:', res.headers);
                        resolve();
                    });

                    req.on('error', e => {
                        console.error(e);
                    });

                    req.end();
                });
            };

            await executeRequest();
        });
    });

我对 JS 不是很熟悉,所以可能在这里做了一些明显错误的事情,只是不确定是什么。

【问题讨论】:

    标签: javascript testing automated-tests e2e-testing testcafe


    【解决方案1】:

    TestCafe 在 Node.js 环境中运行用户编写的测试代码。这意味着您可以在测试中编写任何自定义 JavaScript 代码,并使用任何第三方库和模块来发出请求。

    RequestHooks 机制旨在模拟或记录来自您页面的请求,而不是发送请求。要从您的测试代码发出 HTTP 请求,您可以使用标准的 https nodejs 模块。这是一个例子:

    import https from 'https';
    
    const executeRequest = () => {
        return new Promise(resolve => {
            const options = {
                hostname: ' https://api.com/move/sample',
                port:     443,
                path:     '/',
                method:   'POST'
            };
    
            const req = https.request(options, res => {
                console.log('statusCode:', res.statusCode);
                console.log('headers:', res.headers);
                resolve();
            });
    
            req.on('error', e => {
                console.error(e);
            });
    
            req.end();
        });
    };
    
    fixture `fixture`
        .page `http://google.com`
        .beforeEach(async t => {
            await executeRequest();
        });
    
    test('test', async t => {
        // test code
    });
    

    此外,请查看讨论过类似查询的这些线程:

    How to make post request with data in test-cafe?

    Make external request

    【讨论】:

    • 按照您链接的示例进行了尝试,但没有成功,可能是由于尝试与夹具 before 挂钩? fixture \`Call Create Test Move\` .before(async ctx => { test('test', async t => {
    • 嗨,我更新了答案并添加了详细信息。您能否提供完整的测试代码?您的评论似乎被删减了。
    • 您不需要将整个测试放在before 方法中。请参阅@AlexKamaev 的代码。将您的测试放在before 方法之外。
    猜你喜欢
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-06
    • 2019-10-20
    • 1970-01-01
    相关资源
    最近更新 更多