You are here

protected function BrowserTestBase::getOptions in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/BrowserTestBase.php \Drupal\Tests\BrowserTestBase::getOptions()
  2. 10 core/tests/Drupal/Tests/BrowserTestBase.php \Drupal\Tests\BrowserTestBase::getOptions()

Helper function to get the options of select field.

Parameters

\Behat\Mink\Element\NodeElement|string $select: Name, ID, or Label of select field to assert.

\Behat\Mink\Element\Element $container: (optional) Container element to check against. Defaults to current page.

Return value

array Associative array of option keys and values.

File

core/tests/Drupal/Tests/BrowserTestBase.php, line 534

Class

BrowserTestBase
Provides a test case for functional Drupal tests.

Namespace

Drupal\Tests

Code

protected function getOptions($select, Element $container = NULL) {
  if (is_string($select)) {
    $select = $this
      ->assertSession()
      ->selectExists($select, $container);
  }
  $options = [];

  /** @var \Behat\Mink\Element\NodeElement $option */
  foreach ($select
    ->findAll('xpath', '//option') as $option) {
    $label = $option
      ->getText();
    $value = $option
      ->getAttribute('value') ?: $label;
    $options[$value] = $label;
  }
  return $options;
}