Capybara - 提交不带按钮的表单

发布于 2024-12-25 00:24:24 字数 730 浏览 1 评论 0原文

我正在尝试仅使用 Capybara 和 Rspec 提交一个没有按钮的表单(没有 Cucumber 或 Selenium,我知道已经有一个关于此的问题)。

我发现有一个要点是添加一种方法来提交没有按钮的表单:

module SubmitRackTestFormWithoutButton
  def submit_form!
    Capybara::RackTest::Form.new(driver, form).submit({})
  end
end
Capybara::RackTest::Node.send :include, SubmitRackTestFormWithoutButton

https://gist.github.com/989533,但我还没有让它工作,我对它留下了评论:

我得到未定义的方法“submit_form!”对于#Capybara::Node::Element:... 实际上是通过“Capybara::RackTest::Node.send :include, SubmitRackTestFormWithoutButton”方法submit_form!被添加到 节点(不是元素),但 find 返回一个元素

您是否有一些想法来解决该要点,或其他一些解决方案来提交没有按钮的表单?

谢谢

I am trying to submit a form without button using just Capybara and Rspec (no Cucumber or Selenium, I know there is already a question about that).

I've seen there is a gist to add a method to submit a form without button:

module SubmitRackTestFormWithoutButton
  def submit_form!
    Capybara::RackTest::Form.new(driver, form).submit({})
  end
end
Capybara::RackTest::Node.send :include, SubmitRackTestFormWithoutButton

https://gist.github.com/989533, but I've not gotten it to work and I left a comment on it:

