Skip to content

Instantly share code, notes, and snippets.

@dsantuc
Last active March 31, 2020 02:37
Show Gist options
  • Select an option

  • Save dsantuc/68b91f9c2fcc01d14802d60524afcd60 to your computer and use it in GitHub Desktop.

Select an option

Save dsantuc/68b91f9c2fcc01d14802d60524afcd60 to your computer and use it in GitHub Desktop.

Revisions

  1. David Santucci renamed this gist Feb 14, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. David Santucci renamed this gist Feb 14, 2018. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. David Santucci created this gist Feb 14, 2018.
    70 changes: 70 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    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;
    }