function BrowserTestBase::getOptions

Same name in other branches
  1. 9 core/tests/Drupal/Tests/BrowserTestBase.php \Drupal\Tests\BrowserTestBase::getOptions()
  2. 8.9.x core/tests/Drupal/Tests/BrowserTestBase.php \Drupal\Tests\BrowserTestBase::getOptions()
  3. 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 526

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

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.