Ignore SSL errors with playwright code generation

Viewed 8457

I am using the jest-playwright library (https://github.com/playwright-community/jest-playwright) to perform end-to-end testing. In the jest.config.js file you can set an option to ignore SSL errors:

contextOptions: {
   ignoreHTTPSErrors: true,
}

This works fine when running the tests with jest.
Now, I would want playwright to generate the code when clicking on website elements with the command npx playwright codegen example.com. However, playwright stops because of the SSL error when opening the website.

Is there an option to ignore SSL errors when using playwright code generation?

2 Answers

You can run codegen with custom setup. Just call page.pause() in your initial script which will open codegen controls if you run node my-initial-script.js.

The example code with browser.newContext({ ignoreHTTPSErrors: true }) would look like this:

// my-initial-script.js
const { chromium } = require('playwright');

(async () => {
  // Make sure to run headed.
  const browser = await chromium.launch({ headless: false });

  // Setup context however you like.
  const context = await browser.newContext({ /* pass any options */ ignoreHTTPSErrors: true });

  // Pause the page, and start recording manually.
  const page = await context.newPage();
  await page.pause();
})();

Then you can visit https://expired.badssl.com/ without any problems and record your actions the same way as you do usually with codegen.

On updated versions of playwright, you can run:

npx playwright codegen --ignore-https-errors https://example.com

Additional options for codegen can be found running npx playwright codegen --help.

Related