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 badges
Add a ment  | 

2 Answers 2

Reset to default 4

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 the onResponse 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