使用 Webrat 查找包含链接的标签
因此,我正在使用 Cucumber 进行 BDD,并拥有一个带有从数据库填充的复选框的表单。复选框的标签包含超链接。到目前为止,还不算太奇特(注意,这是 HAML 而不是 Erb,但它应该具有足够的可读性任何 Rails 人员):
I would like my donation to support:
%br
- for podcast in @podcasts
= check_box_tag "donation[podcast_ids][]", podcast.id, true
= donation.label "donation[podcast_ids][]", link_to(podcast.name, podcast.url), :value => podcast.id
%br
问题是在我的 Cucumber 功能中,我不知道如何找到那个复选框来检查它。故事的相关部分是这样的:
Scenario: Happy path
Given I am on the home page
When I fill in "My email address" with "[email protected]"
# Skipped for brevity...
And I check the "Escape Pod" podcast
And I check the "PodCastle" podcast
And I press "I'm ready!"
Then I should see "Thank you!"
And there should be 2 podcast donation records
如果我使用裸的 webrat_steps.rb 文件,我会收到以下错误:
Could not find field: "Escape Pod" (Webrat::NotFoundError)
我很确定这是因为 link_to() 方法,我用它来使“Escape Pod”成为指向实际网站的超链接。但我无法从我的 Cucumber 步骤轻松访问
link_to
,并且我无法找出任何合理的方法将 Webrat 指向正确的复选框,而不是拼凑出整个内容我的步骤中有一堆超链接代码(这使得它非常脆弱)。
我的 BDD 此时已停滞。我不想仅仅因为难以测试就删除该链接。而且感觉它应该不难测试。 Webrat 只是限制了我可以传递给 checks()
方法的内容。谁能为此提出一个优雅的答案?
So I'm doing BDD with Cucumber and have a form with checkboxes populated from a database. The labels for the checkboxes contain hyperlinks. So far, not too exotic (note, this is HAML and not Erb, but it should be readable enough for any Rails person):
I would like my donation to support:
%br
- for podcast in @podcasts
= check_box_tag "donation[podcast_ids][]", podcast.id, true
= donation.label "donation[podcast_ids][]", link_to(podcast.name, podcast.url), :value => podcast.id
%br
The problem is that in my Cucumber features, I can't figure out how to find that checkbox to check it. The relevant part of the story is this:
Scenario: Happy path
Given I am on the home page
When I fill in "My email address" with "[email protected]"
# Skipped for brevity...
And I check the "Escape Pod" podcast
And I check the "PodCastle" podcast
And I press "I'm ready!"
Then I should see "Thank you!"
And there should be 2 podcast donation records
If I'm using the bare webrat_steps.rb file I get the following error:
Could not find field: "Escape Pod" (Webrat::NotFoundError)
I'm quite certain it's because of that link_to()
method, which I'm using to make "Escape Pod" a hyperlink to the actual Web site. But I can't easily access link_to
from my Cucumber step, and I can't figure out any reasonable way of pointing Webrat at the right checkbox short of kludging up a whole bunch of hyperlink code in my step (which makes it very brittle).
My BDD is stalled at this point. I don't want to take out the link just because it's hard to test. And it feels like it shouldn't be hard to test. Webrat is just limiting what I can pass into the checks()
method. Can anyone suggest an elegant answer for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简短的答案是使用 field_by_xpath 或其他 Webrat::Locators 方法之一来选择要在步骤中操作的元素:
您可能需要稍微使用该 xpath,或者使用
field_by_id
代替。请记住,它正在寻找标签的 html id,而不是数据库中的 id。The short answer is the to use field_by_xpath or one of the other Webrat::Locators methods to select what element to manipulate in your step:
You might need to play with that xpath a little, or use
field_by_id
instead. Remember it is looking got the html id of the tag not the id from the database.您能否在有问题的复选框附近发布您的 HTML 在渲染页面中的样子?有时你必须命名字段...我在登录表单方面遇到了各种各样的麻烦...我最终这样做了:
这样以下内容就起作用了:
我知道这不是一个复选框示例,但也许会摆弄你的名称/ID/等都可以
Can you post what your HTML looks like in the rendered page near the problematic checkbox(es)? Sometimes you have to play with naming the field... I had all sorts of trouble with a login form... I ended up doing this:
So that the following worked:
I know it's not a checkbox example, but maybe fiddling with your name/id/etc will work