Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better way to set Cookie before visiting a page #122

Open
eugef opened this issue Jan 29, 2015 · 12 comments
Open

Better way to set Cookie before visiting a page #122

eugef opened this issue Jan 29, 2015 · 12 comments

Comments

@eugef
Copy link

eugef commented Jan 29, 2015

For now it is impossible to set cookie before visiting the page.

The reason is that ZombieDriver::setCookie() uses browser.window.location.hostname which is undefined before you actually visit the page.

My workaround was to use the code which allows to specify domain explicitly:

public function setBrowserCookie($domain, $name, $value)
    {
        if ($value === null) {
            $this->getSession()->evaluateScript(
                'browser.deleteCookie(' . json_encode($name). ')'
            );
        } else {
            $this->getSession()->evaluateScript(
                sprintf('browser.setCookie({name: %s, domain: %s}, %s)',
                    json_encode($name),
                    json_encode($domain),
                    json_encode($value)
                )
            );
        }
    }

So my questions are

  1. Is it possible to set browser.window.location.hostname before visiting the page?
  2. OR is it possible to refactor ZombieDriver::setCookie() so it accepts custom domain?
@aik099
Copy link
Member

aik099 commented Jan 29, 2015

In real browser you can't set cookie before visiting the page, because the cookie domain must match to page you set them on for page itself to recognize them.

Why you need to set cookie before visiting a page?

@aik099
Copy link
Member

aik099 commented Jan 29, 2015

Also documentation http://mink.behat.org/en/latest/guides/session.html says that before you do anything you must visit a page first.

@aik099
Copy link
Member

aik099 commented Jan 29, 2015

So my questions are

No matter which approach you decide to implement keep in mind, that new behavior must be implemented in all other Mink drivers as well.

@stof
Copy link
Member

stof commented Jan 29, 2015

Setting cookies on other domains will be impossible on Selenium or Sahi. This is why the Mink API does not allow it

@eugef
Copy link
Author

eugef commented Jan 29, 2015

@aik099,
I have different behavior on the page depending on cookie exists or not - that is why I need to set it.
Also in a real browser you can send cookies in the request header. I tried to do this with ZombieJs but seems "Cookie" header is ignored.

@stof,
Cookie will be set for the same domain.

@aik099
Copy link
Member

aik099 commented Jan 29, 2015

Maybe you can create wrapper page or pass some flag to a page, that when used will set cookie and redirect to correct page.

@eugef
Copy link
Author

eugef commented Jan 30, 2015

@aik099 , i don't think it is a good solution, this makes my tests/implementation depend on ZombieJs driver restrictions.

Possible solution would be allow to set domain without visiting a pageand then you can set cookie.

@stof
Copy link
Member

stof commented Jan 30, 2015

@eugef it would make your test depend on the Mink API itself

@aik099
Copy link
Member

aik099 commented Jan 30, 2015

@eugef , aren't you trying to set session id in that cookie? I know there is an approach to create session and then inject it's ID in cookie so user would be logged-in on website. I recommend you performing login each time instead.

@eugef
Copy link
Author

eugef commented Jan 30, 2015

@aik099, no it is not a session cookie. Cookie is used by JS to hide/show some elements on the page.

@TerjeBr
Copy link

TerjeBr commented Mar 14, 2017

This worked for me:

use Symfony\Component\BrowserKit\Cookie;

$cookie = new Cookie($name, $value);
$this->getSession()->setRequestHeader('Cookie', (string)$cookie);

@seyfer
Copy link

seyfer commented Dec 26, 2018

@TerjeBr I'm getting this

 Request headers manipulation is not supported by Behat\Mink\Driver\Selenium2Driver (Behat\Mink\Exception\UnsupportedDriverActionException)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants