You are here

protected function BrowserTestBase::getOptions in Drupal 8

Same name and namespace in other branches
  1. 9 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 553

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;
}