testcafe是否支持其余api的测试 [英] Does testcafe support testing of rest api

查看:77
本文介绍了testcafe是否支持其余api的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您尝试直接测试Rest API网址时,测试会在testcafe浏览器中挂起.

Tests hang in the testcafe browser when you try to test a rest api url directly.

我试图使用请求挂钩对其余API端点运行测试,但是当我从命令行运行测试时,浏览器会打开API端点并加载并挂起.测试不会通过或失败并挂起.其余的API端点仅返回JSON响应.

I am trying to run a test against my rest API endpoint using request hooks, but when I run the test from the command line, the browser opens the API endpoint and loads it and hangs. The test doesn't pass or fail and hangs. The rest API endpoint just returns a JSON response.


const logger = RequestLogger('https://example.com/search/suggestions?search=testkeyword');

fixture `test`
    .page('https://example.com/search/suggestions?search=testkeyword');

test
    .requestHooks(logger)
    ('test', async t => {

        // Ensure that the response has been received and that its status code is 200.
        await t.expect(logger.contains(record => record.response.statusCode === 200)).ok();

        const logRecord = logger.requests[0];

        console.log(logRecord.userAgent);           
        console.log(logRecord.request.url);         
        console.log(logRecord.request.method);      
        console.log(logRecord.response.statusCode); 
    });

我希望测试通过200个状态代码的检查,但是该测试挂起而未显示通过/失败. testcafe是否支持其余API端点的测试?我已经检查了此问题- https://github.com/DevExpress/testcafe/issues/1471 ,其中说testcafe不支持非HTML页面.请确认.

I expect the test to pass checking for 200 status code, but the test hangs without showing pass/fail. Does testcafe support testing of rest API endpoints? I have checked this issue - https://github.com/DevExpress/testcafe/issues/1471 where it says testcafe doesn't support non-html pages. Please confirm.

推荐答案

您是正确的,TestCafe适用于html页面,但是如果您未定义任何url,它将使用"about:blank"页面.您可以使用常规的node.js HTTP API在没有请求的情况下会出现这种情况.看下面的例子:

You are right, TestCafe is intended to work with html pages, but it will use the "about:blank" page if you don't define any url. You can use the regular node.js HTTP API without request hooks for this case. Look at the following example:

import http from 'http';

const getResponseData = (url) => new Promise((resolve, reject) => {
    http.get(url, res => {
        const { statusCode } = res;
        const contentType = res.headers['content-type'];

        res.setEncoding('utf8');
        let rawData = '';
        res.on('data', (chunk) => { rawData += chunk; });
        res.on('end', () => resolve({ statusCode, contentType, rawData }));
    }).on('error', e => reject(e));
});

fixture `Test REST API`;

test('Simple Test', async t => {
    const response = await getResponseData('http://example.com/api/1');
    await t
        .expect(response.statusCode).eql(200);
});

这篇关于testcafe是否支持其余api的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