Search code examples
ruby-on-railsrubycucumberbddwebrat

How can I test with a certain word is a link or not with Cucumber?


I would like this custom step:

Then I should see the link 'foo'

and his opposite:

But I should not see the link 'foo'

In the page I can have something like:

lorem foo bar

or alternatively

lorem <a href=''>foo</a> bar

I need to test when 'foo' is a link and when isn't. Thank you.


Solution

  • Try something like this (I haven't tried running it, so minor tweaks might be needed):

    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