symfony / panther

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

Symfony HttpBrowser ignores PANTHER_EXTERNAL_BASE_URI #313

Closed romaricdrigon closed 4 years ago

romaricdrigon commented 4 years ago

Hello,

On our current project, we are replacing (deprecated) Goutte clients by HttpBrowser ones. Some tests are failing ; it appears that PANTHER_EXTERNAL_BASE_URI env variable is not respected. Requests without URI will be send to http://localhost.

Upon investigation, it seems that HttpBrowser ignores baseUri (from https://github.com/symfony/symfony/pull/35177). It prevents (strict) interoperability between clients. In our use case, we wanted to use Chrome for tests needing javascript, and HttpBrowser for the rest, which is a quite common use case I believe.

I don't know what to do from there: decorate HttpBrowser? At least this limitation should be documented.

odolbeau commented 4 years ago

I use an ugly workaround to avoid this behavior. I overridden the createHttpBrowserClient like this:

    protected static function createHttpBrowserClient(array $options = [], array $kernelOptions = []): HttpBrowserClient
    {
        parent::createHttpBrowserClient($options, $kernelOptions);

        self::$httpBrowserClient->request('GET', self::$baseUri);

        return self::$httpBrowserClient;
    }

As the HttpBrowser take a look at the history when you give it a non absolute path, making a first request to the absolute path when retrieving it does the job.

Of course, this is an ugly workaround as it makes an extra request each time you call createHttpBrowserClient... :/

lolautruche commented 4 years ago

Hello

@romaricdrigon @odolbeau You may want to test this PR: https://github.com/symfony/panther/pull/376