bin / behat @FootballTeamBundle很好,但bin / phing不是用于运行FeatureContext

时间:2014-09-11 20:57:36

标签: symfony selenium behat mink

如果我在终端中单独使用bin/behat @FootballTeamBundle,则会拍摄错误截屏并保存在build/behat/文件夹下,但是如果我运行bin/phing则{ {1}}文件似乎整体被忽略,因此不会触发屏幕截图或其内部方法(例如我等待**秒),这很奇怪。有人知道它的解决方案吗?

我还在build.xml中将行更新为FeatureContextbin/behat -f progress --format html --out ${dir-report}/behat.html,但没有任何更改。

提前致谢。

/var/www/html/local/sport/behat.yml

bin/behat @FootballTeamBundle

/var/www/html/local/sport/src/Football/TeamBundle/Features/Context/FeatureContext.php

default:
    context:
        class: FeatureContext
        parameters:
            screen_shots_path: 'build/behat/'
    extensions:
        Behat\Symfony2Extension\Extension:
            mink_driver: true
            kernel:
                env: test
                debug: true
        Behat\MinkExtension\Extension:
            base_url: 'http://localhost/local/sport/web/app_test.php/'
            files_path: 'dummy/'
            browser_name: chrome
            goutte: ~
            selenium2: ~
    paths:
        features: src/Football/TeamBundle/Features
        bootstrap: %behat.paths.features%/Context

/var/www/html/local/sport/build.xml

<?php

namespace Football\TeamBundle\Features\Context;

use Behat\MinkExtension\Context\MinkContext;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Behat\Mink\Driver\Selenium2Driver;

class FeatureContext extends MinkContext
{
    /**
     * Where the failure images will be saved.
     *
     * @var string
     */
    protected $screenShotsPath;

    /**
     * Comes from behat.yml file.
     *
     * @param $parameters
     */
    public function __construct($parameters)
    {
        $this->screenShotsPath = $parameters['screen_shots_path'];
    }

    /**
     * Take screen-shot when step fails.
     * Works only with Selenium2Driver.
     *
     * @AfterStep
     * @param $event
     * @throws \Behat\Mink\Exception\UnsupportedDriverActionException
     */
    public function takeScreenshotAfterFailedStep($event)
    {
        if (4 === $event->getResult()) {
            $driver = $this->getSession()->getDriver();

            if (! ($driver instanceof Selenium2Driver)) {
                throw new UnsupportedDriverActionException(
                    'Taking screen-shots is not supported by %s, use Selenium2Driver instead.',
                    $driver
                );

                return;
            }

            if (! is_dir($this->screenShotsPath)) {
                mkdir($this->screenShotsPath, 0777, true);
            }

            $filename = sprintf(
                '%s_%s_%s.%s',
                $this->getMinkParameter('browser_name'),
                date('Y-m-d') . '_' . date('H:i:s'),
                uniqid('', true),
                'png'
            );

            file_put_contents($this->screenShotsPath . '/' . $filename, $driver->getScreenshot());
        }
    }

    /**
     * @Then /^I wait for "([^"]*)" seconds$/
     *
     * @param $seconds
     */
    public function iWaitForGivenSeconds($seconds)
    {
        $this->getSession()->wait($seconds*1000);
    }
}

1 个答案:

答案 0 :(得分:2)

我从未处理过phing或Symfony的粉丝。希望这纯粹是一个配置问题。尝试使用%behat.paths.base%更具体地了解您的路径。还可以使用命名空间;)

default:
    context:
        class: Football\TeamBundle\Features\Context\FeatureContext
        parameters:
            screen_shots_path: %behat.paths.base%/build/behat/
    extensions:
        Behat\Symfony2Extension\Extension:
            mink_driver: true
            kernel:
                env: test
                debug: true
        Behat\MinkExtension\Extension:
            base_url: 'http://localhost/local/sport/web/app_test.php/'
            files_path: %behat.paths.base%/dummy/
            browser_name: chrome
            goutte: ~
            selenium2: ~
    paths:
        features: %behat.paths.base%/src
        bootstrap: %behat.paths.features%/Context

%behat.paths.base%是您behat.yml所在的位置。所以用它来更新配置,设置与之相关的正确路径。