简体   繁体   中英

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

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?

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 eg 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 eg

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

hope this helps someone else as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM