Ignore SSL errors with playwright code generation
Asked Answered
B

3

16

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?

Bummer answered 11/4, 2021 at 17:55 Comment(1)
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.Cloudy
O
17

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,
  },
});
Otherness answered 23/2, 2023 at 15:46 Comment(2)
This works perfectly - is there any way it could be included in the playwright.config file?Kraul
You can include the setting in the global config yes. playwright.dev/docs/test-configuration#global-configuration. I'll update my answer to include that as well.Otherness
H
12

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.

Hennery answered 1/10, 2021 at 17:15 Comment(0)
D
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.

Disintegration answered 11/4, 2021 at 20:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.