4

I want both, testing Ajax Content and normal toggled content through Javascript.

My Setup is:

- Rails 3.1
- Rspec (Edge)
- Cucumber (Edge)
- Capybara (Edge)

For expample, I want to have a Form, which shows particular fields only, if a certain type of this model is selected:

Article can be an external article (url) or an internal. Type "externa_url" should show 2 input fields and 2 checkboxes more then Type "article", which has a body textarea instead.

Whats the best way to implement this, also with testing?

Should it be Server sided, so that partials are loaded, if a certain article type is selected, or with javascript, toggle the needed html?

radosch
  • 609
  • 3
  • 7
  • There is already a screencast out concerning this topic, but it doesn't fit my needs exactly, so to see a common best pratice is very hard. – radosch May 12 '11 at 00:15
  • 1
    Does this fits your needs? http://openmonkey.com/2010/04/09/javascript-testing-with-cucumber-capybara/ – Fran Jun 02 '11 at 09:09
  • Yes actually I found also only this... amongst other articles about testing with selenium. – radosch Jun 03 '11 at 06:15

1 Answers1

2

Actually inspired by Fransico (in the comments), I write down my knowledge. I write my own answer, it may helps others too....

First I want to mention, I answer my question with – do integration testing only. With cucumber and selenium. And specific javascript testing with jasmine.

But, when integration testing with cucumber (edge) rails (3.1), capybara and selenium you have to be aware of some things:

Look, that you have all your gems updated!

1) Activate your driver, if you haven't already

features/support/capybara.rb

Capybara.javascript_driver = :selenium

2) At the moment only Firefox <=4 works with webdriver selenium for rails, as I found out hardly after hours of configuring and installing each component from scratch, like rack etc.

3) Capybara itself not handles much, which serves you for klicking e.g. on lists, jquery-tokeninput especially.

3.1) I use this for selecting an Item from the tokens in the list:

When(/^I select the option containing "([^\"]*)" in the Tag List/) do |text|
  find("li:contains('#{text}')").click
end

You may find this method with "locate" instead of find, don't try this, api / driver has changed to find. Find waits automatically and check for a Ajax respond in addition of dom finding elements.

4) Add your own helper / finder / click routines for your JS / Ajax responded code, keep in mind, it is "only" an integration test, you may want to test your JS code with yasmine or another js test framework.

For furthter information also check Screencasts from Ryan Bates (http://railscasts.com), he covers several Topics on Testing Rails; check the latest one for Javascript e.g.

Or this blog: http://openmonkey.com/2010/04/09/javascript-testing-with-cucumber-capybara/ (thnx francisco)

hope this helps someone else as well.

radosch
  • 609
  • 3
  • 7
  • 1
    You're welcome. AFAIK you can use other drivers with capybara apart from selenium. I've heard quite good things from akephalos https://github.com/bernerdschaefer/akephalos maybe it worths having a look for you. – Fran Jun 03 '11 at 09:29