You are here

public function VarbaseContext::iSelectTheRadioButton in Varbase: The Ultimate Drupal CMS Starter Kit (Bootstrap Ready) 8.5

Same name and namespace in other branches
  1. 8.8 tests/features/bootstrap/VarbaseContext.php \VarbaseContext::iSelectTheRadioButton()
  2. 8.4 tests/features/bootstrap/VarbaseContext.php \VarbaseContext::iSelectTheRadioButton()
  3. 8.6 tests/features/bootstrap/VarbaseContext.php \VarbaseContext::iSelectTheRadioButton()
  4. 8.7 tests/features/bootstrap/VarbaseContext.php \VarbaseContext::iSelectTheRadioButton()
  5. 9.0.x tests/features/bootstrap/VarbaseContext.php \VarbaseContext::iSelectTheRadioButton()

#varbase: To select a radio button.

Example 1: When I select the "Male" radio button

@When /^I select the "([^"]*)" radio button$/

File

tests/features/bootstrap/VarbaseContext.php, line 1294

Class

VarbaseContext
Defines application features from the specific context.

Code

public function iSelectTheRadioButton($labelText) {

  // Find the label by its text, then use that to get the radio item's ID
  $radioId = null;

  /** @var $label NodeElement */
  foreach ($this
    ->getSession()
    ->getPage()
    ->findAll('css', 'label') as $label) {
    if ($labelText === $label
      ->getText()) {
      if ($label
        ->hasAttribute('for')) {
        $radioId = $label
          ->getAttribute('for');
        break;
      }
      else {
        throw new \Exception("Radio button's label needs the 'for' attribute to be set");
      }
    }
  }
  if (!$radioId) {
    throw new \InvalidArgumentException("Label '{$labelText}' not found.");
  }

  // Now use the ID to retrieve the button and click it

  /** @var NodeElement $radioButton */
  $radioButton = $this
    ->getSession()
    ->getPage()
    ->find('css', "#{$radioId}");
  if (!$radioButton) {
    throw new \Exception("{$labelText} radio button not found.");
  }
  $this
    ->getSession()
    ->getPage()
    ->fillField($radioId, $radioButton
    ->getAttribute('value'));
}