问题
Tests hang in the testcafe browser when you try to test a rest api url directly.
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);
});
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.
回答1:
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);
});
回答2:
Sorry I have a few questions about your answer Note: I dont know if here is the right place to write questions or I should open a new question. Please feel free to tell me and I'll open a new question
1) How would be an example with a post-restApi-Request? (which has headers, body and keycloak token)
2) I dont know why I am getting not getting statusCode: 200 with your example. I just run differebt uris with your example and I am getting these status code
fixture`Test REST API`;
test('Simple Test', async t => {
const myResponse0 = await getResponseData('http://example.com/api/1');
const myResponse1 = await getResponseData("http://microsoft.com");
const myResponse2 = await getResponseData("http://www.microsoft.com/de-de/");
console.log(myResponse0);
console.log(myResponse1);
console.log(myResponse2);
//await t.expect(response.statusCode).eql(301);
});
I am getting:
- http://example.com/api/1' --> status 404
- http://microsoft.com --> status 301
- http://microsoft.com/de-de/ --> status 301
来源:https://stackoverflow.com/questions/57282082/does-testcafe-support-testing-of-rest-api