Symfony 检查Behat Mink场景中的单选按钮状态?
我需要查看输出中是否选中了给定的单选按钮。我应该使用什么样的定义?我在谷歌上搜索了很多,但没有找到一个解决方案(这可能就在我面前,有人可能会向我保证) Mink提供了一个测试复选框的步骤:Symfony 检查Behat Mink场景中的单选按钮状态?,symfony,tdd,behat,gherkin,mink,Symfony,Tdd,Behat,Gherkin,Mink,我需要查看输出中是否选中了给定的单选按钮。我应该使用什么样的定义?我在谷歌上搜索了很多,但没有找到一个解决方案(这可能就在我面前,有人可能会向我保证) Mink提供了一个测试复选框的步骤: the "form_checkbox" checkbox should be checked 但对于单选按钮,您需要编写自己的步骤。比如: /** * @Then /^Radio button with id "([^"]*)" should be checked$/ */ public functio
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的单选按钮。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');
}
我知道这是一个老问题,但我在Stack Overflow和Google上搜索时并没有找到一个好的答案,所以我在这里发布了我的解决方案。这可能对某人有帮助
您可以编写自己的步骤定义。例如,我就是这样做的:
/**
* @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');
}
我知道这是一个老问题,但我在Stack Overflow和Google上搜索时并没有找到一个好的答案,所以我在这里发布了我的解决方案。这可能对某人有帮助
这个定义适用于我:
And the "radio-buton-name" field should contain "radio-button-value"
这个定义适用于我:
And the "radio-buton-name" field should contain "radio-button-value"
Behat/Mink扩展本身提供的方法非常有效: @version
Behat v3.0.14
@请参见\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);
}
/**
*选中具有指定id | name | label |值的复选框。
*
*@When/^(?:| I)勾选“(?P(?:[^“]\\”)”$/
*/
公共功能检查选项($option)
{
$option=$this->fixStepArgument($option);
$this->getSession()->getPage()->checkField($option);
}
刚刚测试过,它也适用于标签(如方法定义上方的phpdoc中所述)。Behat/Mink扩展本身提供的方法运行良好: @version
Behat v3.0.14
@请参见\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);
}
/**
*选中具有指定id | name | label |值的复选框。
*
*@When/^(?:| I)勾选“(?P(?:[^“]\\”)”$/
*/
公共功能检查选项($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');
}
}
我认为这是值得一提的,我添加了一个复选框,使用标签来查找它
/**
* @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');
}
}
有有用的信息给你。为您提供有用的信息。请仔细阅读问题:它不是关于如何设置状态,而是关于如何断言状态。请仔细阅读问题:它不是关于如何设置状态,它是关于如何断言状态的。这应该是可接受的答案,因为它使用Mink附带的步骤定义。这应该是可接受的答案,因为它使用Mink附带的步骤定义。