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

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

问题描述

当您尝试直接测试 rest api url 时,测试会在 testcafe 浏览器中挂起.

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

我正在尝试使用请求挂钩对我的 rest 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 是否支持测试 REST 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是否支持测试rest api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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