Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onResponse not called for AJAX requests when hook constructor receives responseEventConfigureOpts #2190

Closed
fedebertolini opened this issue Nov 24, 2019 · 2 comments · Fixed by #2276

Comments

@fedebertolini
Copy link

What is your Test Scenario?

I implemented a custom hook extending RequestHook. In the hook's constructor I call super(null, { includeHeaders: true }) to listen to all requests and use the response headers.

What is the Current behavior?

The onResponse method is only invoked for non-ajax requests.

What is the Expected behavior?

The onResponse method is invoked for all requests.

What is your web application and your TestCafe test code?

I created a repo with the sample test: https://github.com/fedebertolini/testcafe-hooks-issue

My hook receives the responseEventConfigureOpts config as parameter, and will call super passing this object. I created three simple tests using the hook in 3 different ways:

  1. passing undefined
  2. passing an empty object
  3. passing { includeHeaders: true}

In onResponse I log the url of the ajax requests only. Only in the first case I can see the requests being loaded:

Screenshot 2019-11-24 at 10 37 33

Your complete test code (or attach your test files):
 import { RequestHook } from 'testcafe';

export default class CustomHook extends RequestHook {
  constructor(config) {
    super(null, config);
    this.networkRequests = {};
  }

  onRequest(event) {
    const id = event._requestContext.requestId;
    this.networkRequests[id] = {
      url: event._requestInfo.url,
      isAjax: event.isAjax,
    };
    if (event.isAjax) {
        console.log('request: ' + event._requestInfo.url);
    }
  }

  onResponse(event) {
    const request = this.networkRequests[event.requestId];
    if (!request) return;

    if (request.isAjax) {
        console.log('response: ' + request.url);
    }

    delete this.networkRequests[event.requestId];
  }
}
import customHook from './customHook';

fixture('google').page('https://www.google.com/');

test('With out config', () => {}).requestHooks(new customHook());

test('With empty config', () => {}).requestHooks(new customHook({}));

test('With includeHeaders', () => {}).requestHooks(new customHook({ includeHeaders: true}));
Your complete configuration file (if any):
No config
Your complete test report:
> testcafe chrome index.js

 Running tests in:
 - Chrome 78.0.3904.108 / macOS 10.14.6

 google
request: https://www.google.de/domainless/read?igu=1
response: https://www.google.de/domainless/read?igu=1
request: https://www.google.com/domainless/write?igu=1&data=&xsrf=AO61pXTlD4i518O7D3bSIKfaIcEDFzoNhA:1574588204976
response: https://www.google.com/domainless/write?igu=1&data=&xsrf=AO61pXTlD4i518O7D3bSIKfaIcEDFzoNhA:1574588204976
 ✓ With out config
request: https://www.google.de/domainless/read?igu=1
request: https://www.google.com/domainless/write?igu=1&data=&xsrf=AO61pXS3sSSX7Afhd3tGK3Y9R5ICC57Nmg:1574588208441
 ✓ With empty config
request: https://www.google.de/domainless/read?igu=1
request: https://www.google.com/domainless/write?igu=1&data=&xsrf=AO61pXRFp4rmwJ9BrdfVqefslQilGyyQ8w:1574588209606
 ✓ With includeHeaders

 3 passed (6s)

Steps to Reproduce:

  1. Clone https://github.com/fedebertolini/testcafe-hooks-issue
  2. Run npm i
  3. Run npm start (assuming you have chrome installed)
  4. Check the logs

Your Environment details:

  • testcafe version: 1.7.0
  • node.js version: v12.13.0
  • command-line arguments: testcafe chrome index.js
  • browser name and version: chrome 78.0.3904.108
  • platform and version: macOS 10.14.6
@AlexKamaev AlexKamaev self-assigned this Nov 25, 2019
@AlexKamaev
Copy link
Contributor

Thank you for pointing out to this issue.
I checked your repository and reproduced this behavior. At the moment I cannot say precisely that it is a bug. We need to research this issue in detail.

@lock
Copy link

lock bot commented Mar 27, 2020

This thread has been automatically locked since it is closed and there has not been any recent activity. Please open a new issue for related bugs or feature requests. We recommend you ask TestCafe API, usage and configuration inquiries on StackOverflow.

@lock lock bot added the STATE: Auto-locked Issues that were automatically locked by the Lock bot label Mar 27, 2020
@lock lock bot locked as resolved and limited conversation to collaborators Mar 27, 2020
miherlosev pushed a commit that referenced this issue May 21, 2020
* Server logging

* Use the debug module

* fix

* fix tests

* review

* fix messages
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
3 participants