[英]testcafe for api test - how to verify response code with custom requesthook
I am trying to write a sample test in testcafe to verify api response code.我正在尝试在 testcafe 中编写示例测试来验证 api 响应代码。
Below is my code下面是我的代码
import { RequestHook } from 'testcafe'; class JwtBearerAuthorization extends RequestHook { constructor () { super(); } onRequest (e) { e.requestOptions.headers['Authorization'] = 'some token'; e.requestOptions.headers['Content-Type'] = 'application/json'; } onResponse (e) { } } const jwtBearerAuthorization = new JwtBearerAuthorization(); fixture `Fixture` .page('http://mywebsite.com/api/example/learning_items') .requestHooks(jwtBearerAuthorization); test('basic', async t => { await t .expect(jwtBearerAuthorization.contains(r => r.response.statusCode === 200)).ok(); });
I am not sure how to set responseEventConfigureOpts
to true.我不确定如何将responseEventConfigureOpts
设置为 true。 Documentation is not clear if i pass custom header [like authentication], how to get response code.如果我传递自定义标头 [如身份验证],文档不清楚,如何获取响应代码。
http://devexpress.github.io/testcafe/documentation/test-api/intercepting-http-requests/creating-a-custom-http-request-hook.html http://devexpress.github.io/testcafe/documentation/test-api/intercepting-http-requests/creating-a-custom-http-request-hook.html
responseEventConfigureOpts
is an object with the includeHeaders
and includeBody
properties. responseEventConfigureOpts
是一个具有includeHeaders
和includeBody
属性的对象。 To set them, it is sufficient to pass the { includeHeaders: true/false, includeBody: true/false }
object as a second parameter in the RequestHook
constructor.要设置它们,只需将{ includeHeaders: true/false, includeBody: true/false }
对象作为RequestHook
构造函数中的第二个参数传递即可。
In addition, you don't need to write any hook related logic out of the onRequest
or onResponse
methods.此外,您不需要在onRequest
或onResponse
方法之外编写任何与钩子相关的逻辑。 Take a look at this documentation :看看这个文档:
The
onRequest
method is called before sending the request.在发送请求之前调用onRequest
方法。 Use this method to handle sending the request.使用此方法来处理发送请求。 You can change the request parameters before it is sent.您可以在发送之前更改请求参数。 When a response is received, the hook starts preparing to call theonResponse
method that handles the response.当收到响应时,钩子开始准备调用处理响应的onResponse
方法。
So you can check statusCode of the response in the onResponse
method:因此,您可以在onResponse
方法中检查响应的 statusCode:
onResponse (e) {
const code = e.statusCode;
}
UPD.更新。
import { RequestHook } from 'testcafe';
const allResponces = {};
class Hook extends RequestHook {
constructor (testName) {
super();
this.testName = testName;
allResponces[this.testName] = [];
}
onRequest (e) {
console.log('onRequest');
}
onResponse (e) {
console.log('onResponse');
allResponces[this.testName].push(e);
}
}
const getHook = (testName) => {
return new Hook(testName);
};
fixture `Hook`
.page `http://example.com`;
test('basic', async t => {
await t.click('h1');
await t.click('div');
console.log(allResponces['basic'].length);
await t.expect(allResponces['basic'].every(r => r.statusCode === 200)).ok();
}).requestHooks(getHook('basic'));
import { RequestHook } from 'testcafe';
const allResponces = {};
class Hook extends RequestHook {
testName;
constructor (testName) {
super();
this.testName = testName;
allResponces[this.testName] = [];
}
async onRequest (e) {
console.log('onRequest');
}
async onResponse (e) {
console.log('onResponse');
allResponces[this.testName].push(e);
}
}
const getHook = (testName) => {
return new Hook(testName);
};
fixture `Hook`
.page `https://devexpress.github.io/testcafe/example`;
test('basic', async t => {
await t.click('h1');
await t.click('#developer-name');
console.log(allResponces['basic'].length);
await t.expect(allResponces['basic'].every(r => r.statusCode === 200)).ok();
}).requestHooks(getHook('basic'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.