Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public function __construct(BrowserManagerInterface $browserManager, ?string $ba
$this->baseUri = $baseUri;
}

public function getBrowserManager(): BrowserManagerInterface
{
return $this->browserManager;
}

public function __destruct()
{
$this->quit();
Expand Down Expand Up @@ -242,7 +247,7 @@ public function restart()
$this->webDriver->manage()->deleteAllCookies();
}

$this->quit();
$this->quit(false);
$this->start();
}

Expand Down Expand Up @@ -334,13 +339,16 @@ public function getWindowHandles()
return $this->webDriver->getWindowHandles();
}

public function quit()
public function quit(bool $quitBrowserManager = true)
{
if (null !== $this->webDriver) {
$this->webDriver->quit();
$this->webDriver = null;
}
$this->browserManager->quit();

if ($quitBrowserManager) {
$this->browserManager->quit();
}
}

public function takeScreenshot($saveAs = null)
Expand Down
41 changes: 34 additions & 7 deletions src/PantherTestCaseTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,15 @@ trait PantherTestCaseTrait
protected static $goutteClient;

/**
* @var PantherClient|null
* @var PantherClient|null The primary Panther client instance created
*/
protected static $pantherClient;

/**
* @var PantherClient[] All Panther clients, the first one is the primary one (aka self::$pantherClient)
*/
protected static $pantherClients = [];

/**
* @var array
*/
Expand Down Expand Up @@ -83,8 +88,14 @@ public static function stopWebServer()
}

if (null !== self::$pantherClient) {
self::$pantherClient->quit();
foreach (self::$pantherClients as $i => $pantherClient) {
// Stop ChromeDriver only when all sessions are already closed
$pantherClient->quit(false);
}

self::$pantherClient->getBrowserManager()->quit();
self::$pantherClient = null;
self::$pantherClients = [];
}

if (null !== self::$goutteClient) {
Expand Down Expand Up @@ -129,16 +140,20 @@ public static function isWebServerStarted()
}

/**
* @param array $options see {@see $defaultOptions}
* @param array $kernelOptions
* Creates the primary browser.
*
* @param array $options see {@see $defaultOptions}
*/
protected static function createPantherClient(array $options = [], array $kernelOptions = []): PantherClient
{
self::startWebServer($options);
if (null === self::$pantherClient) {
self::$pantherClient = Client::createChromeClient(null, null, [], self::$baseUri);
if (null !== self::$pantherClient) {
return self::$pantherClient;
}

self::startWebServer($options);

self::$pantherClients[0] = self::$pantherClient = Client::createChromeClient(null, null, [], self::$baseUri);

if (\is_a(self::class, KernelTestCase::class, true)) {
static::bootKernel($kernelOptions);
}
Expand All @@ -150,6 +165,18 @@ protected static function createPantherClient(array $options = [], array $kernel
return self::$pantherClient;
}

/**
* Creates an additional browser. Convenient to test apps leveraging Mercure or WebSocket (e.g. a chat).
*/
protected static function createAdditionalPantherClient(): PantherClient
{
if (null === self::$pantherClient) {
return self::createPantherClient();
}

return self::$pantherClients[] = self::$pantherClient = new PantherClient(self::$pantherClient->getBrowserManager(), self::$baseUri);
}

/**
* @param array $options see {@see $defaultOptions}
* @param array $kernelOptions
Expand Down
2 changes: 1 addition & 1 deletion tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public function testCookie(callable $clientFactory, string $type)
public function testServerPort(callable $clientFactory): void
{
$expectedPort = $_SERVER['PANTHER_WEB_SERVER_PORT'] ?? '9080';
$client = $clientFactory();
$clientFactory();
$this->assertEquals($expectedPort, \mb_substr(self::$baseUri, -4));
}
}
33 changes: 33 additions & 0 deletions tests/MultiClientsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Panther project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Symfony\Component\Panther\Tests;

class MultiClientsTest extends TestCase
{
public function testMultiClient(): void
{
$client = self::createPantherClient();
$client->request('GET', '/cookie.php');

$crawler = $client->request('GET', '/cookie.php');
$this->assertSame('1', $crawler->filter('#barcelona')->text());

$client2 = self::createAdditionalPantherClient();
$crawler2 = $client2->request('GET', '/cookie.php');
$this->assertSame('0', $crawler2->filter('#barcelona')->text());

// Check that the cookie in the other client hasn't changed
$this->assertSame('1', $crawler->filter('#barcelona')->text());
}
}