2010-07-23 13 views

Répondre

1

Essayez quelque chose comme ça (je ne l'ai pas essayé de courir, donc quelques réglages mineurs pourraient être nécessaires):

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

méthode non définie 'find_link 'pour # (NoMethodError) –

0

Il y a déjà étapes prédéfinies qui le font (ou sont au moins très similaires). De 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 

et

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

Pouvez-vous s'il vous plaît écrire ici un exemple pour un mot dans un lien, j'ai essayé sans succès: Et je devrais voir « toto » dans « un » Et je devrais voir « foo » dans « [ a] " Et je devrais voir" [foo] "dans" [a] " mais cela ne fonctionne pas. –

0

Vous pourriez avoir plus de chance en utilisant xpaths pour cela, vous pouvez utiliser quelque chose comme "// a/text() = 'foo'". Vous avez juste besoin d'activer xpaths pour webrat, j'ai écrit un post de blog sur faire quelque chose de similaire avec xpaths dans les tables ici http://www.opsb.co.uk/?p=165, il comprend le code requis pour activer xpaths pour webrat.

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