用Behat检查单选按钮

use*_*744 2 button radio behat mink

我陷入了这个问题1小时.实际上,检查带有Mink的单选按钮应该不难.怎么做,我已经找到了.但它只有在单选按钮位于表单标签内时才有效.如果我有

<table>
    <tr>
        <td>
            <input id="payone" type="radio" name="payment[method]" value="payone_today">
            <label for="payone">Vorkasse</label>
        </td>
    </tr>
</table>
Run Code Online (Sandbox Code Playgroud)

没有办法检查单选按钮,或者我找不到它.有任何想法吗?

小智 11

我发现'当我选择'值"来自"名称"'形式用于选择单选按钮.也许你的代码可以使用:当我从"payment [method]"中选择"payone_today"时.


小智 5

我有一个类似的问题,并使用了类似的解决方案 - 但我认为可能有用的确切要求有一个扭曲.

如果您有两个带有相同标签文本的单选按钮,会发生什么?当你有多个单选按钮组并且上面的解决方案只能在DOM中找到带有该标签的第一个按钮时,这是完全可能的 - 你可能希望它是第二个或第三个.

提供每个单选按钮组都在容器内,然后您可以从该容器开始,而不是按照整个DOM的方式工作.

我对原始问题和稍微扩展版本的解决方案如下所示.

/**
 * @When /^I check the "([^"]*)" radio button$/
 */
public function iCheckTheRadioButton($labelText) {
    $page = $this->getMainContext()->getSession()->getPage();
    foreach ($page->findAll('css', 'label') as $label) {
        if ( $labelText === $label->getText() ) {
            $radioButton = $page->find('css', '#'.$label->getAttribute('for'));
            $radioButton->click();
            return;
        }
    }
    throw new \Exception("Radio button with label {$labelText} not found");
}

/**
 * @When /^I check the "([^"]*)" radio button in "([^"]*)" button group$/
 */
public function iCheckButtonInGroup($labelText, $groupSelector){
    $page = $this->getMainContext()->getSession()->getPage();
    $group = $page->find('css',$groupSelector);
    foreach ($group->findAll('css', 'label') as $label) {
        if ( $labelText === $label->getText() ) {
            $radioButton = $page->find('css', '#'.$label->getAttribute('for'));
            $radioButton->click();
            return;
        }
    }
    throw new \Exception("Radio button with label {$labelText} not found in group {$groupSelector}");
}
Run Code Online (Sandbox Code Playgroud)