Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
feat(codegen): add --http-credentials option (#42322)
Yury Semikhatsky66bfad76299e
diff --git a/docs/src/codegen.md b/docs/src/codegen.mdindex 1b1d06d445..731ffe6e26 100644--- a/docs/src/codegen.md+++ b/docs/src/codegen.md@@ -438,6 +438,26 @@ playwright codegen --user-data-dir=/path/to/your/browser/data/ github.com/microspwsh bin/Debug/netX/playwright.ps1 codegen --user-data-dir=/path/to/your/browser/data/ github.com/microsoft/playwright```+#### Authenticate with HTTP credentials++Run `codegen` with `--http-credentials` to authenticate with [HTTP Basic Authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Authentication). Unlike credentials embedded in the URL, they are sent to any origin that requests them during the recording session and are included in the generated code.++```bash js+npx playwright codegen --http-credentials="username:password" example.com+```++```bash java+mvn exec:java -e -D exec.mainClass=com.microsoft.playwright.CLI -D exec.args='codegen --http-credentials="username:password" example.com'+```++```bash python+playwright codegen --http-credentials="username:password" example.com+```++```bash csharp+pwsh bin/Debug/netX/playwright.ps1 codegen --http-credentials="username:password" example.com+```+## Record using custom setupIf you would like to use codegen in some non-standard setup (for example, use [`method: BrowserContext.route`]), it is possible to call [`method: Page.pause`] that will open a separate window with codegen controls.diff --git a/packages/playwright-core/src/cli/browserActions.ts b/packages/playwright-core/src/cli/browserActions.tsindex 56d32b5654..741cc6293c 100644--- a/packages/playwright-core/src/cli/browserActions.ts+++ b/packages/playwright-core/src/cli/browserActions.ts@@ -38,6 +38,7 @@ export type Options = {colorScheme?: string;device?: string;geolocation?: string;+ httpCredentials?: string;ignoreHttpsErrors?: boolean;lang?: string;loadStorage?: string;@@ -125,6 +126,18 @@ async function launchContext(options: Options, extraOptions: LaunchOptions): ProcontextOptions.permissions = ['geolocation'];}+ // HTTP credentials++ if (options.httpCredentials) {+ const separator = options.httpCredentials.indexOf(':');+ if (separator === -1)+ throw new Error('Invalid http credentials format: use "username:password", for example --http-credentials="admin:secret"');+ contextOptions.httpCredentials = {+ username: options.httpCredentials.substring(0, separator),+ password: options.httpCredentials.substring(separator + 1),+ };+ }+// User agentif (options.userAgent)diff --git a/packages/playwright-core/src/cli/program.ts b/packages/playwright-core/src/cli/program.tsindex 93a2323ba1..efb1040a77 100644--- a/packages/playwright-core/src/cli/program.ts+++ b/packages/playwright-core/src/cli/program.ts@@ -276,6 +276,7 @@ function commandWithOpenOptions(command: string, description: string, options: a.option('--color-scheme <scheme>', 'emulate preferred color scheme, "light" or "dark"').option('--device <deviceName>', 'emulate device, for example "iPhone 11"').option('--geolocation <coordinates>', 'specify geolocation coordinates, for example "37.819722,-122.478611"')+ .option('--http-credentials <credentials>', 'specify HTTP authentication credentials as "username:password", for example --http-credentials="admin:secret"').option('--ignore-https-errors', 'ignore https errors').option('--load-storage <filename>', 'load context storage state from the file, previously saved with --save-storage').option('--lang <language>', 'specify language / locale, for example "en-GB"')diff --git a/tests/library/inspector/cli-codegen-javascript.spec.ts b/tests/library/inspector/cli-codegen-javascript.spec.tsindex 31eddfa5f5..29d7114a61 100644--- a/tests/library/inspector/cli-codegen-javascript.spec.ts+++ b/tests/library/inspector/cli-codegen-javascript.spec.ts@@ -48,6 +48,26 @@ test('should print the correct context options for custom settings', async ({ br});+test('should work with --http-credentials', async ({ browserName, channel, runCLI, server }) => {+ server.setAuth('/empty.html', 'user', 'pass');+ const cli = runCLI(['--http-credentials=user:pass', '--target=javascript', server.EMPTY_PAGE]);+ const expectedResult = `const { ${browserName} } = require('playwright');++(async () => {+ const browser = await ${browserName}.launch({+ ${launchOptions(channel)}+ });+ const context = await browser.newContext({+ httpCredentials: {+ password: 'pass',+ username: 'user'+ }+ });+ const page = await context.newPage();+ await page.goto('${server.EMPTY_PAGE}');`;+ await cli.waitFor(expectedResult);+});+test('should print the correct context options when using a device', async ({ browserName, channel, runCLI, server }) => {test.skip(browserName !== 'chromium');