2016-11-17 121 views
0

我無法獲得Capybara查找複選框元素。水豚(poltergeist)無法找到複選框

我已經嘗試了所有的常用方法:

find_field 
check 
find 
click_on 

這是因爲如果該元素是不存在的。如果我選擇父元素,看它的innerHTML複選框包括:

(byebug) all(:css, '.checkbox.form-group').last['innerHTML'] 
\n <input type=\"checkbox\" id=\"google_agreed_to_terms_at\" value=\"1\" required=\"true\" ng-model=\"agreed_to_terms\" required-notification=\"Please agree to the terms and conditions\" class=\"ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required\" bs-validation=\"\">\ 

,但選擇的子元素它不存​​在:

(byebug) all(:css, '.checkbox.form-group').last.all :xpath, './*' 
[#<Capybara::Node::Element tag="label" 
path="//HTML[1]/BODY[1]/DIV[2]/DIV[1]/DIV[2]/FORM[1]/DIV[1]/LABEL[1]">] 

我覺得我快要瘋掉。

這裏的(從save_and_open_page複製)

<div class="checkbox form-group"> 
    <input type="checkbox" id="agreed_to_terms_at" value="1" required="true" ng-model="agreed_to_terms" required-notification="Please agree to the terms and conditions" class="ng-pristine ng-untouched ng-empty ng-invalid ng-invalid-required" bs-validation=""> 
    <label class="label-light" for="agreed_to_terms_at"> 
    I have read and agree to the terms</label> 
</div> 

我想,也許是鋼軌產生不符合標準的HTML略微相關的代碼,所以我去了手寫的複選框,但它並沒有幫助。

這是怎麼回事嗎?

回答

2

複選框被隱藏,以允許在所有瀏覽器一致的造型工作圍繞它。既然你有一個標籤元素正確關聯你可以告訴檢查單擊標籤,而不是實際的複選框

check('agreed_to_terms_at', allow_label_click: true) 

將點擊複選框,如果可見,如果沒有它會點擊標籤。如果你想讓它僅單擊標籤,你可以做

find(:label, 'I have read and agree').click 

或者

find(:label, for:'agreed_to_terms_at').click 
0

由於CSS樣式,複選框元素被隱藏起來,標籤用於顯示它。所以元素被水豚的默認行爲忽略,忽略隱藏的元素。

我使用

find('#agreed_to_terms_at', :visible => false).trigger('click') 
+0

你就不能添加在您的spec_helper:'Capybara.ignore_hidden_​​elements = FALSE'? (爲了避免在你需要的時候使用你的解決方案) – fabersky

+0

我曾考慮過這樣做,因爲這不是我第一次發現它很麻煩,但我明白水豚的價值,要求你說「是的,我知道用戶看不到這一點,但我仍然想改變它「,以避免隱藏元素被規範意外操縱提供錯誤傳遞的用例。 – ChristopherJ

+1

使用觸發器('click')或將Capybara.ignore_hidden_​​elements設置爲false應該是測試應用程序時的最後解決方案(罰款如果是抓取站點),因爲它們執行用戶從未執行過的操作 –