3

我在这个问题上停留了 1 个小时。实际上,用 Mink 检查单选按钮应该不难。怎么做,我已经找到了。但它仅在单选按钮位于表单标签内时才有效。如果我有

<table>
    <tr>
        <td>
            <input id="payone" type="radio" name="payment[method]" value="payone_today">
            <label for="payone">Vorkasse</label>
        </td>
    </tr>
</table>

没有办法检查单选按钮,或者我找不到它。有任何想法吗?

4

4 回答 4

11

我发现“当我从“名称”中选择“值”时表单适用于选择单选按钮。也许您的代码可以使用:当我从“付款[方法]”中选择“payone_today”时。

于 2013-10-20T04:22:46.600 回答
5

我遇到了类似的问题并使用了类似的解决方案 - 但我认为可能有用的确切要求有所不同。

如果您有两个具有相同标签文本的单选按钮会发生什么?当您有多个单选按钮组并且上面的解决方案只会在 DOM 中找到带有该标签的第一个按钮时,这是完全可能的 - 您可能希望它是第二个或第三个。

提供每个单选按钮组都在一个容器内,然后您可以从该容器开始,而不是通过整个 DOM 工作。

我对原始问题和稍微扩展的版本的解决方案都如下所示。

/**
 * @When /^I check the "([^"]*)" radio button$/
 */
public function iCheckTheRadioButton($labelText) {
    $page = $this->getMainContext()->getSession()->getPage();
    foreach ($page->findAll('css', 'label') as $label) {
        if ( $labelText === $label->getText() ) {
            $radioButton = $page->find('css', '#'.$label->getAttribute('for'));
            $radioButton->click();
            return;
        }
    }
    throw new \Exception("Radio button with label {$labelText} not found");
}

/**
 * @When /^I check the "([^"]*)" radio button in "([^"]*)" button group$/
 */
public function iCheckButtonInGroup($labelText, $groupSelector){
    $page = $this->getMainContext()->getSession()->getPage();
    $group = $page->find('css',$groupSelector);
    foreach ($group->findAll('css', 'label') as $label) {
        if ( $labelText === $label->getText() ) {
            $radioButton = $page->find('css', '#'.$label->getAttribute('for'));
            $radioButton->click();
            return;
        }
    }
    throw new \Exception("Radio button with label {$labelText} not found in group {$groupSelector}");
}
于 2014-05-30T09:50:17.520 回答
1

If your radio button has a <label>, and that label uses the for attribute to identify its target, you can use this code:

/**
 * @When /^I select the "([^"]*)" radio button$/
 */
public function iSelectTheRadioButton($labelText)
{
    // Find the label by its text, then use that to get the radio item's ID
    $radioId = null;
    $ctx = $this->getMainContext();

    /** @var $label NodeElement */
    foreach ($ctx->getSession()->getPage()->findAll('css', 'label') as $label) {
        if ($labelText === $label->getText()) {
            if ($label->hasAttribute('for')) {
                $radioId = $label->getAttribute('for');
                break;
            } else {
                throw new \Exception("Radio button's label needs the 'for' attribute to be set");
            }
        }
    }
    if (!$radioId) {
        throw new \InvalidArgumentException("Label '$labelText' not found.");
    }

    // Now use the ID to retrieve the button and click it
    /** @var NodeElement $radioButton */
    $radioButton = $ctx->getSession()->getPage()->find('css', "#$radioId");
    if (!$radioButton) {
        throw new \Exception("$labelText radio button not found.");
    }

    $ctx->fillField($radioId, $radioButton->getAttribute('value'));
}

I adapted this from the blog post linked to in Nassim's answer - the code there relies on your radio button <input> being wrapped in a label, which wasn't possible (well, not easy) in my situation.

于 2013-12-17T16:29:13.657 回答
-1

我发现最简单的代码解决方案是这样的。

/**
 * @Given /^I check the "([^"]*)" radio button with "([^"]*)" value$/
 */
public function iCheckTheRadioButtonWithValue($element, $value)
{
  foreach ($this->getMainContext()->getSession()->getPage()->findAll('css', 'input[type="radio"][name="'. $element .'"]') as $radio) {
    if ($radio->getAttribute('value') == $value) {
      $radio->check();
      return true;
    }
  }
  return false;
}
于 2014-09-05T07:18:42.800 回答