You are here

public function ThunderFormFieldTestTrait::setFieldValue in Thunder 8.3

Same name and namespace in other branches
  1. 8.5 tests/src/FunctionalJavascript/ThunderFormFieldTestTrait.php \Drupal\Tests\thunder\FunctionalJavascript\ThunderFormFieldTestTrait::setFieldValue()
  2. 8.2 tests/src/FunctionalJavascript/ThunderFormFieldTestTrait.php \Drupal\Tests\thunder\FunctionalJavascript\ThunderFormFieldTestTrait::setFieldValue()
  3. 8.4 tests/src/FunctionalJavascript/ThunderFormFieldTestTrait.php \Drupal\Tests\thunder\FunctionalJavascript\ThunderFormFieldTestTrait::setFieldValue()
  4. 6.2.x tests/src/FunctionalJavascript/ThunderFormFieldTestTrait.php \Drupal\Tests\thunder\FunctionalJavascript\ThunderFormFieldTestTrait::setFieldValue()
  5. 6.0.x tests/src/FunctionalJavascript/ThunderFormFieldTestTrait.php \Drupal\Tests\thunder\FunctionalJavascript\ThunderFormFieldTestTrait::setFieldValue()
  6. 6.1.x tests/src/FunctionalJavascript/ThunderFormFieldTestTrait.php \Drupal\Tests\thunder\FunctionalJavascript\ThunderFormFieldTestTrait::setFieldValue()

Set value for defined field name on current page.

Parameters

\Behat\Mink\Element\DocumentElement $page: Current active page.

string $fieldName: Field name.

string|array $value: Value for field.

5 calls to ThunderFormFieldTestTrait::setFieldValue()
ChannelsTagsTest::testChannelsCreation in tests/src/FunctionalJavascript/ChannelsTagsTest.php
Test channel creation, tagging of articles and channel page with articles.
InlineEntityFormTest::testGalleryCollapse in tests/src/FunctionalJavascript/Integration/InlineEntityFormTest.php
Test saving collapsed gallery paragraph.
LiveblogTest::liveblogSetTitle in tests/src/FunctionalJavascript/Integration/LiveblogTest.php
Set the title of a liveblog post.
LiveblogTest::testWithPusher in tests/src/FunctionalJavascript/Integration/LiveblogTest.php
Testing of module integration.
ThunderFormFieldTestTrait::setFieldValues in tests/src/FunctionalJavascript/ThunderFormFieldTestTrait.php
Set fields on current page.

File

tests/src/FunctionalJavascript/ThunderFormFieldTestTrait.php, line 50

Class

ThunderFormFieldTestTrait
Trait for manipulation of form fields.

Namespace

Drupal\Tests\thunder\FunctionalJavascript

Code

public function setFieldValue(DocumentElement $page, $fieldName, $value) {

  // If field is checkbox list, then use custom functionality to set values.
  // TODO needs documentation.
  $checkboxes = $page
    ->findAll('xpath', "//input[@type=\"checkbox\" and starts-with(@name, \"{$fieldName}[\")]");
  if (!empty($checkboxes)) {
    $this
      ->setCheckbox($page, $fieldName, $value);
    return;
  }

  // If field is date/time field, then set value directly to field.
  $dateTimeFields = $page
    ->findAll('xpath', "//input[(@type=\"date\" or @type=\"time\") and @name=\"{$fieldName}\"]");
  if (!empty($dateTimeFields)) {
    $this
      ->setRawFieldValue($fieldName, $value);
    return;
  }

  // Handle specific types of form fields.
  $field = $page
    ->findField($fieldName);
  $fieldTag = $field
    ->getTagName();
  if ($fieldTag === 'textarea') {

    // Clear text first, before setting value for "textarea" field.
    $this
      ->getSession()
      ->evaluateScript("jQuery('[name=\"{$fieldName}\"]').val('');");
  }
  elseif ($fieldTag === 'select') {

    // Handling of dropdown list.
    if (!$page
      ->find('css', "[name=\"{$fieldName}\"][class*='select2-widget']")) {
      $page
        ->selectFieldOption($fieldName, $value, TRUE);
    }
    else {
      foreach ($value as $item) {
        $id = is_array($item) ? $item[0] : "\$ID:{$item}";
        $label = is_array($item) ? $item[1] : $item;
        if (!$field
          ->find('named', [
          'option',
          $id,
        ])) {
          $this
            ->getSession()
            ->evaluateScript("jQuery('[name=\"{$fieldName}\"]').append(new Option('{$label}', '{$id}', false, false)).trigger('change')");
          $page
            ->selectFieldOption($fieldName, $id, TRUE);
        }
        else {
          $page
            ->selectFieldOption($fieldName, $id, TRUE);
        }
      }
    }
    return;
  }
  $this
    ->scrollElementInView('[name="' . $fieldName . '"]');
  $page
    ->fillField($fieldName, $value);
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
}