脳みそスワップアウト

揮発性なもので。おもにPHPのこととか。

behatでラジオボタン選択

Minkにはどういうわけかラジオボタンのタスク(?)がない。
(behat-3.0.15, mink-1.7.0)

自分で作る必要がある。
作ってみた。英語は怪しい。

<?php
class FeatureContext extends \Behat\MinkExtension\Context\RawMinkContext {
    /**
     * ラジオボタンを選択する
     * @param string $name
     * @param string $val
     * @throws \Behat\Mink\Exception\ElementNotFoundException
     * @Given /^"([^"]*)" ラジオボタンの "([^"]*)" を選択$/
     */
    public function iSelectTheRadioButton($name, $val) {
        $radio = $this->getSession()->getPage()->find('css', 'input[type="radio"][name="'.$name.'"]');
        if (null === $radio) {
            throw new \Behat\Mink\Exception\ElementNotFoundException($this->getSession(), 'radio', 'name', $name);
        }
        $radio->setValue($val);
    }

    /**
     * ラジオボタンが選択されていること
     * @param string $name
     * @param string $val
     * @throws \Behat\Mink\Exception\ElementNotFoundException
     * @throws \Exception
     * @Given /^"([^"]*)" ラジオボタンが "([^"]*)" であること$/
     */
    public function radioButtonShouldBeChecked($name, $val) {
        $val = strval($val);
        $radio = $this->getSession()->getPage()->find('css', 'input[type="radio"][name="'.$name.'"]');
        if (null === $radio) {
            throw new \Behat\Mink\Exception\ElementNotFoundException($this->getSession(), 'radio', 'name', $name);
        }
        if($val !== $radio->getValue()) {
            throw new Exception('Radio button with name ' . $name.' and value ' . $val. ' is not checked');
        }
    }
}