我正在尝试使用 Behat/Mink 测试“记住我”功能。这是我的场景:
Scenario: A user logs in ticking "Remember me". As he closes his browser and visits back the site, he should be automatically logged in
Given I am on "/login"
Then I should see "Site Login"
When I fill in "Username" with "test"
And I fill in "Password" with "test"
And I check "Remember me"
When I press "Login"
Then I should see "Welcome"
When I restart the browser
Then I go to "/login"
Then I should see "Welcome"
这是重新启动浏览器的定义:
/**
* @When /^I restart the browser$/
*/
public function iRestartTheBrowser()
{
$this->getSession()->restart();
}
我也试过
$this->getSession()->reset();
问题是由于 cookie 在浏览器重新启动时被删除,“记住我”功能不再起作用。有没有办法在不清除 cookie 的情况下在 mink 中重新启动?
最佳答案
您可以在重新启动 session 之前 get a cookie 和 set it back 之后:
$cookie = $session->getCookie('remember_me');
$session->restart();
// I'm not sure if visiting a page before setting a cookie is actually needed
// after restarting the session.
// It's definitely needed when setting a cookie before the first request
// (to set the cookie domain).
$session->visit('/')
$session->setCookie('remember_me', $cookie);
关于session-cookies - 如何重新启动浏览器以保留 Behat 中的 cookie?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22497125/