8

I am using rspec to test some pages and would like to use the click method. However the DOM doesn't have the link or button. it's just a span and magically it becomes a link with javascript.

<span class="footerLink feedbackLink">
    Feedback
</span>

rspec offers "click_link" and "click_button"

any ideas how I could click a span?

Sarah A
  • 1,185
  • 12
  • 27
  • When you say "magically becomes a link" do you mean that you attach a onclick handler with Javascript or you change the element type to be an anchor from a span after some Javascript code runs? – Corey Sunwold Mar 23 '13 at 20:29
  • Yes, sorry for using "magically." onclick even is being attached. Frankly I think it would have been easier to just have a link there but that's the current design – Sarah A Mar 23 '13 at 21:08
  • Possible duplicate: [Cucumber and Capybara, clicking a non-link or button element](http://stackoverflow.com/q/3585533/841064) – Andrei Botalov Mar 24 '13 at 20:01

1 Answers1

12

You should be able to do

find('.feedbackLink').click

find returns an Element, and the Element class has a #click method.

Since you probably are handling the click in Javascript, don't forget to add :js => true as a metatag on your test.

Mark Rushakoff
  • 249,864
  • 45
  • 407
  • 398
  • Thank you very much for your answer: I added specify "sends feedback", js: true do and find('.feedbackLink').click. But it opens Firefox. is there a way to skip that and run it just as any other test in rspec? – Sarah A Mar 23 '13 at 21:06
  • @SarahA unfortunately, if it depends on Javascript, it needs to run in a browser. The good news is that the browser doesn't have to be Firefox -- [Poltergeist](https://github.com/jonleighton/poltergeist) is one among several headless options. – Mark Rushakoff Mar 23 '13 at 21:09
  • 1
    Yeah, you can ignore js specs: `rspec --tags ~js` And Capybara (the tester backend of RSpec) can use https://github.com/thoughtbot/capybara-webkit what is a headless version of WebKit rendering engine. – Gabor Garami Mar 23 '13 at 21:46