如何验证链接指向正确的图像并具有正确的目标和标题

发布于 2024-09-19 01:22:01 字数 473 浏览 4 评论 0原文

我在 Rails 2.3.9 应用程序中使用水豚和黄瓜。

这是我的 html 代码

<a href="http://twitter.com/dorelal" target="_blank" title="twitter">
  <img alt="twitter" src="/images/social/twitter.png?1284129939" />
</a>

我想验证以下项目

image should be ending with twitter.png
image alt should be "twitter"
link should have href "http://twitter.com/dorelal"
link should have target "_blank"
link should have title "twitter"

I am using capybara with cucumber in rails 2.3.9 application.

Here is my html code

<a href="http://twitter.com/dorelal" target="_blank" title="twitter">
  <img alt="twitter" src="/images/social/twitter.png?1284129939" />
</a>

I want to verify following items

image should be ending with twitter.png
image alt should be "twitter"
link should have href "http://twitter.com/dorelal"
link should have target "_blank"
link should have title "twitter"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

初与友歌 2024-09-26 01:22:02

Capybara 现在支持检查 href 的功能。

https://github.com/jnicklas/capybara/pull/207

Capybara now supports the ability to check for href.

https://github.com/jnicklas/capybara/pull/207

屌丝范 2024-09-26 01:22:02

验证链接的最佳方法是让您的驱动程序单击它,然后检查它是否到达正确的页面。这将使您更有信心您的应用程序不会错误地出错、重定向等。对于不明智地单击测试中的链接的特殊情况(例如:如果链接不在现场),我使用此步骤:

Then I should see a link titled "foo"
Then I should see a link titled "foo" that goes to "http://www.example.org"

Then /^I should see a link titled "(.+?)"(?: that goes to "(.+)")?$/ do |title, target|
  if target.blank?
    page.should have_link(title)
  else
    page.should have_link(title, :href => target)
  end
end

如果如果您收到“参数数量错误(2 为 1)”,请更新您的 Capybara 版本。 :href =>最近添加了“bar”选项。

The best way to verify a link is to have your driver click it and then check that it made it to the right page. That will give you more confidence that your app isn't incorrectly erroring, redirecting, etc. For the exceptional cases where clicking the link in a test is ill-advised (ex: if the link goes offsite), I use this step:

Then I should see a link titled "foo"
Then I should see a link titled "foo" that goes to "http://www.example.org"

Then /^I should see a link titled "(.+?)"(?: that goes to "(.+)")?$/ do |title, target|
  if target.blank?
    page.should have_link(title)
  else
    page.should have_link(title, :href => target)
  end
end

If you get "wrong number of arguments (2 for 1)", update your Capybara version. The :href => 'bar' option was added recently.

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