Behat/Mink 没有通过 id 找到字段

Behat/Mink doesn't find field by id

我正在尝试填写一个字段。 为什么 Behat 没有通过 id 找到字段?

输入字段:

   <input class="js-text-full text-full form-text required" data-drupal-selector="edit-field-article-nr-supplier-0-value" type="text" id="edit-field-article-nr-supplier-0-value" name="field_article_nr_supplier[0][value]" value="" size="60" maxlength="255" placeholder="" required="required" aria-required="true">

PHP代码:

public function fillField($field, $value)
{
    $field = $this->fixStepArgument($field);
    $value = $this->fixStepArgument($value);
    $this->getSession()->getPage()->fillField($field, $value);
}

行为:

When I fill in "edit-field-article-nr-supplier-0-value" with "12"

它说它没有通过 id 找到字段:

 When I fill in "edit-field-article-nr-supplier-0-value" with "12"   # Drupal\DrupalExtension\Context\MinkContext::fillField()
  Form field with id|name|label|value|placeholder "edit-field-article-nr-supplier-0-value" not found. (Behat\Mink\Exception\ElementNotFoundException)

我找到了解决方法。我填写了 javascript

的输入
  /**
   * @When I fill in :value on the field :field with javascript
   */
  public function findAllInputFields($value, $field){
      $javascript = "window.onload = function () {var e = document.getElementById('$field').value='$value';}";
      $this->getSession()->executeScript($javascript);
  }

重要,不要忘记@javascript注解。

@javascript
Scenario: Fill out field
    When I fill in 12 on the field "edit-field-article-nr-supplier-0-value" with javascript

如果有人有更好的解决方案,请分享我想知道。

Mink 的方法 fillField() 使用 findField() 来查找字段 - 而 findField() 使用 name 作为选择器而不是 id。这就是为什么您最初的方法不起作用的原因。有关详细信息,请参阅 Mink 的 class TraversableElement 的 source