2010-12-12 27 views
3

我想驗證具有特定href的鏈接是否存在於頁面上。我目前正在做我應該看到「/ some-link-here」,但似乎失敗。我怎樣才能確保鏈接存在,而不必點擊+我應該在「/ some-link-here」頁面?確定鏈接是否存在w黃瓜/水豚

回答

5

您將需要添加自定義步驟

Then /^"([^\"]*)" should link to "([^\"]*)"(?: within "([^\"]*)")$/ do |link_text, 
page_name, container| 
    with_scope(container) do 
    URI.parse(page.find_link(link_text)['href']).path.should == path_to(page_name) 
    end 
end 

您可以像使用Then "User Login" should link to "the user_login page"步驟,USER_LOGIN是你的路線

0

我用jatin的答案的名字,但有一個單獨的作用域步:

When /^(.*) within ([^:]+)$/ do |step, parent| 
    with_scope(parent) { When step } 
end 

Then /^"([^\"]*)" should link to "([^\"]*)"$/ do |link_text, page_name| 
    URI.parse(page.find_link(link_text)['href']).path.should == path_to(page_name) 
end 

然後,我有這個在我的測試:

step '"my foods" should link to "food_histories" within ".tabs"' 

這在我的路徑:

# note: lots not shown 
def path_to(page_name) 
    case page_name 
    when /^food_histories$/ 
    food_histories_path 
    end 
end 
0

這是我做了我自己,很簡單,但它確實意味着你硬編碼您的網址這是誠實的,因爲它使你的測試很脆不理想。特別是如果您使用的是第三方網址!

但是,如果您使用的是您管理的網址,並且很樂意繼續進行此測試,那就去做吧。

Then /^the link is "(.*?)"$/ do |arg1| 
    page.should have_xpath("//a[@href='" + arg1 + "'][@target='_blank']") 
end