### Launch Chromium Browser and Get Context Source: https://github.com/playwright-php/playwright/blob/main/docs/guide/core-concepts.md Launches a Chromium browser instance and returns a default BrowserContext. Ensure Playwright is installed and the browser binaries are available. ```php false]); // ... do stuff ... // Closing the context will also close the associated browser. $context->close(); ``` -------------------------------- ### Local Development Setup for Playwright PHP Source: https://github.com/playwright-php/playwright/blob/main/README.md Commands for setting up the local development environment for Playwright PHP. Installs PHP dependencies and the bundled Playwright server, then downloads browsers and system dependencies. ```bash composer install # installs PHP deps and the bundled Playwright server vendor/bin/playwright-install --with-deps # downloads browsers + optional system deps ``` -------------------------------- ### Install Playwright PHP and Browsers Source: https://context7.com/playwright-php/playwright/llms.txt Install the Playwright PHP library via Composer and download the necessary browser binaries. Use the `--with-deps` flag for CI environments or fresh machines to include system dependencies. ```bash composer require playwright-php/playwright ``` ```bash vendor/bin/playwright-install --browsers ``` ```bash vendor/bin/playwright-install --with-deps ``` ```bash PLAYWRIGHT_BROWSERS_PATH=/path/to/.playwright-browsers vendor/bin/playwright-install --browsers ``` -------------------------------- ### GitHub Actions Workflow for Playwright PHP Tests Source: https://context7.com/playwright-php/playwright/llms.txt This YAML workflow automates the setup and execution of Playwright PHP tests in a GitHub Actions CI/CD pipeline. It includes steps for checking out code, setting up PHP and Node.js, installing dependencies, installing Playwright browsers with system dependencies, and running PHPUnit tests. ```yaml 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 Playwright browsers with system dependencies - run: vendor/bin/playwright-install --with-deps # Run PHPUnit tests - run: vendor/bin/phpunit --colors=always # Optional: Cache Playwright browsers # Cache location: ~/.cache/ms-playwright ``` -------------------------------- ### Install Playwright Browsers Source: https://github.com/playwright-php/playwright/blob/main/README.md Installs the necessary Playwright browsers (Chromium, Firefox, WebKit) for your project. Use --with-deps to also install OS dependencies. ```bash vendor/bin/playwright-install --browsers ``` ```bash vendor/bin/playwright-install --with-deps ``` ```bash vendor/bin/playwright-install --dry-run --with-deps ``` -------------------------------- ### GitHub Actions CI Workflow for Playwright PHP Source: https://github.com/playwright-php/playwright/blob/main/README.md Example workflow snippet for setting up a CI environment on GitHub Actions to run Playwright PHP tests. Installs Node.js, Composer dependencies, and Playwright browsers. ```yaml 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 ``` -------------------------------- ### Start External Browser Server with Playwright Protocol Source: https://github.com/playwright-php/playwright/blob/main/docs/guide/connect-browser.md Launches a browser server instance using Playwright. Copy the printed ws://... value to use as the connection endpoint. ```php launchServer('chromium', ['headless' => true]); echo $server->wsEndpoint().PHP_EOL; while (true) { sleep(1); } ``` -------------------------------- ### Install Playwright Browsers Source: https://github.com/playwright-php/playwright/blob/main/docs/guide/getting-started.md Download the necessary browser binaries for Playwright to function. This command ensures the Playwright server is updated and fetches the latest browser versions. ```bash vendor/bin/playwright-install --browsers ``` ```bash vendor/bin/playwright-install --with-deps ``` -------------------------------- ### Quick Start: Open Page and Get Title Source: https://github.com/playwright-php/playwright/blob/main/README.md Launches a Chromium browser, navigates to a URL, and retrieves the page title. Ensure you have autoload.php included and the Playwright namespace imported. ```php true]); $page = $context->newPage(); $page->goto('https://example.com'); echo $page->title().PHP_EOL; // Example Domain $context->close(); ``` -------------------------------- ### PHPUnit Integration with PlaywrightTestCaseTrait Source: https://github.com/playwright-php/playwright/blob/main/README.md Minimal example demonstrating how to use the PlaywrightTestCaseTrait for E2E tests with PHPUnit. Ensure PHPUnit 10.0+ is installed. Call setUpPlaywright() and tearDownPlaywright() in your test case's setup and teardown methods. ```php 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'); } } ``` -------------------------------- ### Start Chrome with Remote Debugging Enabled Source: https://github.com/playwright-php/playwright/blob/main/docs/guide/connect-browser.md Launches Google Chrome with the remote debugging port enabled. This is required for connecting via CDP. ```bash "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" --remote-debugging-port=9222 --user-data-dir=/tmp/chrome-pw ``` -------------------------------- ### Install Playwright PHP Library Source: https://github.com/playwright-php/playwright/blob/main/docs/guide/getting-started.md Use Composer to add the Playwright PHP library to your project dependencies. ```bash composer require playwright-php/playwright ``` -------------------------------- ### Install Route Server Source: https://github.com/playwright-php/playwright/blob/main/docs/contributing/testing.md Use `RouteServerTestTrait` to fulfill requests directly via Playwright routing, avoiding the need for a PHP built-in web server. The `routeUrl` helper provides stable URLs for routed content. ```php use Playwright\Tests\Support\RouteServerTestTrait; // Inside a test case using PlaywrightTestCaseTrait $this->installRouteServer($this->page, [ '/index.html' => '