Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/src/api/class-browsercontext.md
Original file line number Diff line number Diff line change
Expand Up @@ -1520,9 +1520,12 @@ its geolocation.

### param: BrowserContext.setHTTPCredentials.httpCredentials
* since: v1.8
- `httpCredentials` <[null]|[Object]>
- `httpCredentials` <[null]|[Object]|[Array]<[Object]>>
- `username` <[string]>
- `password` <[string]>
- `origin` ?<[string]> Restrain sending http credentials on specific origin (scheme://host:port).

Pass an array to use different credentials for different origins. The first entry that matches the request origin is used, and entries with no origin match any request.

## async method: BrowserContext.setOffline
* since: v1.8
Expand Down
4 changes: 3 additions & 1 deletion docs/src/api/params.md
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ An object containing additional HTTP headers to be sent with every request. Defa
Whether to emulate network being offline. Defaults to `false`. Learn more about [network emulation](../emulation.md#offline).

## context-option-httpcredentials
- `httpCredentials` <[Object]>
- `httpCredentials` <[Object]|[Array]<[Object]>>
* alias: HttpCredentials
- `username` <[string]>
- `password` <[string]>
Expand All @@ -728,6 +728,8 @@ Whether to emulate network being offline. Defaults to `false`. Learn more about
Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication).
If no origin is specified, the username and password are sent to any servers upon unauthorized responses.

Pass an array to use different credentials for different origins. The first entry that matches the request origin is used, and entries with no origin match any request.

## context-option-colorscheme
* langs: js, java
- `colorScheme` <null|[ColorScheme]<"light"|"dark"|"no-preference">>
Expand Down
139 changes: 131 additions & 8 deletions packages/playwright-client/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10453,13 +10453,28 @@ export interface BrowserContext {

/**
* @deprecated Browsers may cache credentials after successful authentication. Create a new browser context instead.
* @param httpCredentials
* @param httpCredentials Pass an array to use different credentials for different origins. The first entry that matches the request origin
* is used, and entries with no origin match any request.
*/
setHTTPCredentials(httpCredentials: null|{
username: string;

password: string;
}): Promise<void>;

/**
* Restrain sending http credentials on specific origin (scheme://host:port).
*/
origin?: string;
}|ReadonlyArray<{
username: string;

password: string;

/**
* Restrain sending http credentials on specific origin (scheme://host:port).
*/
origin?: string;
}>): Promise<void>;

/**
* @param offline Whether to emulate network being offline for the browser context.
Expand Down Expand Up @@ -11229,6 +11244,9 @@ export interface Browser {
/**
* Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication). If no
* origin is specified, the username and password are sent to any servers upon unauthorized responses.
*
* Pass an array to use different credentials for different origins. The first entry that matches the request origin
* is used, and entries with no origin match any request.
*/
httpCredentials?: {
username: string;
Expand All @@ -11248,7 +11266,25 @@ export interface Browser {
* `WWW-Authenticate` header is received. Defaults to `'unauthorized'`.
*/
send?: "unauthorized"|"always";
};
}|Array<{
username: string;

password: string;

/**
* Restrain sending http credentials on specific origin (scheme://host:port).
*/
origin?: string;

/**
* This option only applies to the requests sent from corresponding
* [APIRequestContext](https://playwright.dev/docs/api/class-apirequestcontext) and does not affect requests sent from
* the browser. `'always'` - `Authorization` header with basic authentication credentials will be sent with the each
* API request. `'unauthorized` - the credentials are only sent when 401 (Unauthorized) response with
* `WWW-Authenticate` header is received. Defaults to `'unauthorized'`.
*/
send?: "unauthorized"|"always";
}>;

