playwright

playwright-phpMIT活跃维护

PHP 版的 Playwright 浏览器自动化库,用 PHP 就能驱动浏览器做导航、E2E 测试、断言和截图,省去在 PHP 项目里另搭一套自动化工具链。

Playwright PHP library for browser automation: navigation, E2E tests, assertions, screenshots, and so much more!

v1.6.0发布于 2026-11-07 23:07 UTC256star +1 / 7 天26fork同步于 2026-11-10 06:05 UTC

怎么上手

由 README 提炼,执行前请确认
composer require --dev playwright-php/playwright
vendor/bin/playwright-install --browsers
硬件要求按 README 说明

README 未说明显存或 GPU 要求;运行环境需 PHP 8.2+ 与 Node.js 20+,浏览器由 playwright-install 下载。

下载:源码 zip·GitHub Releases(安装包)本站不镜像安装包

项目说明

由 README 自动翻译提炼

在 PHP 里直接驱动 Chromium、Firefox、WebKit 做 E2E 测试与网页抓取,无需自己管理 Node 服务

PHP 版的 Playwright 浏览器自动化库,用 PHP 就能驱动浏览器做导航、E2E 测试、断言和截图,省去在 PHP 项目里另搭一套自动化工具链。

核心能力

  • 用 browser/context/page/locator 模型操作浏览器
  • 交互自动等待,减少手写 sleep
  • 内置 PHPUnit trait 与 expect() 流式断言
  • 包内自带 Node 服务,无需单独启动服务器
  • 支持 storageState 复用登录态与上下文

容易踩的坑

必须额外执行 playwright-install 下载浏览器,否则无法启动;PHPUnit 集成需 PHPUnit 10.0 及以上;CI 或新机器上需加 --with-deps 安装系统依赖。

内容由模型根据 README 生成,可能不完整或不准确,以官方文档为准。看原文 README

最近发版

