admin管理员组文章数量:1420220
I am trying to write a sample test in testcafe to verify api response code.
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('')
.requestHooks(jwtBearerAuthorization);
test('basic', async t => {
await t
.expect(jwtBearerAuthorization.contains(r => r.response.statusCode === 200)).ok();
});
I am trying to write a sample test in testcafe to verify api response code.
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./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. 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
Share Improve this question edited Nov 5, 2018 at 20:59 Alex Skorkin 4,2743 gold badges27 silver badges48 bronze badges asked Oct 8, 2018 at 23:34 GauravGaurav 1512 silver badges14 bronze badges2 Answers
Reset to default 4responseEventConfigureOpts
is an object with the includeHeaders
and includeBody
properties. To set them, it is sufficient to pass the { includeHeaders: true/false, includeBody: true/false }
object as a second parameter in the RequestHook
constructor.
In addition, you don't need to write any hook related logic out of the onRequest
or onResponse
methods. Take a look at this documentation:
The
onRequest
method is called before sending the request. 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.
So you can check statusCode of the response in the onResponse
method:
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.`;
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'));
本文标签: javascripttestcafe for api testhow to verify response code with custom requesthookStack Overflow
版权声明:本文标题:javascript - testcafe for api test - how to verify response code with custom requesthook - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745324432a2653523.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论