代码之家  ›  专栏  ›  技术社区  ›  Nick Vanderbilt

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

  •  0
  • Nick Vanderbilt  · 技术社区  · 14 年前

    我在Rails2.3.9应用程序中使用了Capybara和黄瓜。

    这是我的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"
    
    2 回复  |  直到 14 年前
        1
  •  1
  •   Tyler K    14 年前

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

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

        2
  •  0
  •   ddurdik    14 年前

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

    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'选项。