2013-09-24 41 views
2

我在這個問題上停留了1個小時。實際上,用Mink檢查一個單選按鈕應該不難。怎麼做,我已經找到了。但它只在單選按鈕位於表單標籤內時才起作用。如果我有用Behat檢查單選按鈕

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

沒有辦法檢查單選按鈕,或者我找不到它。有任何想法嗎?

回答

6

我發現「當我從」名稱「」表單中選擇「值」表單可以用於選擇單選按鈕。也許你的代碼適用於:當我從「付款[方法]」中選擇「payone_today」時。

+0

這個答案適用於我,並且比爲已存在的東西添加整個額外方法簡單得多。 – DanielM

+1

也爲我工作,但如果輸入元素與「顯示:無」,那麼它不工作。 – Reshma

1

如果你的單選按鈕有<label>,而標籤使用for屬性,以確定其目標,您可以使用此代碼:

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

我適應了這個在納西姆的答案鏈接到博客文章 - 那裏的代碼依賴於你的單選按鈕<input>被包裹在一個標籤中,這在我的情況下是不可能的(很好,不容易)。

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}"); 
} 
+0

我正在嘗試組中的第二個示例檢查按鈕。我將parent id作爲groupselector。但它的拋出錯誤「調用成員函數FindAll」 – Reshma

+0

我猜你的組選擇器不起作用。你能發佈更多關於選擇器的細節,標記是什麼樣的。 – PCaligari

+0

這很有幫助,因爲我有幾個頁面,每個頁面上有多個「是」和「否」集。 – zkent

0

我發現最簡單的代碼解決方案就像這樣。

/** 
* @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; 
} 
+0

在無線電設備上調用check()是個問題。這段代碼會拋出錯誤'不能打勾,因爲它不是一個複選框(收音機)' – Greg

+0

感謝您的回答,但這是我得到的'你不能勾選'條件「,因爲它不是一個複選框(收音機)。 –