Behat 错误 - 步骤已定义

zar*_*hdi 1 php selenium automation behat selenium-chromedriver

这是我的功能文件 功能:作为访问者,我应该能够加载主页

  @javascript @program
  Scenario: View first page
    When I fill in the following:
      | username | myusername |
      | password | mypass |
    And press "Login"
    Then I should see "Dashboard"
    Then I go to "/programs/list"
Run Code Online (Sandbox Code Playgroud)

我的 composer.json 如下:

{
    "require": {
        "behat/mink-extension": "^2.2",
        "behat/mink-goutte-driver": "^1.2",
        "behat/mink-selenium2-driver": "^1.3"
    }
}
Run Code Online (Sandbox Code Playgroud)

我的 behat.yml 文件如下:

default:
  extensions:
    Behat\MinkExtension:
      base_url: http://myURL.com
      selenium2: ~
      browser_name: 'chrome'
  suites:
      defaults:
          contexts:
              - FeatureContext
              - Behat\MinkExtension\Context\MinkContext
Run Code Online (Sandbox Code Playgroud)

我的 FeatureContext.php 文件是

class FeatureContext extends MinkContext implements Context, 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()
    {
        $this->driver = new \Behat\Mink\Driver\Selenium2Driver('chrome');
        $this->session = new \Behat\Mink\Session($this->driver);
        $this->session->start();
    }
    public function iAmOnHomepage($arg1)
    {
        $this->getSession()->visit($this->locatePath('http://myURL.com'));
    }
    /**
     * @When |I fill in :arg1 with :arg2
     */
    public function iFillInWith($username, $password)
    {
        $MinkContext = new MinkContext;
        $MinkContext -> assertFieldContains ("username" , $username);
        $MinkContext -> assertFieldContains ("password" , $password);
        $this->driver->close();
    }
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

 FeatureContext::iAmOnHomepage()  
 Behat\MinkExtension\Context\MinkContext::iAmOnHomepage() And press
 "Login"   Step "/^(?:|I )am on (?:|the )homepage$/" is already defined
 in FeatureContext::iAmOnHomepage()
Run Code Online (Sandbox Code Playgroud)

lau*_*uda 6

在您的情况下,您收到此错误是因为您在 FeatureContext 中两次加载 MinkContext 并通过 behat.yml。

如果您使用 MinkContext 扩展了 FeatureContext,那么您不必在 behat.yml 中声明 MinkContext。从 behat.yml 中
删除该行Behat\MinkExtension\Context\MinkContext应该可以解决您的问题。