webrat 自动填写表单字段
我正在学习如何用 cucumber/webrat 编写测试。我的测试场景之一设置为测试表单验证(将字段留空)。奇怪的是,我没有使用 fill_in
填写的字段被设置为该字段的 name
属性。仅当我运行黄瓜时才会发生这种情况,使用浏览器时不会发生这种情况。
我使用的步骤很简单:
When /^I submit the form$/ do
# Not filling in the 'Name' field here
fill_in 'Description', :with => 'This is a description'
click_button 'Save'
end
运行使用上述步骤的方案后,我可以看到文本字段“名称”设置为“名称”而不是空。如果我用空格或 nil 填充该字段,情况也是如此:
fill_in 'Name', :with => ''
我正在测试的表单非常简单:
<form action="/item/create" method="post">
<div>
<label for="ItemName">Name</label>
<input type="text" name="name" id="ItemName" />
</div>
<div>
<label for="ItemDescription">Description</label>
<textarea name="description" id="ItemDescription"></textarea>
</div>
<input type="submit" value="Save" />
</form>
知道为什么会发生这种情况吗?
I am learning how to write tests with cucumber/webrat. One of my test scenarios is set to test form validation (leaving field(s) empty). Strangely enough, fields that I do not fill-in with fill_in
are set to the field's name
attribute. This only happens when I run cucumber, when using a browser this does not happen.
The step I'm using is straight forward:
When /^I submit the form$/ do
# Not filling in the 'Name' field here
fill_in 'Description', :with => 'This is a description'
click_button 'Save'
end
After running the scenario that uses the step above, I can see that the text field "Name" is set to "name" instead of being empty. This is also the case if I fill in that field with an empty space or nil
:
fill_in 'Name', :with => ''
The form I'm testing on is simple enough:
<form action="/item/create" method="post">
<div>
<label for="ItemName">Name</label>
<input type="text" name="name" id="ItemName" />
</div>
<div>
<label for="ItemDescription">Description</label>
<textarea name="description" id="ItemDescription"></textarea>
</div>
<input type="submit" value="Save" />
</form>
Any idea why this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我猜您正在将 Webrat 与 Mechanize 适配器一起使用,对吗?如果是这样,我发现自己对同样的问题感到非常沮丧。事实证明,这是 Webrat 将表单字段值传递给 Mechanize 的方式中的一个错误。您可以在此处找到详细信息和补丁:https://webrat.lighthouseapp.com/projects/10503/tickets/384-webrat-does-not-pass-empty-form-fields- Correctly-to-mechanize
或者,如果您不想使用 Webrat 的修补版本,一个不太理想的解决方法是用空格 (' ') 代替 fill_in ,并确保您的应用程序的输入验证在考虑字段是否已填充时修剪或忽略空格正确地。
不幸的是,似乎有很多这样的问题,它们提供的补丁尚未合并到“官方”Webrat 代码库中。大约一个半月前,我给作者发了一封电子邮件,询问他是否仍在维护它,如果没有,请考虑找一个会维护它的人,因为很多人仍在使用它。到目前为止,我还没有收到回复。
I'm guessing you're using Webrat with the Mechanize adapter, is that right? If so, I found myself very frustrated by the same issue. It turns out it is a bug in how Webrat passes form field values to Mechanize. You can find details and a patch here: https://webrat.lighthouseapp.com/projects/10503/tickets/384-webrat-does-not-pass-empty-form-fields-correctly-to-mechanize
Alternatively, if you don't want to use a patched version of Webrat, a slightly non-optimal workaround is to instead fill_in with whitespace (' ') and make sure your app's input validation trims or ignores whitespace when considering whether a field has been filled in correctly.
Unfortunately there seem to be a number of issues like this, which have contributed patches that haven't been merged into the "official" Webrat codebase. I e-mailed the author about a month and a half ago to ask whether he was still maintaining it and, if not, to please consider putting a call out for someone who would, as many people still use it. To date, I haven't yet received a response.
您可以尝试的一件事是确保在该字段上关闭自动完成功能 (autocomplete="off"),看看这是否会影响结果。
One thing you can try is to make sure autocomplete is turned off on that field (autocomplete="off") to see if that affects the outcome.
您可以在该步骤中尝试类似的操作吗?
或者更好,在功能文件中使用
我还建议转到 Capybara,您可以执行以下操作:
https://github.com/jnicklas/capybara/issues/issue/219
这将让你为你的 selenium 测试设置 Firefox 配置文件。
Can you try something like this within the step?
Or even better, in the feature file use
I would also suggest moving to Capybara, you can do things like this:
https://github.com/jnicklas/capybara/issues/issue/219
Which will let you set up Firefox profiles for your selenium tests.