BrowserContext

Playwright\Browser\BrowserContextInterface

Methods

clock()

ClockInterface

The context's clock, to fake and advance time.

function clock(): ClockInterface

credentials()

CredentialsInterface

The context's virtual WebAuthn authenticator, to seed and read passkeys.

function credentials(): CredentialsInterface

setGeolocation()

void

Sets the context's geolocation; null coordinates clear it.

function setGeolocation(?float $latitude, ?float $longitude, ?float $accuracy = 0): void

setOffline()

void

Toggles the context's network offline or back online.

function setOffline(bool $offline): void

addCookies()

void
function addCookies(array $cookies): void

addInitScript()

void
function addInitScript(string $script): void

setExtraHTTPHeaders()

void

Set extra HTTP headers for every request made by pages in this context.

function setExtraHTTPHeaders(array $headers): void

clearCookies()

void
function clearCookies(array $options = []): void

deleteCookie()

void

Delete all cookies with the given name across domain and path variants.

function deleteCookie(string $name): void

clearPermissions()

void
function clearPermissions(): void

close()

void
function close(): void

isClosed()

bool

Whether the context is closed, including when its browser was closed instead.

function isClosed(): bool

cookies()

array
function cookies(?array $urls = null): array

exposeBinding()

void
function exposeBinding(string $name, callable $callback): void

exposeFunction()

void
function exposeFunction(string $name, callable $callback): void

grantPermissions()

void
function grantPermissions(array $permissions): void

newPage()

PageInterface
function newPage(array $options = []): PageInterface

pages()

array
function pages(): array

browser()

?BrowserInterface

The browser this context belongs to, or null for a persistent context. Contexts obtained from Browser::context() or Browser::newContext() always carry their browser.

function browser(): ?BrowserInterface

storageState()

array

Get storage state as array (legacy method).

function storageState(?string $path = null): array

getStorageState()

StorageState

Get storage state as StorageState object.

function getStorageState(): StorageState

setStorageState()

void

Load storage state from StorageState object.

function setStorageState(StorageState $storageState): void

saveStorageState()

void

Save storage state to file.

function saveStorageState(string $filePath): void

loadStorageState()

void

Load storage state from file.

function loadStorageState(string $filePath): void

route()

void
function route(string $url, callable $handler): void

unroute()

void
function unroute(string $url, ?callable $handler = null): void

unrouteAll()

void

Removes every route registered on this context in one call. 'behavior' decides what happens to handlers still running: wait for them, wait but swallow their errors, or return without waiting.

function unrouteAll(array $options = []): void

getEnv()

?string
function getEnv(string $name): ?string

tracing()

TracingInterface

Tracing helper associated with this context.

function tracing(): TracingInterface

startTracing()

void
function startTracing(PageInterface $page, array $options = []): void

stopTracing()

void
function stopTracing(PageInterface $page, string $path): void

setNetworkThrottling()

void

Set network throttling configuration.

function setNetworkThrottling(NetworkThrottling $throttling): void

disableNetworkThrottling()

void

Disable network throttling.

function disableNetworkThrottling(): void

waitForEvent()

array
function waitForEvent(string $event, ?callable $predicate = null, ?int $timeout = null): array

waitForPopup()

PageInterface
function waitForPopup(callable $action, array $options = []): PageInterface

request()

APIRequestContextInterface

API testing helper associated with this context. Requests made with this API will use context cookies.

function request(): APIRequestContextInterface

setDefaultTimeout()

void

Sets the default maximum time for methods that accept a timeout option.

function setDefaultTimeout(int $timeout): void

setDefaultNavigationTimeout()

void

Sets the default maximum time for navigation methods.

function setDefaultNavigationTimeout(int $timeout): void