kingkoen
kingkoen

Reputation: 171

Ignore SSL errors with playwright code generation

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?

Upvotes: 17

Views: 44249

Answers (3)

Rael Gugelmin Cunha
Rael Gugelmin Cunha

Reputation: 3532

Permanent fix

Edit playwright.config.ts and add:

import { defineConfig } from '@playwright/test';
export default defineConfig({
  use: {
    ignoreHTTPSErrors: true,
  },
});

Runtime only

You can run the codegen with:

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

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

Upvotes: 16

robstarbuck
robstarbuck

Reputation: 8091

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,
  },
});

Upvotes: 20

theDavidBarton
theDavidBarton

Reputation: 8841

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.

Upvotes: 12

Related Questions