Skip to content

mockRequest with Puppeteer is not intercepting HTTP requests #2845

Closed
@sunny-goel

Description

@sunny-goel

What are you trying to achieve?

I am using I.mockRequest with puppeteer.
Post authenticating the user, Application requests some data from the backend by requesting HTTPS GET request to this endpoint: /api/v1/b2c/zones?limit=9007199254740991.
After start mocking, this API request should be intercepted and custom response should be returned according to my settings:

I.amOnPage('/');
I.click('Log In');
I.fillField('email', '[email protected]');
I.fillField('password', secret('****'));
I.click('Log In');
I.mockRequest('GET', '/api/v1/b2c/zones?limit=9007199254740991', 200, { "data": []});
I.see('List currently active routes')
I.pause();

What do you get instead?

I always get response from the server and my custom response is never gets dispatched .
I have tried putting before and after I.click('Log In'); statement but no change in behaviour.

Details

  • CodeceptJS version: ^3.0.5
  • NodeJS Version: v15.4.0
  • Operating System: mac
  • puppeteer : ^8.0.0
  • Configuration file:
exports.config = {
  tests: './*_test.js',
  output: './output',
  helpers: {
    Puppeteer: {
      url: 'XXXX',
      show: true,
      chrome: {
        args: [
          '--disable-web-security',
        ],
      },
      waitForNavigation: "networkidle0",
      windowSize: '1200x900'
    },
    MockRequestHelper: {
      require: '@codeceptjs/mock-request'
    }
  },
  include: {
    I: './steps_file.js'
  },
  bootstrap: null,
  mocha: {},
  name: 'Puppeteer',
  plugins: {
    pauseOnFail: {},
    retryFailedStep: {
      enabled: true
    },
    tryTo: {
      enabled: true
    },
    screenshotOnFail: {
      enabled: true
    }
  }
}

package.json

{
  "dependencies": {},
  "resolutions": {
    "@pollyjs/adapter-puppeteer": "^5.1.0",
    "@pollyjs/core": "^5.1.0"
  },
  "devDependencies": {
    "@codeceptjs/mock-request": "^0.3.0",
    "@pollyjs/adapter-fetch": "^5.1.0",
    "@pollyjs/adapter-puppeteer": "^5.1.0",
    "@pollyjs/adapter-xhr": "^5.1.0",
    "@pollyjs/core": "^5.1.0",
    "@pollyjs/persister-rest": "^5.0.0",
    "codeceptjs": "^3.0.5",
    "puppeteer": "^8.0.0"
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions