【发布时间】:2016-07-02 14:05:56
【问题描述】:
我是 Behat 和 Mink 的新手,我正在尝试使用 MinkContext 扩展 FeatureContext,但是当我这样做时,每一步都会抛出一个错误,指出在 MinkContext 中定义的第一个函数也在 FeatureContext 中定义(它不是t)。错误信息如下:
Step "/^(?:|I )am on (?:|the )homepage$/"
is already defined in FeatureContext::iAmOnHomepage()
如果我从类中删除第一个函数,每一步都会抛出相同的错误,但现在它引用了 MinkContext 类中的第二个函数:
Step "/^(?:|I )am on "(?P<page>[^"]+)"$/"
is already defined in FeatureContext::visit()
使用 RawMinkContext 扩展 FeatureContext 效果很好。
这可能是什么原因造成的?
---- 编辑(附加信息)------------
我正在使用 Behat 3。
这是我当前的全部 FeatureContext.php,但我仍然收到错误消息。我搜索了包含我的 Behat 安装的整个文件夹,但我只能找到一个
<?php
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\MinkContext;
/**
* Defines application features from the specific context.
*/
class FeatureContext extends MinkContext implements SnippetAcceptingContext
{
/**
* Initializes context.
*
* Every scenario gets its own context instance.
* You can also pass arbitrary arguments to the
* context constructor through behat.yml.
*/
public function __construct()
{
date_default_timezone_set("US/Eastern");
}
}
这是我的 behat.yml 文件:
# behat.yml
default:
extensions:
Behat\MinkExtension:
goutte: ~
selenium2: ~
base_url: https://harvest.cals.ncsu.edu/
suites:
default:
contexts:
- FeatureContext
- Behat\MinkExtension\Context\MinkContext
这是 MinkContext.php 的顶部: 命名空间 Behat\MinkExtension\Context;
use Behat\Behat\Context\TranslatableContext;
use Behat\Gherkin\Node\TableNode;
/**
* Mink context for Behat BDD tool.
* Provides Mink integration and base step definitions.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class MinkContext extends RawMinkContext implements TranslatableContext
{
/**
* Opens homepage
* Example: Given I am on "/"
* Example: When I go to "/"
* Example: And I go to "/"
*
* @Given /^(?:|I )am on (?:|the )homepage$/
* @When /^(?:|I )go to (?:|the )homepage$/
*/
public function iAmOnHomepage()
{
$this->visitPath('/');
}
...
--- 编辑 2:工作版本 ------------
FeatureContext.php:
<?php
use Behat\Behat\Context\Context;
use Behat\Behat\Context\SnippetAcceptingContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use Behat\MinkExtension\Context\MinkContext;
use Behat\Mink\WebAssert;
/**
* Defines application features from the specific context.
*/
class FeatureContext extends MinkContext implements Context, SnippetAcceptingContext
{
...
behat.yml(现在带有 enable Chrome 的 Selenium 标签)
# behat.yml
default:
extensions:
Behat\MinkExtension:
goutte: ~
selenium2:
wd_host: "http://127.0.0.1:4444/wd/hub"
# chrome
capabilities: { "browserName": "chrome", "browser": "chrome", "version": "25", 'chrome': {'switches':['--no-sandbox']}}
base_url: https://harvest.cals.ncsu.edu/
browser_name: chrome
suites:
default:
contexts:
- FeatureContext
【问题讨论】:
-
也许你在其他地方加载了 MinkContext。你使用什么版本的 Behat。使用您删除的方法从您的 FeatureContext 发布一些代码 sn-ps。
-
它看起来确实被加载了两次,但我在任何地方都找不到第二个副本,所以它一定是 MinkContext.php 以某种方式被调用了两次。
-
也许你在 behat.yml 中使用 MinkContext,删除它,它应该可以工作。 behat.yml 应该只包含你的本地上下文
-
所以你的功能上下文应该有扩展 MinkContext 实现上下文,SnippetAcceptingContext 并且你应该从 behat.yml 上下文中删除带有 MinkContext 的行
-
就是这样。如果您将其发布为答案,我会将其标记为正确。