phpcookiesselenium-chromedriversymfony-panther

Symfony/Panther - Cookies are disabled on chromium-driver in Docker


I'm trying to write E2E tests with Panther. My tests are dockerized. I load chromium-driver and the Panther library :

RUN apt-get update ;\
    apt-get install -y default-mysql-client php8.2-mysql php8.2-ldap php8.2-xdebug git chromium-driver

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer ;\
    chmod 777 /var/www

RUN apt-get clean ;\
    rm -rf /tmp/* /var/tmp/*

RUN cd /var/www/html;\
    composer req --dev symfony/panther;\
    composer req --dev symfony/framework-bundle

ENV PANTHER_WEB_SERVER_DIR=/var/www/html
ENV PANTHER_WEB_SERVER_PORT=8080
ENV PANTHER_NO_SANDBOX=1
ENV KERNEL_CLASS=PantherTestKernel

But when I launch the container and run a simple test to log in, I get an error with the message "Your browser either does not know how to handle cookies, or refuses to handle them."

use Symfony\Component\Panther\PantherTestCase;

class E2eTest extends PantherTestCase
{
    public function testMyApp(): void
    {
        $client = static::createPantherClient(['port'=>8080]);
        $crawler = $client->request('GET', '/login_page.php');

        $crawler->filter('#username')->sendKeys('test');
        $crawler->filter('input[type="submit"]')->click();

        $crawler = $client->waitFor('#password');
        $crawler->filter('#password')->sendKeys('test123');
        $crawler->filter('input[type="submit"]')->click();

        $crawler = $client->waitFor('#main-container', 10, 2000);
        $client->takeScreenshot('tests/screenshot.png');
        $this->assertSelectorNotExists('.alert'); #fails
    }
}

Solution

  • I finally found the solution. It was simply due to the browser which allow cookies only on domain, not 127.0.0.1

    So, replace this line

    $client = static::createPantherClient(['port'=>8080]);
    

    By that one

    $client = static::createPantherClient(['hostname'=>'localhost', 'port'=>8080]);