testcafe是否支持rest的测试api

Does testcafe support testing of rest api

当您尝试直接测试休息 api url 时,测试在 testcafe 浏览器中挂起。

我正在尝试 运行 使用请求挂钩针对我的其余 API 端点进行测试,但是当我 运行 从命令行进行测试时,浏览器会打开 API 端点并加载它并挂起。测试不通过或失败并挂起。其余 API 端点只是 returns 一个 JSON 响应。


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 状态代码,但测试挂起而没有显示 pass/fail。 testcafe 是否支持测试 rest API 端点?我已经检查了这个问题 - https://github.com/DevExpress/testcafe/issues/1471 它说 testcafe 不支持非 html 页面。请确认。

你是对的,TestCafe 旨在使用 html 页面,但如果你不定义任何 url,它将使用 "about:blank" 页面。对于这种情况,您可以使用没有请求挂钩的常规 node.js HTTP API。看下面的例子:

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);
});