How to test dynamic parts in a view (dhtml) with cucumber, rspec, capybara rails?

I want both to test Ajax content and normal downloadable content through Javascript.

My setup:

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

For example, I want to have a form that shows only certain fields if a certain type of this model is selected:

An article can be an external article (url) or internal. Type "externa_url" should show 2 input fields and 2 check boxes larger than Type "article", which instead has a text area of ​​the body.

What is the best way to implement this, also when testing?

If the server is one-way, so that partial files are uploaded, if a certain type of article is selected, or with javascript, switch the necessary html?

+4
source share
1 answer

Actually inspired by Francico (in the comments), I write down my knowledge. I am writing my own answer, this may help others ...

First of all, I want to mention, I answer my question: - only integration testing. With cucumber and selenium. And concrete javascript testing with jasmine.

But, when testing integration with cucumber (edge) rails (3.1), capybara and selenium, you should know about some things:

See that you have updated all your gems!

1) Activate your driver if you are not already

functions / support / capybara.rb

Capybara.javascript_driver = :selenium 

2) At the moment, only Firefox <= 4 works with webdriver for rails, since I barely found after several hours setting up and installing each component from scratch, for example, a rack, etc.

3) Capybara itself does not process much, which serves you for klicking, for example. in lists, especially jquery-tokeninput.

3.1) I use this to select 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 can find this method with "locate" instead of find, do not try this, api / driver has changed to find. Automatically find expectations and check Ajax response in addition to dom search elements.

4) Add your own helper / search / click routines for your JS / Ajax response code, keep in mind that this is an β€œonly” integration test, you can test your JS code using yasmine or another js test environment.

For more information, also check out Screencasts from Ryan Bates (http://railscasts.com), he covers several topics on testing Rails; check out the latest version for Javascript, for example.

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

hope this helps someone else too.

+2
source

All Articles