简体   繁体   English

使用Capybara检查隐藏的复选框

[英]Check hidden checkbox using Capybara

I've got a checkbox that I want to check. 我有一个我想检查的复选框。 This button has a confirm alert associated and some other stuff. 此按钮具有关联的confirm alert和其他一些内容。

在此输入图像描述

<label>
  <div class="check label-checked">
    <span>Confirm</span>
    <i class="fa fa-square-o"></i>
    <input class="confirm-reservation-js" hidden="" name="actions_to[56]" type="checkbox" value="confirm">
  </div>
</label>

# js.erb

$(__s.bookings + ".confirm-reservation-js").click(function() {
    var input, form;
    if (confirm( "<%= I18n.t('.reservations.alert.action') %>" )) {
      form  = $(__s.bookings + '[id^=edit_booking_]');
      input = $("<input>")
                .attr("type", "hidden")
                .attr("name", "confirm").val("true");
      form.append($(input));
      form.submit();
    }
  });

I cannot check this button with Capybara. 我不能用Capybara检查这个按钮。 I was trying with: 我正在尝试:

1- find(locator, visible: false).click 1- find(locator, visible: false).click

2- find(locator).trigger('click') 2- find(locator).trigger('click')

Where locator is the path to my input. locator是我输入的路径。

Additional 额外

# Gemfile
gem 'capybara', '2.0.2'
gem 'capybara-webkit', '~> 1.1.0'

# Test file
it "..." , js: true do

Somebody can help me to check this checkbox please? 有人可以帮我看看这个复选框吗?

Thanks. 谢谢。

Capybara has an option (Capybara.ignore_hidden_elements) to configure the default query behavior of finding nodes within the DOM. Capybara有一个选项(Capybara.ignore_hidden_​​elements)来配置在DOM中查找节点的默认查询行为。 It's default value is false. 它的默认值为false。 That means that your css or xpath queries will find all nodes in the document regardless of their visibility on the page. 这意味着您的css或xpath查询将查找文档中的所有节点,无论它们在页面上的可见性如何。 You can overwrite this behavior for an individual query by passing the :visible => true|false option. 您可以通过传递:visible => true | false选项来覆盖单个查询的此行为。

Change the default behavior of capybara ie 更改capybara的默认行为即

Capybara.ignore_hidden_elements = true

You can also follow the link : http://makandracards.com/makandra/7617-change-how-capybara-sees-or-ignores-hidden-elements :) 您也可以点击链接: http//makandracards.com/makandra/7617-change-how-capybara-sees-or-ignores-hidden-elements :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM