Capybara - 提交不带按钮的表单
我正在尝试仅使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
所有的生产代码都应该是可测试的,因此,如果您添加仅由测试使用的代码,那么测试将毫无意义......
尝试这样做:
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:
您可以通过在输入中按 Enter 键来完成此操作
You can do this by pressing enter within the input
这是一个简单的解决方案,不需要 capybary-webkit、qt、lmnop 或其他任何东西。
不需要提交按钮。人们说你需要它,但无论如何。
只需猴子补丁一两个类
然后你可以做类似的事情
它有点像 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
Then you can do something like
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.
我让它在水豚 1.1.2 中工作:
看起来这里描述了类似的解决方案: http://minimul.com/submitting-a-form-without-a-button-using-capybara.html
I got this to work in capybara 1.1.2 with:
and it looks like a similar solution is described here: http://minimul.com/submitting-a-form-without-a-button-using-capybara.html
虽然使用水豚可以实现您想要的效果,但更简单、更实用的解决方案是在表单上放置一个提交按钮。
表单上没有理由没有按钮,没有表单的可访问性很差,没有 GUI 或使用屏幕阅读器的用户将无法提交表单。
如果您不希望表单按钮可见,我可以建议使用一些 CSS 将其隐藏:
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:
现在你应该使用 click_on
Now You should use click_on
最好的解决方案是使用 JS 驱动程序测试(如上面的评论所述):
如果没有 JS 驱动程序:
事实上,每个表单都应该有一个按钮,如果该按钮不应该出现在 UI 中,只需使用
display: none
隐藏它,然后改为click_button
执行以下操作:best solution is by using JS driver test (as mentioned comment above):
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 insteadclick_button
do:因为我正在寻找同样的东西,并且 @Motine 在一些评论中提到这个问题,我刚刚在这里发现了这种非常好的和简洁的方式(IMO)在使用RackTest驱动程序(没有硒, cuprite、javascript 等):
技巧就是在字符串末尾添加
"\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.):
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).使用 :rack_test 驱动程序时,上述解决方案均不适用于当前的 capybara 3.40.0。
阅读源代码后,我想出了这样一句话来提交表格:
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: