2012-11-22 38 views
0

我需要在输出中看到给定的单选按钮是否被选中。我应该使用什么样的定义?我已经搜索了很多,并没有找到解决方案(这可能是在我面前,因为有人可能会向我保证)。检查Behat Mink场景中的单选按钮状态?

+0

[贝哈特小抄及水貂小抄(http://blog.lepine.pro/images/2012-03-behat-c​​heat-sheet-en.pdf)对你有用的信息。 – BentCoder

回答

4

水貂提供了一个步骤,测试复选框:

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。

0

您可以编写自己的步骤定义。例如,这是我做过什么:

/** 
* @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/

2

此定义为我的作品:

And the "radio-buton-name" field should contain "radio-button-value" 
+0

这应该是公认的答案,因为它使用Mink附带的步骤定义。 –

0

由贝哈特/水貂扩展本身提供的方法非常有效:

@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的方法定义上面提到的)标签也未尝不可。

+0

请仔细阅读这个问题:它不是关于如何设置状态,而是关于如何断言状态。 –

0

我认为这值得一提,我添加了一个用于使用标签找到它的复选框。

/** 
* @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'); 
    } 
}