Skip to content

Commit 4eba6c6

Browse files
authored
Add documentation for connecting to external browsers (#70)
Close #64 cc @gimler Signed-off-by: Simon André <smn.andre@gmail.com>
1 parent 741f5c5 commit 4eba6c6

File tree

6 files changed

+329
-4
lines changed

6 files changed

+329
-4
lines changed

README.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
## About
1414

15-
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.
15+
Playwright for PHP lets you launch real browsers (Chromium, Firefox, WebKit), drive pages and locators, and write
16+
reliable end‑to‑end tests — all from PHP.
1617

1718
- **Familiar model**: browser → context → page → locator
1819
- **Auto‑waiting** interactions reduce flakiness
@@ -21,6 +22,7 @@ Playwright for PHP lets you launch real browsers (Chromium, Firefox, WebKit), dr
2122
- No separate server to manage — a lightweight Node server is started for you
2223

2324
Requirements:
25+
2426
- PHP 8.2+
2527
- Node.js 20+ (used by the bundled Playwright server and browsers)
2628

@@ -72,13 +74,20 @@ echo $page->title().PHP_EOL; // Example Domain
7274
$context->close();
7375
```
7476

77+
## Documentation
78+
79+
- [Quick Start Guide](docs/guide.md)
80+
- [Playwright Inspector](docs/inspector.md)
81+
- [Contributing: Testing](docs/contributing/testing.md)
82+
7583
## Usage
7684

7785
### Browser
7886

7987
- Choose a browser: `Playwright::chromium()`, `Playwright::firefox()`, or `Playwright::webkit()`.
8088
- `Playwright::safari()` is an alias of `webkit()`.
81-
- Common launch options: `headless` (bool), `slowMo` (ms), `args` (array of CLI args), and an optional `context` array with context options.
89+
- Common launch options: `headless` (bool), `slowMo` (ms), `args` (array of CLI args), and an optional `context` array
90+
with context options.
8291

8392
```php
8493
$context = Playwright::webkit([
@@ -168,6 +177,7 @@ final class HomePageTest extends TestCase
168177
```
169178

170179
Notes:
180+
171181
- The trait provides `$this->playwright`, `$this->browser`, `$this->context`, and `$this->page` properties.
172182
- Call `setUpPlaywright()` in `setUp()` and `tearDownPlaywright()` in `tearDown()` for proper lifecycle management.
173183
- Use `$this->expect($locator)` or `$this->expect($page)` for fluent assertions.
@@ -201,12 +211,14 @@ jobs:
201211
```
202212
203213
Tips:
214+
204215
- Cache Node and Composer if you need faster builds.
205216
- You can also cache Playwright browsers under `~/.cache/ms-playwright`.
206217

207218
## Contributing
208219

209-
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:
220+
Contributions are welcome. Please use Conventional Commits, include tests for behavior changes, and ensure docs/examples
221+
are updated when relevant. A typical first run inside the repository is:
210222

211223
```bash
212224
composer install # installs PHP deps and the bundled Playwright server
@@ -217,5 +229,5 @@ See `docs/contributing/testing.md` for more details on the local workflow.
217229

218230
## License
219231

220-
This package is released by the [Playwright PHP](https://playwright-php.dev)
232+
This package is released by the [Playwright PHP](https://playwright-php.dev)
221233
project under the MIT License. See the [LICENSE](LICENSE) file for details.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the community-maintained Playwright PHP project.
7+
* It is not affiliated with or endorsed by Microsoft.
8+
*
9+
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
require __DIR__.'/../../vendor/autoload.php';
16+
17+
use Playwright\Exception\PlaywrightException;
18+
use Playwright\PlaywrightFactory;
19+
20+
$playwright = PlaywrightFactory::create();
21+
22+
// Example 1: launchServer() exposes a Playwright WebSocket endpoint that can be reused
23+
// by other processes. This endpoint is ephemeral and valid while the server is running.
24+
echo "=== Example 1: Launch Browser Server ===\n";
25+
$server = $playwright->launchServer('chromium', ['headless' => true]);
26+
$wsEndpoint = $server->wsEndpoint();
27+
echo "Browser server started at: {$wsEndpoint}\n";
28+
29+
// Example 2: connect() uses Playwright protocol, so it must target a launchServer() endpoint.
30+
echo "\n=== Example 2: Connect to Browser Server ===\n";
31+
$browser = $playwright->chromium()->connect($wsEndpoint);
32+
$context = $browser->newContext();
33+
$page = $context->newPage();
34+
$page->goto('https://example.com');
35+
echo "Page title: {$page->title()}\n";
36+
37+
$browser->close();
38+
// Closing the client connection does not stop the server process.
39+
$server->close();
40+
41+
// Example 3: connectOverCDP() is Chromium-only and attaches to a running Chrome instance.
42+
// First, start Chrome with remote debugging enabled.
43+
echo "\n=== Example 3: Connect Over CDP (Chromium only) ===\n";
44+
echo "To use this example:\n";
45+
echo "1. Start Chrome with remote debugging on port 9222\n";
46+
echo " macOS example:\n";
47+
echo " \"/Applications/Google Chrome.app/Contents/MacOS/Google Chrome\" --remote-debugging-port=9222 --user-data-dir=/tmp/chrome-pw\n";
48+
echo "2. Optionally set CDP_ENDPOINT (default: http://127.0.0.1:9222)\n";
49+
50+
$cdpEndpoint = getenv('CDP_ENDPOINT') ?: 'http://127.0.0.1:9222';
51+
52+
try {
53+
$cdpBrowser = $playwright->chromium()->connectOverCDP($cdpEndpoint);
54+
$cdpContext = $cdpBrowser->newContext();
55+
$cdpPage = $cdpContext->newPage();
56+
$cdpPage->goto('https://playwright.dev');
57+
echo "Connected via CDP! Title: {$cdpPage->title()}\n";
58+
$cdpBrowser->close();
59+
} catch (PlaywrightException $e) {
60+
echo "CDP connection failed at {$cdpEndpoint}: {$e->getMessage()}\n";
61+
}
62+
63+
$playwright->close();
64+
65+
echo "\nDone!\n";
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the community-maintained Playwright PHP project.
7+
* It is not affiliated with or endorsed by Microsoft.
8+
*
9+
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
require __DIR__.'/../../vendor/autoload.php';
16+
17+
use Playwright\PlaywrightFactory;
18+
19+
// Connect to an external Playwright browser-server instance started elsewhere.
20+
// This expects a Playwright WebSocket endpoint, not a Chrome DevTools (CDP) URL.
21+
// launchServer() starts a browser process and exposes wsEndpoint() for connect().
22+
23+
$playwright = PlaywrightFactory::create();
24+
25+
// Provide the wsEndpoint() emitted by a process that called launchServer().
26+
$remoteWsEndpoint = getenv('REMOTE_WS_ENDPOINT');
27+
28+
if (false === $remoteWsEndpoint || '' === $remoteWsEndpoint) {
29+
echo "REMOTE_WS_ENDPOINT is not set.\n";
30+
echo "This example needs the exact wsEndpoint() value from launchServer().\n";
31+
echo "\nQuick setup:\n";
32+
echo "1. In another PHP process, run:\n";
33+
echo " \$server = \$playwright->launchServer('chromium', ['headless' => true]);\n";
34+
echo " echo \$server->wsEndpoint();\n";
35+
echo " while (true) { sleep(1); }\n";
36+
echo "2. Run this example with:\n";
37+
echo " REMOTE_WS_ENDPOINT='ws://127.0.0.1:PORT/ID' php docs/examples/connect_remote_browser.php\n";
38+
$playwright->close();
39+
40+
exit(1);
41+
}
42+
43+
echo "Attempting to connect to remote browser at: {$remoteWsEndpoint}\n";
44+
45+
try {
46+
$browser = $playwright->chromium()->connect($remoteWsEndpoint);
47+
echo "Successfully connected to remote browser!\n";
48+
49+
$context = $browser->newContext();
50+
$page = $context->newPage();
51+
52+
$page->goto('https://example.com');
53+
echo "Page title: {$page->title()}\n";
54+
echo "URL: {$page->url()}\n";
55+
56+
// Keep remote servers alive for other clients; only close this client connection.
57+
$browser->close();
58+
echo "Browser connection closed.\n";
59+
} catch (Exception $e) {
60+
echo "Failed to connect: {$e->getMessage()}\n";
61+
echo "Tip: REMOTE_WS_ENDPOINT must be the full wsEndpoint() value, including path.\n";
62+
echo "\nTo set up an external Playwright browser-server instance:\n";
63+
echo "1. Start any PHP process with:\n";
64+
echo " \$server = \$playwright->launchServer('chromium', ['headless' => true]);\n";
65+
echo " echo \$server->wsEndpoint();\n";
66+
echo " while (true) { sleep(1); }\n";
67+
echo "2. Copy its wsEndpoint() value (ws://...)\n";
68+
echo "3. Set REMOTE_WS_ENDPOINT to that value and run this script again\n";
69+
}
70+
71+
$playwright->close();

docs/examples/launch_server.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the community-maintained Playwright PHP project.
7+
* It is not affiliated with or endorsed by Microsoft.
8+
*
9+
* (c) 2025-Present - Playwright PHP - https://github.com/playwright-php
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
require __DIR__.'/../../vendor/autoload.php';
16+
17+
use Playwright\PlaywrightFactory;
18+
19+
// Start a reusable Playwright browser server for external clients.
20+
// This is useful when you want one long-lived browser shared by many scripts.
21+
22+
$playwright = PlaywrightFactory::create();
23+
24+
echo "Launching browser server...\n";
25+
26+
$server = $playwright->launchServer('chromium', [
27+
'headless' => true,
28+
]);
29+
30+
$wsEndpoint = $server->wsEndpoint();
31+
32+
echo "Browser server running!\n";
33+
echo "WebSocket endpoint: {$wsEndpoint}\n";
34+
echo "\nOther processes can connect using:\n";
35+
echo "\$browser = \$playwright->chromium()->connect('{$wsEndpoint}');\n";
36+
echo "\nPress Ctrl+C to stop the server...\n";
37+
38+
// Keep serving until interrupted so clients can keep connecting to this endpoint.
39+
while (true) {
40+
sleep(1);
41+
}

docs/guide/advanced-recipes.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@
33
This page contains recipes for common but more advanced automation scenarios. Use these examples to leverage the full
44
power of Playwright for PHP.
55

6+
## Table of Contents
7+
8+
* [File Uploads](#file-uploads)
9+
* [Network Interception](#network-interception)
10+
* [Handling Dialogs](#handling-dialogs)
11+
* [Working with Frames](#working-with-frames)
12+
* [Connecting to External Browsers](#connecting-to-external-browsers)
13+
* [Browser Configuration with `PlaywrightConfigBuilder`](#browser-configuration-with-playwrightconfigbuilder)
14+
615
## File Uploads
716

817
You can upload one or more files to an `<input type="file">` element using the `setInputFiles()` method on a `Locator`.
@@ -88,6 +97,10 @@ $frameButton = $frame->locator('button:has-text("Submit")');
8897
$frameButton->click();
8998
```
9099

100+
## Connecting to External Browsers
101+
102+
This topic has its own page: **[Connecting to External Browsers](connect-browser.md)**.
103+
91104
## Browser Configuration with `PlaywrightConfigBuilder`
92105

93106
For advanced browser setups, such as using a proxy server or setting custom launch arguments, you can use the

0 commit comments

Comments
 (0)