且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Symfony2配置behat

更新时间:2023-11-19 22:21:28

我正在分享我在所有Symfony2项目中一直使用的内容. Behat + Mink + Selenium

I'm sharing what I have been using for all my Symfony2 projects. Behat+Mink+Selenium

COMPOSER:

mySymfonyProject/composer.json:

"require": {
    "behat/behat": "2.5.*@stable",
    "behat/behat-bundle": "1.0.0",
    "behat/symfony2-extension": "1.1.2",
    "behat/mink": "1.5.0",
    "behat/mink-extension": "~1.3",
    "behat/mink-selenium2-driver": "1.1.1",
    "behat/mink-goutte-driver": "1.0.9"
},
"config": {
    "bin-dir": "bin"
},
"minimum-stability": "dev",

BEHAT

mySymfonyProject/behat.yml:

default:
    context:
        class: FeatureContext
    extensions:
        Behat\Symfony2Extension\Extension:
            mink_driver: true
            kernel:
                env: test
                debug: true
        Behat\MinkExtension\Extension:
            base_url: 'http://mysymfonyproject.local/app_test.php/'
            javascript_session: selenium2
            browser_name: firefox
            goutte: ~
            selenium2: ~
    paths:
        features: %behat.paths.base%/src
        bootstrap: %behat.paths.features%/Context

下载到您的项目中.可以在此处确保下载位于页面的中间.

Download into your project. It is here, make sure you download 2.43.1 version which is in the middle of the page.

运行它:java -jar selenium-server-standalone-2.43.1.jar

上下文特征

mySymfonyProject/src/Site/CommonBundle/Features/Context/FeatureContext.php

<?php

namespace Site\CommonBundle\Features\Context;

use Behat\MinkExtension\Context\MinkContext;
use Behat\Symfony2Extension\Context\KernelAwareInterface;
use Symfony\Component\HttpKernel\KernelInterface;

class FeatureContext extends MinkContext implements KernelAwareInterface
{
    /**
     * Hold Symfony kernel object.
     *
     * @var object Kernel Object.
     */
    protected $kernel;

    /**
     * Helps to use doctrine and entity manager.
     *
     * @param KernelInterface $kernelInterface Interface for getting Kernel.
     */
    public function setKernel(KernelInterface $kernelInterface)
    {
        $this->kernel = $kernelInterface;
    }

    //And your own methods
}

测试

拥有功能文件后,您可以像这样运行它们(一次运行.有关更多信息,请参阅behat doc):

When you have feature files you can run them like this (this runs all in one go. for more info read behat doc):

bin/behat @SiteCommonBundle