Device-like context

Open a page with mobile viewport, touch, scale factor, locale, and timezone options.

This example creates a browser context with explicit mobile-like options.

Use it when you want a runnable script without installing the playwright-php/devices companion package. For named device presets, use the devices package.

Run it

bash
php content/examples/devices.php

Expected output:

text
Viewport: 390x844
Screenshot saved to: .../var/playwright-artifacts/examples/device-like-context.png

What it demonstrates

  • Device behavior is context configuration.
  • Viewport, scale factor, touch support, locale, and timezone must be set before navigation.
  • Screenshots should be written to a known artifact directory.
  • Named presets belong to the devices companion package.

Go next

Source

devices.php
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_once __DIR__.'/../../vendor/autoload.php';

use Playwright\Playwright;

$artifactDir = __DIR__.'/../../var/playwright-artifacts/examples';
is_dir($artifactDir) || mkdir($artifactDir, 0777, true);

$context = Playwright::chromium([
    'headless' => true,
    'context' => [
        'viewport' => ['width' => 390, 'height' => 844],
        'deviceScaleFactor' => 3,
        'isMobile' => true,
        'hasTouch' => true,
        'locale' => 'fr-FR',
        'timezoneId' => 'Europe/Paris',
    ],
]);

$page = $context->newPage();
$page->goto('https://example.com');

$viewport = $page->viewportSize();
$screenshot = $page->screenshot($artifactDir.'/device-like-context.png');

echo sprintf('Viewport: %dx%d', $viewport['width'], $viewport['height']).PHP_EOL;
echo 'Screenshot saved to: '.$screenshot.PHP_EOL;

$context->close();