测试记住我使用Behat / Mink的功能

时间:2013-02-01 16:18:13

标签: behat mink

我正在尝试在Symfony2项目中测试Behat和Mink的记忆功能。但是,我的方法不起作用。

我尝试了以下内容:

#behat.yml

Scenario: Checking Remember me
  Given I am on "/"
  When  I fill in "username" with "john"
  And   I fill in "password" with "john"
  And   I check "remember_me"
  And   I press "Login"
  Then  I should be logged in
  When  I restart the browser
  Then  I should be logged in

Scenario: Not Checking Remember me
  Given I am on "/"
  When  I fill in "username" with "john"
  And   I fill in "password" with "john"
  And   I press "Login"
  Then  I should be logged in
  When  I restart the browser
  Then  I should be logged out

我的功能上下文包含(以及其他)以下方法:

#FeatureContext.php

/**
 * @Then /^I should be logged in$/
 */
public function iShouldBeLoggedIn()
{
    $this->assertElementOnPage('.user-area');
}

/**
 * @Given /^I should be logged out$/
 */
public function iShouldBeLoggedOut()
{
    $this->assertElementNotOnPage('.user-area');
}

/**
 * @When /^I restart the browser$/
 */
public function iRestartTheBrowser()
{
    $driver = $this->getSession()->getDriver();
    $session = new Session($driver);
    $session->start();
    $session->visit('/');
}

问题出在iRestartTheBrowser()之内。这不是它应该做的事情。我正在寻找一种清除会话数据但保留cookie的方法。有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

我认为您的规范可能会更好,因为

Scenario: Checking Remember me
    Given I have logged in before and selected remember me
    When I visit "some protected web page"
    Then I should be logged in

Given可以设置记住我的状态,也许上下文可以帮助您分离重置会话​​的需要吗?

对于非专业人士和开发人员来说,上述内容也更容易阅读