Search code examples
phpbehat

Behat/Mink doesn't find field by id


I am trying to fill out a field. Why does Behat not find the field by id?

Input Field:

   <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 Code:

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

Behat:

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

It says it doesn't find a field by 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)

Solution

  • I found a work around. I filled out the input filed with 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);
      }
    

    Important, don't forget the @javascript annotation.

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

    If someone has a better solution, please share I would like to know.