/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to `false`.
Expand Down Expand Up @@ -17270,6 +17306,9 @@ export interface BrowserType<Unused = {}> {
/**
* Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication). If no
* origin is specified, the username and password are sent to any servers upon unauthorized responses.
*
* Pass an array to use different credentials for different origins. The first entry that matches the request origin
* is used, and entries with no origin match any request.
*/
httpCredentials?: {
username: string;
Expand All @@ -17289,7 +17328,25 @@ export interface BrowserType<Unused = {}> {
* `WWW-Authenticate` header is received. Defaults to `'unauthorized'`.
*/
send?: "unauthorized"|"always";
};
}|Array<{
username: string;

password: string;

/**
* Restrain sending http credentials on specific origin (scheme://host:port).
*/
origin?: string;

/**
* This option only applies to the requests sent from corresponding
* [APIRequestContext](https://playwright.dev/docs/api/class-apirequestcontext) and does not affect requests sent from
* the browser. `'always'` - `Authorization` header with basic authentication credentials will be sent with the each
* API request. `'unauthorized` - the credentials are only sent when 401 (Unauthorized) response with
* `WWW-Authenticate` header is received. Defaults to `'unauthorized'`.
*/
send?: "unauthorized"|"always";
}>;

/**
* If `true`, Playwright does not pass its own configurations args and only uses the ones from
Expand Down Expand Up @@ -18885,6 +18942,9 @@ export interface APIRequest {
/**
* Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication). If no
* origin is specified, the username and password are sent to any servers upon unauthorized responses.
*
* Pass an array to use different credentials for different origins. The first entry that matches the request origin
* is used, and entries with no origin match any request.
*/
httpCredentials?: {
username: string;
Expand All @@ -18904,7 +18964,25 @@ export interface APIRequest {
* `WWW-Authenticate` header is received. Defaults to `'unauthorized'`.
*/
send?: "unauthorized"|"always";
};
}|Array<{
username: string;

password: string;

/**
* Restrain sending http credentials on specific origin (scheme://host:port).
*/
origin?: string;

/**
* This option only applies to the requests sent from corresponding
* [APIRequestContext](https://playwright.dev/docs/api/class-apirequestcontext) and does not affect requests sent from
* the browser. `'always'` - `Authorization` header with basic authentication credentials will be sent with the each
* API request. `'unauthorized` - the credentials are only sent when 401 (Unauthorized) response with
* `WWW-Authenticate` header is received. Defaults to `'unauthorized'`.
*/
send?: "unauthorized"|"always";
}>;

/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to `false`.
Expand Down Expand Up @@ -23588,6 +23666,9 @@ export interface Electron {
/**
* Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication). If no
* origin is specified, the username and password are sent to any servers upon unauthorized responses.
*
* Pass an array to use different credentials for different origins. The first entry that matches the request origin
* is used, and entries with no origin match any request.
*/
httpCredentials?: {
username: string;
Expand All @@ -23607,7 +23688,25 @@ export interface Electron {
* `WWW-Authenticate` header is received. Defaults to `'unauthorized'`.
*/
send?: "unauthorized"|"always";
};
}|Array<{
username: string;

password: string;

/**
* Restrain sending http credentials on specific origin (scheme://host:port).
*/
origin?: string;

/**
* This option only applies to the requests sent from corresponding
* [APIRequestContext](https://playwright.dev/docs/api/class-apirequestcontext) and does not affect requests sent from
* the browser. `'always'` - `Authorization` header with basic authentication credentials will be sent with the each
* API request. `'unauthorized` - the credentials are only sent when 401 (Unauthorized) response with
* `WWW-Authenticate` header is received. Defaults to `'unauthorized'`.
*/
send?: "unauthorized"|"always";
}>;

/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to `false`.
Expand Down Expand Up @@ -24225,6 +24324,9 @@ export interface AndroidDevice {
/**
* Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication). If no
* origin is specified, the username and password are sent to any servers upon unauthorized responses.
*
* Pass an array to use different credentials for different origins. The first entry that matches the request origin
* is used, and entries with no origin match any request.
*/
httpCredentials?: {
username: string;
Expand All @@ -24244,7 +24346,25 @@ export interface AndroidDevice {
* `WWW-Authenticate` header is received. Defaults to `'unauthorized'`.
*/
send?: "unauthorized"|"always";
};
}|Array<{
username: string;

password: string;

/**
* Restrain sending http credentials on specific origin (scheme://host:port).
*/
origin?: string;

/**
* This option only applies to the requests sent from corresponding
* [APIRequestContext](https://playwright.dev/docs/api/class-apirequestcontext) and does not affect requests sent from
* the browser. `'always'` - `Authorization` header with basic authentication credentials will be sent with the each
* API request. `'unauthorized` - the credentials are only sent when 401 (Unauthorized) response with
* `WWW-Authenticate` header is received. Defaults to `'unauthorized'`.
*/
send?: "unauthorized"|"always";
}>;

/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to `false`.
Expand Down Expand Up @@ -25457,8 +25577,11 @@ export interface BrowserContextOptions {
/**
* Credentials for [HTTP authentication](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication). If no
* origin is specified, the username and password are sent to any servers upon unauthorized responses.
*
* Pass an array to use different credentials for different origins. The first entry that matches the request origin
* is used, and entries with no origin match any request.
*/
httpCredentials?: HTTPCredentials;
httpCredentials?: HTTPCredentials|Array<HTTPCredentials>;

/**
* Whether to ignore HTTPS errors when sending network requests. Defaults to `false`.
Expand Down
13 changes: 11 additions & 2 deletions packages/playwright-core/src/client/browserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import { mkdirIfNeeded } from './fileUtils';

import type { EvaluateOptions } from './jsHandle';
import type { BrowserContextOptions, Headers, SetStorageState, StorageState, WaitForEventOptions } from './types';
import type { HttpCredentials } from '@protocol/structs';
import type * as structs from '../../types/structs';
import type * as api from '../../types/types';
import type { URLMatch } from '@isomorphic/urlMatch';
Expand Down Expand Up @@ -353,8 +354,8 @@ export class BrowserContext extends ChannelOwner<channels.BrowserContextChannel>
await this._channel.setOffline({ offline }, kNoTimeout);
}

async setHTTPCredentials(httpCredentials: { username: string, password: string } | null): Promise<void> {
await this._channel.setHTTPCredentials({ httpCredentials: httpCredentials || undefined }, kNoTimeout);
async setHTTPCredentials(httpCredentials: HttpCredentials | HttpCredentials[] | null): Promise<void> {
await this._channel.setHTTPCredentials({ httpCredentials: toHttpCredentialsProtocol(httpCredentials || undefined) }, kNoTimeout);
}

async addInitScript(script: Function | string | { path?: string, content?: string }, arg?: any, options?: EvaluateOptions) {
Expand Down Expand Up @@ -567,12 +568,20 @@ export async function prepareBrowserContextParams(options: BrowserContextOptions
contrast: options.contrast === null ? 'no-override' : options.contrast,
acceptDownloads: toAcceptDownloadsProtocol(options.acceptDownloads),
clientCertificates: await toClientCertificatesProtocol(options.clientCertificates),
httpCredentials: toHttpCredentialsProtocol(options.httpCredentials),
};
if (contextParams.recordVideo && contextParams.recordVideo.dir)
contextParams.recordVideo.dir = path.resolve(contextParams.recordVideo.dir);
return contextParams;
}

export function toHttpCredentialsProtocol(credentials?: HttpCredentials | HttpCredentials[]): HttpCredentials[] | undefined {
if (!credentials)
return undefined;
const list = Array.isArray(credentials) ? credentials : [credentials];
return list.length ? list : undefined;
}

function toAcceptDownloadsProtocol(acceptDownloads?: boolean) {
if (acceptDownloads === undefined)
return undefined;
Expand Down
Loading
Loading