在 Behat Mink 场景中检查单选按钮状态?

Bar*_*cki 1 tdd symfony gherkin behat mink

我需要在输出中查看是否选中了给定的单选按钮。我应该使用什么样的定义?我在谷歌上搜索了很多,但没有找到解决方案(这可能就在我面前,因为有人可能会向我保证)。

Nas*_*sim 5

Mink 提供了一个测试复选框的步骤:

the "form_checkbox" checkbox should be checked
Run Code Online (Sandbox Code Playgroud)

但是对于单选按钮,您需要编写自己的步骤。就像是 :

/**
 * @Then /^Radio button with id "([^"]*)" should be checked$/
 */
public function RadioButtonWithIdShouldBeChecked($sId)
{
    $elementByCss = $this->getSession()->getPage()->find('css', 'input[type="radio"]:checked#'.$sId);
    if (!$elementByCss) {
        throw new Exception('Radio button with id ' . $sId.' is not checked');
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以使用 find() 方法通过 CSS 选择器来定位元素。在这里,我们搜索一个被选中并具有给定 id 的单选按钮。