2010-07-23 60 views

回答

1

嘗試是這樣的(我沒有試過運行,所以可能需要一些小改動):

Then /^I should see the link "([^\"]*)"$/ do |linked_text| 
    # AFAIK this raises an exception if the link is not found 
    find_link(linked_text) 
end 

Then /^I should not see the link "([^\"]*)"$/ do |linked_text| 
    begin 
    find_link(linked_text) 
    raise "Oh no, #{linked_text} is a link!" 
    rescue 
    # cool, the text is not a link 
    end 
end 
+0

未定義的方法'find_link'for#(NoMethodError) – 2010-07-28 11:25:06

0

已經有預定義的Webrat這樣做的步驟(或者至少非常相似)。從web_steps.rb

Then /^(?:|I)should see \/([^\/]*)\/ within "([^\"]*)"$/ do |regexp, selector| 
    within(selector) do |content| 
    regexp = Regexp.new(regexp) 
    if defined?(Spec::Rails::Matchers) 
     content.should contain(regexp) 
    else 
     assert_match(regexp, content) 
    end 
    end 
end 

Then /^(?:|I)should not see "([^\"]*)" within "([^\"]*)"$/ do |text, selector| 
    within(selector) do |content| 
    if defined?(Spec::Rails::Matchers) 
     content.should_not contain(text) 
    else 
     hc = Webrat::Matchers::HasContent.new(text) 
     assert !hc.matches?(content), hc.negative_failure_message 
    end 
    end 
end 
+0

能否請您寫在這裏的鏈路中的一個字一個例子,我嘗試沒有成功: 我應該能看到「富」中的「A」 我應該看到在「富」「[ a]「 我應該在」[a]「 之內看到」[foo]「,但它不起作用。 – 2010-07-24 10:32:31

0

你可能有更多的運氣使用xpaths爲此,你可以使用類似「// a/text()='foo'」。你只需要爲webrat啓用xpaths,我寫了一篇博客文章,關於在這裏使用xpaths做些類似於http://www.opsb.co.uk/?p=165的表,它包括啓用webrat的xpaths所需的代碼。

0
Then /^I should not see the link "([^\"]*)"$/ do |linked_text| 
    page.should_not have_css("a", :text => linked_text) 
end 

Then /^I should see the link "([^\"]*)"$/ do |linked_text| 
    page.should have_css("a", :text => linked_text) 
end