Cucumber/Webrat: follow link by CSS class?
Asked Answered
S

5

14

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?

Stoichiometry answered 20/10, 2009 at 6:48 Comment(1)
I've added the ruby tag, Brian.Flowerer
F
4

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 |
Faus answered 19/2, 2010 at 17:23 Comment(0)
R
3

Here is how to assert text within an element with the class name of "edit_botton"

Then I should see "Translation here" within "[@class='edit_button']"
Rite answered 2/6, 2010 at 21:38 Comment(0)
L
1

How about find('a.some-class').click?

Lux answered 21/2, 2012 at 9:4 Comment(0)
C
0

I'm not very familiar with the WebRat API, but what about using a DOM lookup to get the reference ID of the class that you are looking for then passing that to the click_link function?

Here's a link to some javascript to retrieve an item by class. http://mykenta.blogspot.com/2007/10/getelementbyclass-revisited.html

Now that I think about it, what about using Javascript to just simply change it to some random ID then clicking that?

Either way, that should work until the frugal debate of a name to include the getbyclass function as is resolved.

Currycomb answered 11/11, 2009 at 5:25 Comment(0)
L
0

Does have_tag work for you?

have_tag('a.edit_button')

Lionhearted answered 24/11, 2009 at 17:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.