黄瓜、水豚和硒 - 提交不带按钮的表单
我使用 Cucumber、capybara 和 selenium 驱动程序进行了测试。该测试应填写表格并提交。正常的文本是
Scenario: Fill form
Given I am on the Form page
When I fill in "field1" with "value1"
And I fill in "field2" with "value2"
And I press "OK"
Then I should see "Form submited"
问题是我在表单中没有“确定”按钮 我需要一种方法来执行“form.submit”,而无需单击任何按钮或链接 - 与使用浏览器在表单字段中按 ENTER 时发生的情况相同。
我不知道如何告诉水豚提交表格。我该怎么做呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您可以访问 selenium send_keys< /a> 调用返回事件的方法,例如
You can access the selenium send_keys method to invoke a return event like
一个简单的解决方案:
使用 capybara-envjs 为我工作。也应该与硒一起使用。
A simple solution:
Worked for me with capybara-envjs. Should work with selenium as well.
我只得自己解决这个问题。在 webrat 中我有这样的东西:
我能够在 Capybara 中实现同样的事情:
I just had to solve this problem myself. In webrat I had something like this:
I was able to achieve the same thing with this in Capybara:
使用 capybara Selenium 驱动程序,您可以执行以下操作:
With the capybara Selenium driver you can do something like this:
简而言之:你不能。
某些浏览器根本不允许您在没有提交按钮的情况下提交表单(尤其是 Internet Explorer <= 6)。所以这种形式一开始就是个坏主意。添加一个提交按钮并使用 CSS 将其放置在屏幕之外。
Simply put: you can't.
Some browsers will not allow you to submit a form without a submit button at all (most notably Internet Explorer <= 6). So this kind of form is a bad idea to begin with. Add a submit button and position it off the screen with CSS.
您可能会推出自己的步骤(例如,我使用链接“确定”提交表单),并自己模拟提交功能。
这里是 Rails 3 中删除的 javascript 模拟,以支持“unobtrusive”(强调引号)Javascript。这条线
可能是回答你的问题的线索。完整代码位于此处
You may probably roll your own step (And I submit the form with the link "Ok", for example), and emulate the submit functionality yourself.
Here it is the javascript emulation dropped in Rails 3 to support "unobtrusive" (emphasis on the quotes) Javascript. The line
is probably the clue to answer your problem. The full code is here
我建议您添加一个提交按钮,然后使用 CSS 隐藏它。然后您可以测试表单提交,但仍然获得您想要的用户行为。
I'd recommend you add a submit button, then hide it with CSS. Then you can test the form submission, but still get the user behavior you want.
使用 Webrat,您可以
: 307,RSpec 书
With Webrat you can just:
p. 307, The RSpec Book
display:none 解决方案不适用于使用 selenium 驱动程序的水豚,因为 selenium 抱怨与不可见元素交互。
如果您尝试上述解决方案,您最终可能会看到以下错误消息:
The display:none solution does not work with capybara using selenium driver because selenium complains about interacting with invisible elements.
If you try the above solution you may end up seeing the following error message:
您可以尝试发送换行符:
You can try sending a newline:
这有点hackish,但它满足了需求。我对 Capybara 进行了猴子修补,以支持元素上的
#submit
方法。它并不健壮,因为它天真地从每个输入元素的
name
和value
属性创建 POST 参数。 (就我而言,我所有的元素都是
hidden
类型,所以它工作正常)。This is a bit hackish, but it filled a need. I monkey-patched Capybara to support a
#submit
method on elements.It is not robust because it naively creates the POST parameters from every input elements's
name
andvalue
attributes. (In my case, all my<input>
elements were of typehidden
, so it works fine).试试这个..
Try this..