13

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?

kingkoen
  • 131
  • 1
  • 1
  • 3
  • Similar file is called `playwright.config.ts` for Playwright. But you might want to consider specifying that config in the "code" instead of your global config. – MarkHu Jul 31 '23 at 22:54

3 Answers3

11

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.

theDavidBarton
  • 7,643
  • 4
  • 24
  • 51
8

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.

Rael Gugelmin Cunha
  • 3,327
  • 30
  • 25
8

Another option is to configure the test to ignore the HTTPS errors.

import { test } from "@playwright/test";

test.use({
  ignoreHTTPSErrors: true,
});

test("test", async ({ page }) => {
  await page.goto(
    "https://example.com/"
  );
});

NB - test.use... is what's included when running npx playwright codegen --ignore-https-errors.


UPDATE The setting can also be included in your playwright.config.ts file (see docs).

import { defineConfig } from '@playwright/test';
export default defineConfig({
  use: {
    ignoreHTTPSErrors: true,
  },
});
robstarbuck
  • 6,893
  • 2
  • 41
  • 40
  • This works perfectly - is there any way it could be included in the playwright.config file? – Dorian Fabre Mar 22 '23 at 12:38
  • You can include the setting in the global config yes. https://playwright.dev/docs/test-configuration#global-configuration. I'll update my answer to include that as well. – robstarbuck Mar 22 '23 at 16:15