贝哈特/水貂不按id发现场

问题描述 投票:1回答:2

我想填写的字段。为什么贝哈特未找到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)
php behat
2个回答
1
投票

水貂的方法fillField()使用findField()发现场 - 和findField()使用name的选择,而不是id。这就是为什么你最初的方法没有奏效。见source水貂的类TraversableElement的详细信息。


0
投票

我发现了一个解决方法。我填写了输入字段使用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

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

© www.soinside.com 2019 - 2024. All rights reserved.