开发者

How to verify an link points to the right image and has the right target and title

开发者 https://www.devze.com 2023-01-16 01:45 出处:网络
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\">

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"


Capybara now supports the ability to check for href.

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消