|
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; |
|
} |
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 :)