Skip to content

Instantly share code, notes, and snippets.

@dsantuc
Last active March 31, 2020 02:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsantuc/68b91f9c2fcc01d14802d60524afcd60 to your computer and use it in GitHub Desktop.
Save dsantuc/68b91f9c2fcc01d14802d60524afcd60 to your computer and use it in GitHub Desktop.
Getting all cookies from various BeHat/Mink browser drivers
public function getAllCookies ($driver) {
$cookies = array();
if ($driver instanceof Behat\Mink\Driver\BrowserKitDriver) {
$cookies = $this->getBrowserKitCookies($driver);
}
else if ($driver instanceof Behat\Mink\Driver\Selenium2Driver) {
$cookies = $this->getSeleniumCookies($driver);
}
else if ($driver instanceof Behat\Mink\Driver\ZombieDriver) {
$cookies = $this->getZombieCookies($driver);
}
else if ($driver instanceof Zumba\Mink\Driver\PhantomJSDriver) {
$cookies = $this->getPhantomJSCookies($driver);
}
return $cookies;
}
private function getBrowserKitCookies ($driver) {
$cookies = array();
$cookieJar = $driver->getClient()->getCookieJar();
foreach ($cookieJar->all() as $cookie) {
$cookies[] = array(
"name"=>$cookie->getName(),
"value"=>$cookie->getValue()
);
}
return $cookies;
}
private function getSeleniumCookies ($driver) {
$cookies = array();
$wdSession = $driver->getWebDriverSession();
return $this->urlDecodeCookies($wdSession->getAllCookies());
}
private function urlDecodeCookies ($cookiesIn) {
$cookies = array();
foreach ($cookiesIn as $cookie) {
$cookie['value'] = urldecode($cookie['value']);
$cookies[] = $cookie;
}
return $cookies;
}
private function getZombieCookies ($driver) {
$cookies = json_decode($driver->getServer()->evalJS("JSON.stringify(browser.cookies);"), true);
return $this->urlDecodeCookies($cookies);
}
private function getPhantomJSCookies ($driver) {
$cookies = array();
foreach ($driver->getBrowser()->cookies() as $cookie) {
$cookies[] = array(
"name"=>$cookie->getName(),
"value"=>$cookie->getValue()
);
}
return $cookies;
}
@afilina
Copy link

afilina commented Sep 26, 2019

Thanks! I had no idea that ->getWebDriverSession()->getAllCookies() even existed. This method wasn't part of the DriverInterface but specific to the Selenium2Driver implementation, which is why I completely overlooked it.

@dsantuc
Copy link
Author

dsantuc commented Sep 26, 2019

Glad you found this useful. This was kind of a hack because I wanted to be able to reuse authenticated sessions across tests, as described here: https://www.gnaritas.com/2017/04/26/automated-testing-for-wordpress-4/

@afilina
Copy link

afilina commented Sep 26, 2019

In my case, I needed to download a file using Guzzle while running Selenium tests. Downloading files using Selenium and moving them to the right location on the Docker container that runs Behat tests was a pain. The problem with Guzzle, however, is that it needs the cookie, since the file download needs authentication too. It's really the dual approach to accessing my SUT that made it necessary to extract cookies from Selenium and put them in a Guzzle CookieJar. I'll probably need to blog about this too :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment