10

我正在学习如何用 cucumber/webrat 编写测试。我的测试场景之一设置为测试表单验证(将字段留空)。奇怪的是,我没有填写fill_in的字段被设置为字段的name属性。这仅在我运行 cucumber 时发生,在使用浏览器时不会发生。

我正在使用的步骤很简单:

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>

知道为什么会这样吗?

4

3 回答 3

2

我猜你正在使用带有 Mechanize 适配器的 Webrat,对吗?如果是这样,我发现自己对同样的问题感到非常沮丧。事实证明这是 Webrat 如何将表单字段值传递给 Mechanize 的一个错误。您可以在此处找到详细信息和补丁:https ://webrat.lighthouseapp.com/projects/10503/tickets/384-webrat-does-not-pass-empty-form-fields-correctly-to-mechanize

或者,如果您不想使用 Webrat 的修补版本,一个稍微不理想的解决方法是使用空格 (' ') 代替 fill_in,并确保您的应用程序的输入验证在考虑字段是否已被删除时修剪或忽略空格正确填写。

不幸的是,似乎有很多这样的问题,它们提供了尚未合并到“官方”Webrat 代码库中的补丁。大约一个半月前,我给作者发了一封电子邮件,询问他是否仍在维护它,如果没有,请考虑打电话给愿意的人,因为很多人仍在使用它。到目前为止,我还没有收到回复。

于 2011-08-04T13:46:36.143 回答
1

您可以尝试的一件事是确保在该字段上关闭自动完成功能(autocomplete="off"),看看这是否会影响结果。

<form action="/item/create" method="post">
  <div>
    <label for="ItemName">Name</label>
    <input type="text" name="name" id="ItemName" autocomplete="off" />
  </div>
  <div>
    <label for="ItemDescription">Description</label>
    <textarea name="description" id="ItemDescription"></textarea>
  </div>
  <input type="submit" value="Save" />
</form>
于 2011-02-03T04:02:48.020 回答
0

你可以在步骤中尝试这样的事情吗?

给定 %{我用 ""} 填写 "name"

甚至更好,在功能文件中使用

鉴于我用“”填写“名称”。

我还建议搬到 Capybara,你可以这样做:

https://github.com/jnicklas/capybara/issues/issue/219

这将允许您为硒测试设置 Firefox 配置文件。

于 2011-02-04T05:18:47.220 回答