Skip to content

Commit c8d0803

Browse files
committed
Refactor CookieSetter to optimize browser navigation
Improved the cookie setter logic to avoid unnecessary page navigations: - Extract driver start logic into ensureDriverStarted() method - Unify page load detection for Selenium2Driver and ChromeDriver - Skip base_url visit when page is already loaded (about:blank, data:,) - Move prepareMinkSessionIfNeeded() after SymfonyDriver early return - Add readonly modifier to class and type declarations to methods - Simplify shouldMinkSessionBePrepared() logic with early returns
1 parent 8f755d0 commit c8d0803

2 files changed

Lines changed: 20 additions & 21 deletions

File tree

src/Sylius/Behat/Service/Setter/CookieSetter.php

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,19 @@
2020
use FriendsOfBehat\SymfonyExtension\Driver\SymfonyDriver;
2121
use Symfony\Component\BrowserKit\Cookie;
2222

23-
final class CookieSetter implements CookieSetterInterface
23+
final readonly class CookieSetter implements CookieSetterInterface
2424
{
2525
public function __construct(
2626
private Session $minkSession,
2727
private \ArrayAccess $minkParameters,
2828
) {
2929
}
3030

31-
public function setCookie($name, $value)
31+
public function setCookie(string $name, string $value): void
3232
{
3333
$driver = $this->minkSession->getDriver();
3434

35-
if ($driver instanceof ChromeDriver || $driver instanceof PantherDriver) {
36-
if (!$driver->isStarted()) {
37-
$driver->start();
38-
}
39-
}
40-
41-
$this->prepareMinkSessionIfNeeded($this->minkSession);
35+
$this->ensureDriverStarted($driver);
4236

4337
if ($driver instanceof SymfonyDriver) {
4438
$driver->getClient()->getCookieJar()->set(
@@ -48,9 +42,17 @@ public function setCookie($name, $value)
4842
return;
4943
}
5044

45+
$this->prepareMinkSessionIfNeeded($this->minkSession);
5146
$this->minkSession->setCookie($name, $value);
5247
}
5348

49+
private function ensureDriverStarted(mixed $driver): void
50+
{
51+
if (($driver instanceof ChromeDriver || $driver instanceof PantherDriver) && !$driver->isStarted()) {
52+
$driver->start();
53+
}
54+
}
55+
5456
private function prepareMinkSessionIfNeeded(Session $session): void
5557
{
5658
if ($this->shouldMinkSessionBePrepared($session)) {
@@ -66,18 +68,19 @@ private function shouldMinkSessionBePrepared(Session $session): bool
6668
return false;
6769
}
6870

69-
if ($driver instanceof Selenium2Driver && $driver->getWebDriverSession() === null) {
70-
return true;
71+
if ($driver instanceof Selenium2Driver) {
72+
return $driver->getWebDriverSession() === null || $this->isPageNotLoaded($session->getCurrentUrl());
7173
}
7274

7375
if ($driver instanceof ChromeDriver) {
74-
return true;
76+
return $this->isPageNotLoaded($session->getCurrentUrl());
7577
}
7678

77-
if (str_contains($session->getCurrentUrl(), $this->minkParameters['base_url'])) {
78-
return false;
79-
}
79+
return !str_contains($session->getCurrentUrl(), $this->minkParameters['base_url']);
80+
}
8081

81-
return true;
82+
private function isPageNotLoaded(string $url): bool
83+
{
84+
return in_array($url, ['', 'about:blank', 'data:,'], true);
8285
}
8386
}

src/Sylius/Behat/Service/Setter/CookieSetterInterface.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,5 @@
1515

1616
interface CookieSetterInterface
1717
{
18-
/**
19-
* @param string $name
20-
* @param string $value
21-
*/
22-
public function setCookie($name, $value);
18+
public function setCookie(string $name, string $value): void;
2319
}

0 commit comments

Comments
 (0)