symfony / panther

A browser testing and web crawling library for PHP and Symfony
MIT License
2.91k stars 213 forks source link

Example using Panther with remote Chrome instance in Docker (with Behat) #590

Open dkarlovi opened 1 year ago

dkarlovi commented 1 year ago

Compose file:

# docker-compose.yaml
services:
    web: # my app with PHP, but without Chrome
        image: app:latest
    chrome:
        image: selenium/standalone-chrome
        shm_size: 2gb

Behat config, using https://github.com/robertfausk/behat-panther-extension:

# behat.yaml.dist
default:
    extensions:
        Robertfausk\Behat\PantherExtension: ~
        FriendsOfBehat\SymfonyExtension:
            bootstrap: tests/bootstrap.php
        Behat\MinkExtension:
            base_url: http://web/
            javascript_session: panther
            sessions:
                symfony:
                    symfony: ~
                panther:
                    panther:
                        manager_options:
                            host: http://chrome:4444/wd/hub

Patch for Panther

Selenium is not supported here so I patch this file:

https://github.com/symfony/panther/blob/52e7ea43f0fd10c912830ccca87d93247634b715/src/PantherTestCaseTrait.php#L163-L169

like so:

--- /dev/null
+++ ../src/PantherTestCaseTrait.php
@@ -161,12 +161,16 @@
         }

         self::startWebServer($options);
+        self::$pantherClients[0] = self::$pantherClient = Client::createSeleniumClient($managerOptions['host'], null, self::$baseUri);

+        /*
         if (PantherTestCase::CHROME === $browser) {
             self::$pantherClients[0] = self::$pantherClient = Client::createChromeClient(null, null, $managerOptions, self::$baseUri);
         } else {
             self::$pantherClients[0] = self::$pantherClient = Client::createFirefoxClient(null, null, $managerOptions, self::$baseUri);
         }
+        */

         if (is_a(self::class, KernelTestCase::class, true)) {
             static::bootKernel($kernelOptions); // @phpstan-ignore-line

This works as expected, the patch could be made into a PR by somebody and it would work out of the box.