当多个名称和 ID 相同时,如何填写 Webrat 中的特定字段?

发布于 2024-08-08 13:06:54 字数 796 浏览 3 评论 0原文

我刚刚开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神妖 2024-08-15 13:06:54

您可以使用 webrats within 方法

例如在黄瓜步骤文件中

#my_steps
Then /^I update character name with  "([^\"]*)"$/ do |updated_character|
  within '#adopt_character_form'
     fill_in "character[name]", :with => updated_character
  end
end

希望有帮助

You can use webrats within method

For example in a cucumber steps file

#my_steps
Then /^I update character name with  "([^\"]*)"$/ do |updated_character|
  within '#adopt_character_form'
     fill_in "character[name]", :with => updated_character
  end
end

Hope that helps

无法回应 2024-08-15 13:06:54

考虑到多个元素具有相同的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文