playwright-php/playwright
Composer 安装命令:
composer require --dev playwright-php/playwright
包简介
Modern PHP library for Playwright automation: browsing, scraping, screenshots, testing, and more.
关键字:
README 文档
README
Playwright PHP - Modern Browser Automation
About
Playwright for PHP lets you launch real browsers (Chromium, Firefox, WebKit), drive pages and locators, and write reliable end‑to‑end tests — all from PHP.
- Familiar model: browser → context → page → locator
- Auto‑waiting interactions reduce flakiness
- PHPUnit integration with a base trait and fluent
expect()assertions - Cross‑browser: Chromium, Firefox, and WebKit supported
- No separate server to manage — a lightweight Node server is started for you
Requirements:
- PHP 8.2+
- Node.js 20+ (used by the bundled Playwright server and browsers)
Install
Add the library to your project:
composer require 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 # On fresh machines/CI where you need Playwright's OS dependencies too vendor/bin/playwright-install --with-deps # The same commands apply when you work on this repository # (the script lives in vendor/bin/playwright-install here too) # Preview commands without changes vendor/bin/playwright-install --dry-run --with-deps
For advanced install options (including browser cache location), see the Getting Started guide.
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(), orPlaywright::webkit(). Playwright::safari()is an alias ofwebkit().- Common launch options:
headless(bool),slowMo(ms),args(array of CLI args), and an optionalcontextarray with context options.
$context = Playwright::webkit([ 'headless' => false, 'slowMo' => 200, 'args' => ['--no-sandbox'], // 'context' => [ ... context options ... ], ]);
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('alice@example.com'); $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->pageproperties. - Call
setUpPlaywright()insetUp()andtearDownPlaywright()intearDown()for proper lifecycle management. - Use
$this->expect($locator)or$this->expect($page)for fluent assertions. - If you prefer full control, you can skip the trait and use the static
Playwrightfacade directly.
CI usage (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
Contributions are welcome. Please use Conventional Commits, include tests for behavior changes, and ensure docs/examples are updated when relevant. A typical first run inside the repository is:
composer install # installs PHP deps and the bundled Playwright server vendor/bin/playwright-install --with-deps # downloads browsers + optional system deps
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.
playwright-php/playwright 适用场景与选型建议
playwright-php/playwright 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40.95k 次下载、GitHub Stars 达 193, 最近一次更新时间为 2025 年 10 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「testing」 「test」 「browser」 「web」 「webkit」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 playwright-php/playwright 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 playwright-php/playwright 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 playwright-php/playwright 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Testing Suite For Lumen like Laravel does.
The PHP SDK for Checkmango
Specification-oriented BDD helpers for PHPUnit
KissTest is command-line free, fast, simple and beautiful unit test framework.
PWWEB Artomator
Alfabank REST API integration
统计信息
- 总下载量: 40.95k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 194
- 点击次数: 47
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-11