5 个版本v1.6.0
  1. v1.6.0最新正式版2026-11-07 · 2026-11-07 23:07 UTC

    Added

    • Report missing Playwright dependencies with installation commands and include redacted Node stderr when the process exits (#130)
    • toHaveScreenshot() on PageAssertionsInterface, LocatorAssertionsInterface, and ExpectInterface compares screenshots against PNG or lossless WebP baselines (#174)
    • Launching a browser from a custom path through withExecutablePath() and PW_EXECUTABLE_PATH (#191)
    • PageInterface::pageErrors() exposes recorded JavaScript errors (#179)
    • Remote browser configuration through PlaywrightConfig::wsEndpoint, withWsEndpoint() and the PW_WS_ENDPOINT / PLAYWRIGHT_WS_ENDPOINT environment variables (#195)

    Changed

    • Playwright static browser launch helpers throw InvalidArgumentException for unsupported options that were previously ignored (#200)

    Fixed

    • Use PlaywrightConfig::logger for page and locator diagnostics, with redacted navigation URLs and command errors (#83)
    • Preserve Playwright's default Chromium feature flags so cross-site iframes can access storage (#199)

    New Contributors

    Full Changelog: https://github.com/playwright-php/playwright/compare/v1.5.1...v1.6.0

  2. v1.5.12026-11-06 · 2026-11-06 21:01 UTC
    查看更新说明

    Fixed

    New Contributors

    Full Changelog: https://github.com/playwright-php/playwright/compare/v1.5.0...v1.5.1

  3. v1.5.02026-09-20 · 2026-09-20 12:55 UTC
    查看更新说明

    Added

    • Targeted browser installation through playwright-install [browser...] (#165)
    • Package manager selection from the consuming project's packageManager declaration or --package-manager-bin (#168)

    Fixed

    • Browser-context API requests preserve response status, headers, binary bodies, cookies, storage state and tracing (#163)
    • API request tracing and HAR recording use the correct request tracer with Playwright 1.63 and newer (#166)
    • Route::fulfill() decodes base64 response bodies (#162)
    • playwright-install keeps pnpm dependencies and lockfiles isolated from enclosing workspaces (#167)
    • Failure screenshots are named after the failing test again on PHPUnit 10 and newer (#169)

    New Contributors

    Full Changelog: https://github.com/playwright-php/playwright/compare/v1.4.0...v1.5.0

  4. v1.4.02026-08-10 · 2026-08-10 19:42 UTC
    查看更新说明

    What's Changed

    New Contributors

    Full Changelog: https://github.com/playwright-php/playwright/compare/v1.3.1...v1.4.0

  5. v1.3.12026-08-04 · 2026-08-04 21:17 UTC
    查看更新说明

    What's Changed

    Full Changelog: https://github.com/playwright-php/playwright/compare/v1.3.0...v1.3.1

用户点评

登录用户的真实使用记录,可按版本和硬件筛选
共 0 条

    还没有人点评。第一个说出真实体验的人最有价值。

    README 原文

    未翻译

      PHP Version   CI   Release   License

    Playwright PHP

    Automate Chromium, Firefox, and WebKit from PHP for browser testing, scraping, screenshots, and other browser-driven workflows.

    • Familiar browser, context, page, and locator model
    • Auto-waiting interactions
    • PHPUnit integration with fluent expect() assertions
    • No separate server to manage: the package starts its bundled Node server

    Requirements:

    • PHP 8.2+
    • Node.js 20+ (used by the bundled Playwright server and browsers)

    Installation

    Add the library to your project:

    composer require --dev playwright-php/playwright
    

    Install the Playwright browsers (Chromium, Firefox, WebKit):

    # Run after composer install in your application or in this repository
    vendor/bin/playwright-install --browsers
    
    # Or install only the browser targets your project uses
    vendor/bin/playwright-install chromium
    
    # On fresh machines/CI where you need Playwright's OS dependencies too
    vendor/bin/playwright-install --with-deps
    
    # Preview commands without changes
    vendor/bin/playwright-install --dry-run --with-deps
    

    For targeted installation, browser cache location, and Chrome or Edge channels, see Browsers, Browser Types, and Channels.

    Quick Start

    Open a page and print its title:

    <?php
    
    require __DIR__.'/vendor/autoload.php';
    
    use Playwright\Playwright;
    
    $context = Playwright::chromium(['headless' => true]);
    $page = $context->newPage();
    $page->goto('https://example.com');
    
    echo $page->title().PHP_EOL; // Example Domain
    
    $context->close();
    

    Documentation

    Usage

    Browser

    • Choose a browser: Playwright::chromium(), Playwright::firefox(), or Playwright::webkit().
    • Playwright::safari() is an alias of webkit().
    • Supported launch options: headless (bool), slowMo (ms), args (array of CLI args), and an optional context array with context options.
    $context = Playwright::webkit([
        'headless' => false,
        'slowMo'   => 200,
        'args'     => ['--no-sandbox'],
        // 'context' => [ ... context options ... ],
    ]);
    

    Unknown launch option names throw InvalidArgumentException. For options such as channel and proxy, use PlaywrightConfigBuilder with PlaywrightFactory.

    Page

    Create pages, navigate, evaluate scripts, and take screenshots:

    $page = $context->newPage();
    $page->goto('https://example.com');
    
    $html  = $page->content();
    $title = $page->title();
    
    $path = $page->screenshot(__DIR__.'/screenshot.png');
    

    Locators and interactions

    $button = $page->locator('text=Sign in');
    $button->click();
    
    $username = $page->locator('#username');
    $username->fill('[email protected]');
    
    $password = $page->locator('#password');
    $password->fill('s3cret');
    $password->press('Enter');
    

    Storage state and context reuse

    $context->storageState(__DIR__.'/state.json');
    
    // Later in another process
    $ctx = Playwright::chromium([
      'context' => ['storageState' => __DIR__.'/state.json'],
    ]);
    

    PHPUnit integration

    The package provides a testing trait and fluent expect() assertions to write robust E2E tests.

    Requirements: PHPUnit 10.0 or higher is required to use the PlaywrightTestCaseTrait.

    Minimal example:

    <?php
    
    use PHPUnit\Framework\TestCase;
    use Playwright\Testing\PlaywrightTestCaseTrait;
    
    final class HomePageTest extends TestCase
    {
        use PlaywrightTestCaseTrait;
    
        protected function setUp(): void
        {
            parent::setUp();
            $this->setUpPlaywright();
        }
    
        protected function tearDown(): void
        {
            $this->tearDownPlaywright();
            parent::tearDown();
        }
    
        public function test_title_is_correct(): void
        {
            $this->page->goto('https://example.com');
    
            $this->expect($this->page)->toHaveTitle('Example Domain');
        }
    }
    

    Notes:

    • The trait provides $this->playwright, $this->browser, $this->context, and $this->page properties.
    • Call setUpPlaywright() in setUp() and tearDownPlaywright() in tearDown() for proper lifecycle management.
    • Use $this->expect($locator) or $this->expect($page) for fluent assertions with auto-waiting.
    • If you prefer full control, you can skip the trait and use the static Playwright facade directly.

    GitHub Actions

    Example workflow snippet:

    jobs:
      tests:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v4
    
          - uses: shivammathur/setup-php@v2
            with:
              php-version: '8.2'
    
          - uses: actions/setup-node@v4
            with:
              node-version: '20'
    
          - run: composer install --no-interaction --prefer-dist
    
          # Install browsers for Playwright PHP
          - run: vendor/bin/playwright-install --with-deps
    
          - run: vendor/bin/phpunit --colors=always
    

    Tips:

    • Cache Node and Composer if you need faster builds.
    • You can also cache Playwright browsers under ~/.cache/ms-playwright.

    Contributing

    Include tests for behavior changes and update documentation or examples when their public behavior changes. Set up the repository with:

    composer install                                # installs PHP deps and the bundled Playwright server
    bin/playwright-install --with-deps              # downloads browsers + optional system deps
    make ci                                         # runs code style, static analysis, and tests
    

    See docs/contributing/testing.md for more details on the local workflow.

    License

    This package is released by the Playwright PHP project under the MIT License. See the LICENSE file for details.