2013-10-15 67 views
2

我很難讓黃瓜「選擇」一個單選按鈕,希望有人可以給我一個理智的檢查。沒有引用大量的HTML垃圾,下面是相關的部分(我從print.html收集到的)。它位於由按鈕激活的模式div內。我可以「點擊」該按鈕,看到模態窗口出現(我在Selenium中將它作爲@javascript場景運行)。不能得到黃瓜/水豚找到單選按鈕

<div class="modal-body pagination-centered"> 
    <img src="/assets/payment.png" alt="Payment" /> 
    <form novalidate="novalidate" method="post" id="edit_cart_1" class="simple_form edit_cart" action="/carts/complete" accept-charset="UTF-8"> 
     <div style="margin:0;padding:0;display:inline"> 
      <input type="hidden" value="✓" name="utf8" /> 
      <input type="hidden" value="put" name="_method" /> 
     </div> 
     <div class="control-group hidden cart_property_id"> 
      <div class="controls"> 
       <input type="hidden" name="cart[property_id]" id="cart_property_id" class="hidden" /> 
      </div> 
     </div> 
     <div id="payment_fat_buttons" class="fat-buttons"> 
      <div class="vertical-button-wrapper"> 
       <input type="radio" value="cash" name="cart[payment_type]" id="cart_payment_type_cash_pay" data-property-id="1" /> 
       <label for="cart_payment_type_cash_pay">Cash</label> 
      </div> 
      <div class="vertical-button-wrapper"> 
       <input type="radio" value="credit" name="cart[payment_type]" id="cart_payment_type_credit_pay" data-property-id="1" /> 
       <label for="cart_payment_type_credit_pay">Credit</label> 
      </div> 
     </div> 
     <div style="display: none;" id="cart_room_number_area_pay"> 
      <div class="control-group string optional cart_room_number"> 
       <label for="cart_room_number_pay" class="string optional control-label">Room number</label> 
       <div class="controls"> 
        <input type="text" value="" size="50" name="cart[room_number]" id="cart_room_number_pay" class="string optional" /> 
       </div> 
      </div> 
     </div> 
     <input type="checkbox" value="1" style="display: none;" name="receipt" id="receipt" /> 
     <div class="sell-modal-footer"> 
      <input type="submit" value="Complete With Receipt" name="commit" id="cart_complete_with_receipt" data_disable_with="Processing..." class="btn btn-danger" /> 
      <input type="submit" value="Complete Sale" name="commit" data_disable_with="Processing..." class="btn btn-danger" /> 
     </div> 
    </form> 
</div> 

我已經嘗試了許多不同的等價方法,我可以想到。顯然大多數只是標籤,或ID,如:

choose 'cart_payment_type_cash_pay' 
choose 'Cash' 

剛剛給我的錯誤:

​​

我認爲它可能有一些做的模態對話框,能見度等。但我公司推出的ID #payment_fat_buttons只是用於測試,當我看它是這樣的:

find('#payment_fat_buttons').choose('Cash') 

它發現DIV OK,但仍然不是單選按鈕。我也試圖讓在它:整個頁面上的XPath,喜歡的範圍內:

within(:xpath, "//div[@id='payment_methods']") do 
    find(:xpath, ".//input[@id='cart_payment_type_cash_pay']").choose 
end 

這就像它也可以找到最外層的DIV,但不是單選按鈕 - 我得到的錯誤:

Unable to find xpath ".//input[@id='cart_payment_type_cash_pay']" (Capybara::ElementNotFound) 

一般,好像我能找到的與周圍的單選按鈕任意元素:只是沒有單選按鈕XPath或CSS表達式。我也可以推送表單上的提交按鈕,沒有任何問題。我嘗試刪除數據屬性作爲測試 - 沒有區別。任何幫助將不勝感激。這讓我感到瘋狂,因爲它看起來很簡單,但我卻無處可去。我需要爲情景的一大部分選擇,所以如果我無法弄清楚的話,我將不得不訴諸於一些狡猾和可怕的事情。提前感謝...

相關版本從Gemfile.lock的:

rails (3.2.13) 
cucumber (1.3.8) 
gherkin (2.12.2) 
cucumber-rails (1.4.0) 
capybara (2.1.0) 
selenium-webdriver (2.35.1) 
+1

這就是我用水豚卡住時所做的事情:在失敗期望之前添加一個'binding.pry'。在會話中執行'current_url',在該URL處打開瀏覽器,檢查瀏覽器控制檯中發生了什麼。 – phoet

+0

這很整齊。它似乎給了我從print.html獲得的同樣的東西。所以仍然難倒。但我把這一點加到了我的伎倆中。謝謝。 – Steve

+0

我當時運行的是Firefox 18,因爲在20年代初期,FF和FF版本都出現了一些問題。剛剛更新到FF 25,現在它可以正常工作 - 但只要找到這些特定元素就沒有改變。 – Steve

回答

7

終於想通這一個。水豚沒有找到單選按鈕,因爲埋在我的風格深處是一些CSS,它爲了改變外觀而隱藏起來。一旦我意識到,我想通了,我可以邊一步只是做了點擊標籤上,而不是尋找單選按鈕的整個問題:

find(:xpath, "//label[@for='the_radio_button_id']").click 

我不知道這是可能得到的單選按鈕 - 但無論如何,它解決了如何點擊由於造型或其他問題而無法找到Capybara的單選按鈕的問題。希望能幫助別人。

+0

整蠱!我被同樣的事情抓住了。如果Capybara能夠檢測到某個元素因爲隱藏而無法找到,那將是可愛的。 – LisaD

+0

非常感謝你爲我節省了大量的時間 –