itnelo/reactphp-webdriver

An async, W3C compliant PHP client for Selenium Grid server (hub)

0.4.0 2021-02-24 21:46 UTC

This package is auto-updated.

Last update: 2024-03-25 04:55:44 UTC


README

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

  • PHP 7.4 or higher.
  • ReactPHP v1 (http ^1, stream ^1).
  • Symfony conflicts: 5.1 (or newer) environments are preferred; the package uses (and will use) some components from there, and their code / version constraints may need a review, to include a wider range of supported environments (otherwise, you need to adjust your platform).

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

  • php-webdriver/webdriver — the original, "blocking" implementation; to get information how to use some advanced methods. For example, WebDriverKeys helper describes Unicode strings for sending special inputs to page elements (e.g. Ctrl, Alt and other keys).

Changelog

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