简体   繁体   English

Behat-实现步骤定义

[英]Behat - Implement Step definitions

I am having difficulties in setting Behat framework correctly. 我在正确设置Behat框架方面遇到困难。 The test runs fine, however the step definitions are not being picked up from the FeatureContext file. 该测试运行正常,但是未从FeatureContext文件中获取步骤定义。 Have tried out answers from other questions with no luck. 尝试了其他问题的解答,但是没有运气。 The behat.yml file is currently stored in the root of the project: behat.yml文件当前存储在项目的根目录中:

 default:
 paths:
    features: 'bin/features'
    bootstrap: 'bin/features/bootstrap'
 context:
    class:      'FeatureContext'
 extensions:
     Behat\MinkExtension\Extension:
         goutte: ~
         selenium2: ~

In the root of the project I have a bin folder which contains the standard behat files: behat.bat, webunit.bat etc. Within the bin folder I have the features folder which contains the file search.feature: 在项目的根目录中,我有一个bin文件夹,其中包含标准的behat文件:behat.bat,webunit.bat等。在bin文件夹中,我具有features文件夹,其中包含文件search.feature:

Feature: Search
In order to find a word
As a website user
I need to be able to search for a word

@javascript
Scenario: Searching for a word that does exist
Given I am on "http://drupalcamp.mx/search/"
When I fill in "Escriba las palabras clave" with "behat, mink"
And I press "Buscar"
Then I should see "Behavior Driven Development"
And I follow "Behavior Driven Development"
And I wait for 5 seconds 

Within the features folder I there is a bootstrap folder which contains the file "FeatureContext" 在features文件夹中,我有一个引导文件夹,其中包含文件“ FeatureContext”

    namespace features;
    use Behat\Behat\Context\ClosuredContextInterface,
        Behat\Behat\Context\TranslatedContextInterface,
        Behat\Behat\Context\BehatContext,
        Behat\Behat\Exception\PendingException;
    use Behat\Gherkin\Node\PyStringNode,
        Behat\Gherkin\Node\TableNode;
    use Behat\MinkExtension\Context\MinkContext;

    /**
    * Features context.
    */
    class FeatureContext extends MinkContext
    {
    /**
    * Initializes context.
    * Every scenario gets it's own context object.
    *
    * @param array $parameters context parameters (set them up through behat.yml)
    */
    public function __construct(array $parameters)
    {
        // Initialize your context here

    }
    /**
    * @Given /^I wait for (\d+) seconds$/
    */
    public function iWaitForSeconds($seconds)
    {
         $this->getSession()->wait($seconds*1000);
    }

When I run "behat" from the root directory of the project in CL, it fires the test in the browser, passes all but fails to recognise the step definition, it states "You can implement step definitions for undefined steps with these snippets:" which then it gives an example. 当我从CL中项目的根目录运行“ behat”时,它将在浏览器中触发测试,通过所有步骤,但无法识别步骤定义,它指出“您可以使用以下代码片段为未定义的步骤实现步骤定义:”然后给出一个例子。

You need to configure the autoloader to load classes from your custom bootstrap folder. 您需要配置自动加载器以从您的自定义引导文件夹加载类。

If you're using composer, here's an example of how it could be done: 如果您使用的是作曲家,请参考以下示例:

"autoload": {
    "psr-0": { "": ["src/", "bin/features/bootstrap/"]}
}

Note: bin folder is a really odd location to put your features or context files. 注意: bin文件夹是放置功能或上下文文件的真正奇怪的位置。

Related question with an answer: Behat + Symfony, Custom definitions are not loaded (actually, might be a duplicate). 与答案相关的问题: Behat + Symfony,未加载自定义定义 (实际上可能是重复的)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM