2

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

4

5 回答 5

5

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 的单选按钮。

于 2012-11-23T11:22:48.343 回答
4

这个定义对我有用:

And the "radio-buton-name" field should contain "radio-button-value"
于 2013-08-02T15:36:47.413 回答
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');
 }

我知道这是一个老问题,但我在 Stack Overflow 和 Google 上搜索时并没有真正找到一个好的答案,所以我在这里发布了我的解决方案。它可能会帮助某人。

http://blog.richardknop.com/2013/04/select-a-radio-button-with-mink-behat/

于 2013-04-30T14:15:41.377 回答
0

Behat/Mink 扩展本身提供的方法运行良好:

@版本 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);
}

刚刚测试过,它也适用于标签(如方法定义上方的 phpdoc 中所述)。

于 2014-10-16T13:07:24.150 回答
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');
    }
}
于 2015-07-15T21:08:57.140 回答