2016-09-28 38 views
0

我的HTML/ERB看起來像這樣水豚並不想選擇一個輸入節點

<fieldset class="row notifications"> 
    <legend> 
     <hr class="dash blue inline"> 
     <%= t :my_notifications %> 
    </legend> 

    <label> 
     <%= f.check_box(:subscribed_to_news) %> 
     <span></span> 
     <span class="checkbox-text"><%= t :accepts_to_receive_news %></span> 
     <br> 
    </label> 
</fieldset> 

當調試我的黃瓜測試與水豚,我找到了該通知複選框f.check_box(:subscribed_to_news)這個爛攤子

page.find('.notifications label')['innerHTML'] 
# => "\n\t\t<input name=\"user[subscribed_to_news]\" type=\"hidden\" value=\"0\"><input type=\"checkbox\" value=\"1\" checked=\"checked\" name=\"user[subscribed_to_news]\" id=\"user_subscribed_to_news\">\n\t\t<span></span>\n\t\t<span class=\"checkbox-text\">blahblahblah</span>\n\t\t<br>\n\t" 

但由於某些原因,我無法找到嵌套的投入也不是由ID

page.find('.notifications label input') 
# => Capybara::ElementNotFound Exception: Unable to find css ".notifications label input" 
page.find('.notifications label #user_subscribed_to_news') # => Capybara::ElementNotFound Exception: Unable to find css ".notifications label #user_subscribed_to_news" 

選擇標籤找到他們雖然

page.find('.notifications label') 
# => #<Capybara::Node::Element tag="label" path="//HTML[1]/BODY[1]/DIV[1]/MAIN[1]/SECTION[1]/FORM[1]/FIELDSET[3]/LABEL[1]"> 

我做錯了什麼?我只想檢查該死的複選框:'(

回答

0

這似乎是複選框是通過正常的CSS/XPath的可達...

我把他用一些JavaScript

page.execute_script(%Q{document.querySelector('#{area} input##{selector}').click()}) 
0

最有可能的原因是該複選框實際上被CSS隱藏,然後用圖像替換,以在不同瀏覽器中啓用相同的複選框樣式如果您使用最新的Capybara可以把它單擊它的標籤時,該複選框是通過調用

page.check('user_subscribed_to_news', allow_label_click: true) # you can also set Capybara.automatic_label_click = true to default to this behavior 

,或者隱藏使用的是舊水豚,你會需要自己點擊標籤

page.find(:label, "blahblahblah").click #match on labels text 

page.find(:label, for: 'user_subscribed_to_news').click #match on labels for attribute if set