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.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. Documentation is not clear if i pass custom header [like authentication], how to get response code.
responseEventConfigureOpts
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.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'));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.