当多个名称和 ID 相同时,如何填写 Webrat 中的特定字段?
我刚刚开始使用 Cucumber 和 Webrat,并将功能规范添加到现有的 Rails 应用程序中。应用程序中的一个页面上有多个表单,代表构建新角色对象的不同方法。这是一个简化的示例:
<form id="adopt_character_form">
....
<input type="text" name="character[name]" id="character_name">
...
</form>
<form id="spawn_character_form">
....
<input type="text" name="character[name]" id="character_name">
...
</form>
如您所见,页面上(至少)有两个具有相同名称和 id 的字段,尽管它们的形式不同,而且我在 Webrat rdoc 或源代码中找不到方法代码来指定特定的一个。
我知道我可以更改其中一个的名称或 id,但我必须偏离 Rails 命名约定才能这样做,而我真的不愿意这样做。我也可以将表单放在不同的页面上,但这需要对工作流程进行更多的修改,而不是我想要的。我还可以尝试将它们合并成一个更加模块化和动态的单一形式,但是仅仅为了支持给定的测试框架而必须对 UI 进行结构更改似乎有点可疑,并且可能并不总是可行,而且我会需要 JavaScript。
有没有办法在 Webrat 中指定这些字段之一,或者我应该放弃并在另一个项目上尝试 Cucumber?
I'm just getting started with Cucumber and Webrat, and am adding feature specifications to an existing Rails application. One page in the app has multiple forms on it, representing different ways to build a new Character object. Here's a simplified example:
<form id="adopt_character_form">
....
<input type="text" name="character[name]" id="character_name">
...
</form>
<form id="spawn_character_form">
....
<input type="text" name="character[name]" id="character_name">
...
</form>
As you can see, there are (at least) two fields that have the same name and id on the page, though they are in different forms, and I can't find a way in the Webrat rdoc or source code to specify a particular one.
I know that I could change the name or id on one of them, but I would have to deviate from the Rails naming conventions to do so, which I'd really rather not. I could also put the forms on different pages, but it would take more modification to the work flow than I want to do. I could also try to merge them in to a single form that was more modular and dynamic, but having to make structural changes to the UI just to support a given testing framework seems a little questionable and might not always be feasible, plus I would have to require javascript.
Is there any way to specify one of these fields in Webrat, or should I just give up and try Cucumber on a different project?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 webrats
within
方法例如在黄瓜步骤文件中
希望有帮助
You can use webrats
within
methodFor example in a cucumber steps file
Hope that helps
考虑到多个元素具有相同的 ID 明显违反了 HTML 有效性,我认为您已经偏离了 Rails 约定的路径。 Rails 的最新版本通常会努力保持标准一致。 IIRC 以区分 ID 的方式调用表单助手非常简单。如果没有别的办法,你可以在每次调用时提供一个显式 ID。出于 Rails 约定的目的,
name
字段是唯一重要的字段,因为这是 Rails 必须映射回模型上的字段名称的字段。Considering having multiple elements with the same ID is an explicit violation of HTML validity, I think you've already deviated from the path of Rails conventions. Recent versions of Rails generally make an effort to keep things standards-conformant. IIRC it's pretty straightforward to call the form helpers in such a way that the IDs are differentiated. If nothing else you can just supply an explicit ID with each call. For the purpose of Rails conventions the
name
field is the only one that matters, since that's the one Rails will have to map back to a field name on a model.