Search code examples
cssrubycss-selectorscucumberwebrat

Cucumber/Webrat: follow link by CSS class?


is it possible to follow a link by it's class name instead of the id, text or title? Given I have (haha, cucumber insider he?) the following html code:

<div id="some_information_container">
  <a href="edit" class="edit_button">Translation here</a>
</div>
  • I do not want to match by text because I'd have to care about the translation values in my tests
  • I want to have my buttons look all the same style, so I will use the CSS class.
  • I don't want to assign a id to every single link, because some of them are perfectly identified through the container and the link class

Is there anything I missed in Cucumber/Webrat? Or do you have some advices to solve this in a better way?

Thanks for your help and best regards,

Joe

edit: I found an interesting discussion going on about this topic right here - seems to remain an open issue for now. Do you have any other solutions for this?


Solution

  • Here's how I did it with cucumber, hope it helps. The # in the step definition helps the CSS understand whats going on.

    This only works with ID's not class names

    Step Definition

    Then /^(?:|I )should see ([^\"]*) within a div with id "([^\"]*)"$/ do |text, selector|
      # checks for text within a specified div id
      within "##{selector}" do |content|  
        if defined?(Spec::Rails::Matchers)
          content.should contain(text)
        else
          hc = Webrat::Matchers::HasContent.new(text)
          assert hc.matches?(content), hc.failure_message
        end  
      end     
    end
    

    Feature

    Scenario Outline: Create Project
      When I fill in name with <title>
          And I select <data_type> from data_type
          And I press "Create"
      Then I should see <title> within a div with id "specifications"
    
    Scenarios: Search Terms and Results
       | data_type  | title        |
       | Books      | A Book Title |