3

我正在使用 Behat 进行一些自动化测试,并添加了 Mink 及其 Goutte 驱动程序。我正在使用最新版本的 Behat 和 Mink。

我已将 Mink 扩展添加到功能上下文文件中,当我运行以下简单功能时它可以工作:

Feature:...
  Scenario: See A Blog Post
    Given I am on the homepage
    And I follow "login"
    Then I should be on "/login"
    And I should see "Login"

但是,当我尝试下一步并尝试填写一些字段时:

    And I fill in "username" with "admin"

其中用户名:

<input class="input-field" type="text" id="username"/>

我收到以下错误:

Malformed field path "" (InvalidArgumentException)

任何帮助都感激不尽,

谢谢!

4

2 回答 2

5

name="username"这是因为您的字段上没有属性。我遇到了类似的问题,但我正在尝试测试一个不能具有名称属性的 Stripe 实例。它出现id并且title不起作用。

于 2015-02-28T19:03:18.060 回答
0

勾选的答案为我解决了这个问题。

我还发现您可以使用以下语法填写表格:

When I fill in the following:
  | username            | admin              |
  | email               | myemail@domain.com |
  | password            | 5uP3r5ecUr3P4s5w0rD|

我还创建了一个自定义 FeatureContext 函数来帮助查找其他元素:

protected function findElementBySelector($selector)
{
    $page = $this->getSession()->getPage();
    $element = $page->find('css', $selector);

    if (null === $element) {
        throw new \Exception(
            sprintf('Element with %s not found.', $selector));
    }

    return $element;
}

其他人使用此函数来查找元素并对其执行操作:

/**
 * @Then I click the element :selector
 */
public function iClickTheElement($selector)
{
    $link = $this->findElementBySelector($selector);
    $link->click();
}

我在我的 .feature 中使用了这个,如下所示:

And I ...
And I click the element ".my-link"
Then I ...

其中 .my-link =<a href="http://domain.com" class="my-link">My Link</a>

下面的链接帮助创建了我的自定义 FeatureContext 函数: http: //mink.behat.org/en/latest/guides/traversing-pages.html

于 2016-04-18T20:47:24.943 回答