Customise a context
A context with a custom viewport, scale factor, and locale.
This example launches Chromium, creates a custom browser context, then captures screenshots with a specific viewport, scale factor, and locale.
Use it when you need the page to load under known environment settings instead of changing the viewport after the page has already rendered.
Run it
php content/examples/custom-browser.php
Expected artifacts:
var/playwright-artifacts/examples/browser-config-test.png
var/playwright-artifacts/examples/apple.com-fr.png
The script uses live public websites, so rendered content can change over time.
What it demonstrates
- A browser can create a context with custom options.
- Viewport, scale factor, and locale belong to the context.
- Pages created from that context inherit those settings.
- Screenshots record the rendered result.
Go next
- Learn contexts: Contexts
- Learn devices: Devices
- Change viewport: Change the viewport
- API reference: BrowserContext
Source
custom-browser.php<?php
declare(strict_types=1);
/*
* This file is part of the community-maintained Playwright PHP project.
* It is not affiliated with or endorsed by Microsoft.
*
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
require __DIR__.'/../../vendor/autoload.php';
use Playwright\PlaywrightFactory;
$artifactDir = __DIR__.'/../../var/playwright-artifacts/examples';
is_dir($artifactDir) || mkdir($artifactDir, 0777, true);
$playwright = PlaywrightFactory::create();
// 1. Launch the browser as usual
$browser = $playwright->chromium()->launch();
// 2. Create a new context with specific viewport and user agent options
$context = $browser->newContext([
'viewport' => [
'width' => 1920,
'height' => 1080,
],
'deviceScaleFactor' => 2,
'locale' => 'en-US',
]);
// 3. Create a new page *from that context*
$page = $context->newPage();
// 4. All actions on this page will now use the context's settings
$page->goto('https://www.whatismybrowser.com/');
$page->screenshot($artifactDir.'/browser-config-test.png');
$page->goto('https://www.apple.com/fr');
$page->screenshot($artifactDir.'/apple.com-fr.png');
$browser->close();
$playwright->close();