I get undefined method `submit_form!' for #Capybara::Node::Element:...
actually by "Capybara::RackTest::Node.send :include,
SubmitRackTestFormWithoutButton" the method submit_form! is added to
the Node (not to the Element), but find return an Element

Do you have some idea to work out that gist, or some other solution to submit a form without button ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

向日葵 2025-01-01 00:24:24

所有的生产代码都应该是可测试的,因此,如果您添加仅由测试使用的代码,那么测试将毫无意义......

尝试这样做:

page.execute_script("$('form#your-form').submit()")

All your production code should be testable, so if you add a code that is only used by the test than the test will make no sense...

Try to do this instead:

page.execute_script("$('form#your-form').submit()")
A君 2025-01-01 00:24:24

您可以通过在输入中按 Enter 键来完成此操作

find('form input').native.send_keys :enter

You can do this by pressing enter within the input

find('form input').native.send_keys :enter
笔落惊风雨 2025-01-01 00:24:24

这是一个简单的解决方案,不需要 capybary-webkit、qt、lmnop 或其他任何东西。

不需要提交按钮。人们说你需要它,但无论如何。

只需猴子补丁一两个类

# /spec/support/capybara.rb
  class Capybara::Session
    def submit(element)
      Capybara::RackTest::Form.new(driver, element.native).submit({})
    end
  end

然后你可以做类似的事情

require 'support/capybara'

before do
  create :lead
  create :user, :different_email
end

it 'Searchable' do
  visit users_path
  page.should have_content 'Slicer'
  page.should have_content 'Dicer'

  fill_in 'Search', with: 'dice'

  form = find '#search-form' # find the form
  page.submit form           # use the new .submit method, pass form as the argument

  page.should have_content 'Dicer'
  page.should_not have_content 'Slicer'
end

它有点像 jacob 在这里的答案,但对于他来说你有在测试中间定义它。

对于此解决方案,您可以在 /support 目录中的某个文件中或在该规范的开头等处定义它。它可以减少测试中的混乱。

Here's a simple solution that doesn't require capybary-webkit, qt, lmnop or whatever.

Does not require a submit button. People say you need it but whatever.

Just monkeypatch a class or two

# /spec/support/capybara.rb
  class Capybara::Session
    def submit(element)
      Capybara::RackTest::Form.new(driver, element.native).submit({})
    end
  end

Then you can do something like

require 'support/capybara'

before do
  create :lead
  create :user, :different_email
end

it 'Searchable' do
  visit users_path
  page.should have_content 'Slicer'
  page.should have_content 'Dicer'

  fill_in 'Search', with: 'dice'

  form = find '#search-form' # find the form
  page.submit form           # use the new .submit method, pass form as the argument

  page.should have_content 'Dicer'
  page.should_not have_content 'Slicer'
end

It's kinda like jacob's answer on here but for his you have to define that in the middle of the test.

For this solution, you can define this in some file in the /support directory or at the beginning of that one spec, etc. It reduces the clutter in the test.

謌踐踏愛綪 2025-01-01 00:24:24

我让它在水豚 1.1.2 中工作:

  form = page.find("form")
  class << form
    def submit!
      Capybara::RackTest::Form.new(driver, native).submit({})
    end
  end
  form.submit!

看起来这里描述了类似的解决方案: http://minimul.com/submitting-a-form-without-a-button-using-capybara.html

I got this to work in capybara 1.1.2 with:

  form = page.find("form")
  class << form
    def submit!
      Capybara::RackTest::Form.new(driver, native).submit({})
    end
  end
  form.submit!

and it looks like a similar solution is described here: http://minimul.com/submitting-a-form-without-a-button-using-capybara.html

失与倦" 2025-01-01 00:24:24

虽然使用水豚可以实现您想要的效果,但更简单、更实用的解决方案是在表单上放置一个提交按钮。

表单上没有理由没有按钮,没有表单的可访问性很差,没有 GUI 或使用屏幕阅读器的用户将无法提交表单。

如果您不希望表单按钮可见,我可以建议使用一些 CSS 将其隐藏:

<input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;">

Although it's possible to achieve what you want using capybara, the easier and more practical solution is to put a submit button on the form.

There is no reason to not have a button on the form, it's bad accessibility to not have a form and users that do not have a GUI or are using screen readers will have trouble submitting the form otherwise.

If you don't want the form button to be visible, can I suggest using some CSS to make it hidden:

<input type="submit" style="position: absolute; left: -9999px; width: 1px; height: 1px;">
玩物 2025-01-01 00:24:24

现在你应该使用 click_on

click_on 'Sign up'

Now You should use click_on

click_on 'Sign up'
清风挽心 2025-01-01 00:24:24

最好的解决方案是使用 JS 驱动程序测试(如上面的评论所述):

page.execute_script(%q{document.querySelector("form[action='/admin/opt_outs']").submit()})

如果没有 JS 驱动程序:

事实上,每个表单都应该有一个按钮,如果该按钮不应该出现在 UI 中,只需使用 display: none 隐藏它,然后改为 click_button 执行以下操作:

find('form input[type=submit]', visible: false)

best solution is by using JS driver test (as mentioned comment above):

page.execute_script(%q{document.querySelector("form[action='/admin/opt_outs']").submit()})

if no JS driver:

Fact is every form should have a button, if the button should not appear in UI just hide it with display: none and then instead click_button do:

find('form input[type=submit]', visible: false)
冷情 2025-01-01 00:24:24

因为我正在寻找同样的东西,并且 @Motine 在一些评论中提到这个问题,我刚刚在这里发现了这种非常好的和简洁的方式(IMO)在使用RackTest驱动程序(没有硒, cuprite、javascript 等):

fill_in 'q', with: "search\n"

技巧就是在字符串末尾添加 "\n",并且如果表单中只有一个输入,则RackTest 驱动程序将 去掉换行符并提交表单(就像按回车键一样)。

As I was looking for the same thing and @Motine mentionned in some comments this issue, I just discovered here this very nice and concise way (IMO) to submit single input forms (like a search field) when using the RackTest driver (no selenium, cuprite, javascript, etc.):

fill_in 'q', with: "search\n"

The trick is simply to add the "\n" at the end of the string and if there's only one input in the form, the RackTest driver will strip the newline and submit the form (as if you hit enter).

孤凫 2025-01-01 00:24:24

使用 :rack_test 驱动程序时,上述解决方案均不适用于当前的 capybara 3.40.0。

阅读源代码后,我想出了这样一句话来提交表格:

Capybara::RackTest::Form.new(page.driver.browser, page.find("form").native).submit(nil)

None of the above solutions work with the current capybara 3.40.0 when using the :rack_test driver.

After reading the source I came up with this one-liner to submit a form:

Capybara::RackTest::Form.new(page.driver.browser, page.find("form").native).submit(nil)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文