2013-10-15 68 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

非常感谢你为我节省了大量的时间 –