我需要在输出中看到给定的单选按钮是否被选中。我应该使用什么样的定义?我已经搜索了很多,并没有找到解决方案(这可能是在我面前,因为有人可能会向我保证)。检查Behat Mink场景中的单选按钮状态?
回答
水貂提供了一个步骤,测试复选框:
the "form_checkbox" checkbox should be checked
但对于单选按钮,你需要编写自己的一步。例如:
/**
* @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');
}
}
您可以使用find()方法使用CSS选择器来定位元素。在这里,我们搜索一个选中的单选按钮,并使用给定的ID。
您可以编写自己的步骤定义。例如,这是我做过什么:
/**
* @When /^I check the "([^"]*)" radio button$/
*/
public function iCheckTheRadioButton($labelText)
{
foreach ($this->getMainContext()->getSession()->getPage()->findAll('css', 'label') as $label) {
if ($labelText === $label->getText() && $label->has('css', 'input[type="radio"]')) {
$this->getMainContext()->fillField($label->find('css', 'input[type="radio"]')->getAttribute('name'), $label->find('css', 'input[type="radio"]')->getAttribute('value'));
return;
}
}
throw new \Exception('Radio button not found');
}
我知道这是一个老问题,但我真的没有找到一个很好的答案,它同时在堆栈溢出或谷歌搜索,所以我在这里发布我的解决方案。它可能有助于某人。
http://blog.richardknop.com/2013/04/select-a-radio-button-with-mink-behat/
此定义为我的作品:
And the "radio-buton-name" field should contain "radio-button-value"
这应该是公认的答案,因为它使用Mink附带的步骤定义。 –
由贝哈特/水貂扩展本身提供的方法非常有效:
@versionBehat v3.0.14
@ see\Behat\MinkExtension\Context\MinkContext
/**
* Checks checkbox with specified id|name|label|value.
*
* @When /^(?:|I)check "(?P<option>(?:[^"]|\\")*)"$/
*/
public function checkOption($option)
{
$option = $this->fixStepArgument($option);
$this->getSession()->getPage()->checkField($option);
}
只是测试,它与(如在刚刚PHPDoc的方法定义上面提到的)标签也未尝不可。
请仔细阅读这个问题:它不是关于如何设置状态,而是关于如何断言状态。 –
我认为这值得一提,我添加了一个用于使用标签找到它的复选框。
/**
* @Then the checkbox for :checkboxLabel should be selected
*/
public function theCheckboxForShouldBeSelected($checkboxLabel)
{
$elementByCss = $this->getSession()->getPage()->find('css', 'label:contains("'.$checkboxLabel.'") input:checked');
if (!$elementByCss) {
throw new Exception('Checkbox with label ' . $checkboxLabel.' is not checked');
}
}
- 1. 用Behat检查单选按钮
- 2. 检查单选按钮状态WINAPI
- 3. 单选按钮选中状态的jQuery
- 4. 如何检测jquery中单选按钮的检查状态?
- 5. 如何检查Flex 3中单选按钮的状态?
- 6. 获取单选按钮的检查状态
- 7. 检查单选按钮android的状态并计算答案
- 8. 如何检查oracle形式的单选按钮状态
- 9. jQuery - 检查单选按钮的当前状态并隐藏div
- 10. Behat + Mink + Selenium安装
- 11. 检查动态列表中检查按钮的状态
- 12. 如何检查状态改变单选按钮android?
- 13. 连续检查单选按钮状态[WxPython]
- 14. 使用jQuery Mobile + knockout.js初始“已检查”单选按钮状态
- 15. Behat/Mink无法模拟点击页脚上的按钮
- 16. 单选按钮的jquery中获取单选按钮的选中状态ID
- 17. 检查并更改单选按钮组中的单选按钮
- 18. WatiN:如何检查单选按钮组中的单选按钮
- 19. 检查网页中Facebook状态按钮的状态
- 20. jQuery - 单选按钮状态变化
- 21. PIC部件 - 检查按钮的状态
- 22. 保存单选按钮的状态
- 23. 关于单选按钮的选中状态
- 24. 按钮选中状态Android?
- 25. iPhone按钮选中状态
- 26. 检查单选按钮(笨)
- 27. 单选按钮,检查值
- 28. 从检查单选按钮
- 29. Jquery单选按钮检查
- 30. 检查单选按钮
[贝哈特小抄及水貂小抄(http://blog.lepine.pro/images/2012-03-behat-cheat-sheet-en.pdf)对你有用的信息。 – BentCoder