2016-11-17 96 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应该是测试应用程序时的最后解决方案(罚款如果是抓取站点),因为它们执行用户从未执行过的操作 –