You are here

protected function WebformAssertLegacyTrait::assertFieldsByValue in Webform 6.x

Same name and namespace in other branches
  1. 8.5 tests/src/Traits/WebformAssertLegacyTrait.php \Drupal\Tests\webform\Traits\WebformAssertLegacyTrait::assertFieldsByValue()

Asserts that a field exists in the current page with a given Xpath result.

Parameters

\Behat\Mink\Element\NodeElement[] $fields: Xml elements.

string $value: (optional) Value of the field to assert. You may pass in NULL (default) to skip checking the actual value, while still checking that the field exists.

string $message: (optional) A message to display with the assertion. Do not translate messages with t().

2 calls to WebformAssertLegacyTrait::assertFieldsByValue()
WebformAssertLegacyTrait::assertFieldByXPath in tests/src/Traits/WebformAssertLegacyTrait.php
Asserts that a field exists in the current page by the given XPath.
WebformAssertLegacyTrait::assertNoFieldByXPath in tests/src/Traits/WebformAssertLegacyTrait.php
Asserts that a field does not exist or its value does not match, by XPath.

File

tests/src/Traits/WebformAssertLegacyTrait.php, line 555

Class

WebformAssertLegacyTrait
Provides convenience methods for assertions in browser tests.

Namespace

Drupal\Tests\webform\Traits

Code

protected function assertFieldsByValue($fields, $value = NULL, $message = '') {

  // If value specified then check array for match.
  $found = TRUE;
  if (isset($value)) {
    $found = FALSE;
    if ($fields) {
      foreach ($fields as $field) {
        if ($field
          ->getAttribute('type') == 'checkbox') {
          if (is_bool($value)) {
            $found = $field
              ->isChecked() == $value;
          }
          else {
            $found = TRUE;
          }
        }
        elseif ($field
          ->getAttribute('value') == $value) {

          // Input element with correct value.
          $found = TRUE;
        }
        elseif ($field
          ->find('xpath', '//option[@value = ' . (new Escaper())
          ->escapeLiteral($value) . ' and @selected = "selected"]')) {

          // Select element with an option.
          $found = TRUE;
        }
        elseif ($field
          ->getTagName() === 'textarea' && $field
          ->getValue() == $value) {

          // Text area with correct text. Use getValue() here because
          // getText() would remove any newlines in the value.
          $found = TRUE;
        }
        elseif ($field
          ->getTagName() !== 'input' && $field
          ->getText() == $value) {
          $found = TRUE;
        }
      }
    }
  }
  $this
    ->assertTrue($fields && $found, $message);
}