如何在behat中启用cookies进行登录功能测试?

How to enable cookies in behat to perform a login functional test?

我正在尝试使用 SonataUserBundle 登录表单和 behat feature 测试有效登录,我想检查用户是否被重定向到 login_check 但由于 cookie 问题,它似乎永远无法工作。

我的问题很简单,如何使用 behat 执行有效的功能测试以检查用户是否登录到 Symfony2 应用程序?

我的 behat.yml 文件中有此配置:

default:
    formatters:
        pretty: true
    autoload:
        '': %paths.base%/features/bootstrap
    suites:
        test_suite:
            type: symfony_bundle
            bundle: MyBundle
            contexts:
                - Acme\MyBundle\Features\Context\FeatureContext:
                    session:   '@session'
                    output_path: build/behat/output
                    screen_shot_path: build/behat/screenshot
            mink_javascript_session: selenium2
    extensions:
        Behat\Symfony2Extension: ~
        Behat\MinkExtension:
            base_url: http://<internalURL>/web/app_test.php
            sessions:
                goutte: # fast, CLI, browser, no javascript support
                    goutte: ~
                selenium2: # fast, CLI, opens up a browser
                    selenium2: ~
                symfony2: # bleeding fast, CLI, no browser
                    symfony2: ~

Scenario: Clicking on the submit button with good credentials
    When I fill in "_username" with "mylogin"
    And I fill in "_password" with "mypassword"
    Then I press "Login"
    And print last response
    Then I should be on "/login_check"

当我在 behat 功能配置文件中执行 And print last response 时,出现此错误:

Your session has timed out, or you have disabled cookies.

特征的结果:

Then I should be on "/login_check"
      Current page is "/<internalURL>/app_test.php/login",
 but "/<internalURL>/app_test.php/login_check" expected. 

(Behat\Mink\Exception\ExpectationException)

为了测试此行为,您可以创建此方法:

/**
 * @When I restart the browser
 */
public function restartBrowser()
{
    $rememberMe = $this->getSession()->getCookie('REMEMBER_ME');
    $this->getSession()->restart();
    $this->visitPath('/');
    $this->getSession()->setCookie('REMEMBER_ME', $rememberMe);
}

请注意,如果您设置了 framework.session.name,cookie 可以以您的应用程序名称作为前缀。 所以 REMEMBER_ME 可以是 YOURAPP_REMEMBER_ME

所以这里有一个用法的例子:

    Scenario: I can remember my login in my browser
        Given I fill in "_username" with "username"
        And I fill in "_password" with "password"
        And I check "Remember me?"
        And I press "Login"
        Then I should be on my account homepage
        When I restart the browser
        And I go to "/"
        And I should see 1 "body.logged-in" elements

希望有用。

此致。