Home

Awesome

ReactPHP WebDriver

This is a direct port of RemoteWebDriver logic from the php-webdriver/webdriver package, which utilizes ReactPHP event loop and promise API for browser interaction w/o execution flow blocking.

Selenium WebDriver is a software that is used to manipulate browsers from the code (primarily, for testing and web scraping). You can find more here: https://selenium.dev.

This PHP client sends async HTTP requests to the Grid. It is a central endpoint for commands, a bridge between your code and browser instances. See SeleniumHQ/docker-selenium to get your own remote browser (or a cluster).

Enjoy!

Requirements

Installation

With composer:

$ composer require itnelo/reactphp-webdriver:^0.4

How to use

Call a factory method to get your instance (recommended). The minimal configuration is:

use React\EventLoop\Factory as LoopFactory;
use Itnelo\React\WebDriver\WebDriverFactory;

$loop = LoopFactory::create();

$webDriver = WebDriverFactory::create(
    $loop,
    [
        'hub' => [
            'host' => 'selenium-hub',
            'port' => 4444,
        ],
    ]
);

You can customize a set of parameters for the underlying ReactPHP Browser and tune driver options:

use React\EventLoop\Factory as LoopFactory;
use Itnelo\React\WebDriver\WebDriverFactory;

$loop = LoopFactory::create();

$webDriver = WebDriverFactory::create(
    $loop,
    [
        'browser' => [
            'tcp' => [
                'bindto' => '192.169.56.10:0',
            ],
            'tls' => [
                'verify_peer' => false,
                'verify_peer_name' => false,
            ],
        ],
        'hub' => [
            'host' => 'selenium-hub',
            'port' => 4444,
        ],
        'command' => [
            'timeout' => 30,
        ],
    ]
);

Manual configuration (if you prefer to configure each component as a separate service, e.g. compiling a DI container and want to reuse existing service definitions):

use React\EventLoop\Factory as LoopFactory;
use React\Socket\Connector as SocketConnector;
use React\Http\Browser;
use Itnelo\React\WebDriver\Client\W3CClient;
use Itnelo\React\WebDriver\Timeout\Interceptor as TimeoutInterceptor;
use Itnelo\React\WebDriver\SeleniumHubDriver;

$loop = LoopFactory::create();

$socketConnector = new SocketConnector(
    $loop,
    [
        'tcp' => [
            'bindto' => '192.169.56.10:0',
        ],
        'tls' => [
            'verify_peer' => false,
            'verify_peer_name' => false,
        ],
    ]
);
$browser = new Browser($loop, $socketConnector);
$browser = $browser->withRejectErrorResponse(false);

$hubClient = new W3CClient(
    $browser,
    [
        'server' => [
            'host' => 'selenium-hub',
            'port' => 4444,
        ],
    ]
);

$timeoutInterceptor = new TimeoutInterceptor($loop, 30);

$webDriver = new SeleniumHubDriver(
    $loop,
    $hubClient,
    $timeoutInterceptor
);

See a self-documented WebDriverInterface.php (and ClientInterface.php) for the API details. Not all methods and arguments are ported (only the most necessary), so feel free to open an issue / make a pull request if you want more.

See also

Changelog

All notable changes to this project will be documented in CHANGELOG.md.