使用 TestCafe 发出真正的 HTTP 请求
Make a Real HTTP Request Using TestCafe
由于严格通过前端自动化我们工作流程的某些部分会很复杂,我们需要在前端自动化测试之前发出 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 不是很熟悉,所以可能在这里做了一些明显错误的事情,只是不确定它是什么。
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?
由于严格通过前端自动化我们工作流程的某些部分会很复杂,我们需要在前端自动化测试之前发出 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 不是很熟悉,所以可能在这里做了一些明显错误的事情,只是不确定它是什么。
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?