4

We're developing a plugin for rails, and I've got Cucumber working with Capybara wonderfully. BUT, when developing, I've been (erroneously, I guess) putting the Javascript files in the parent test application's public/javascript directory. So when Capybara runs, it doesn't find the javascript file. How can we get Cucumber/Capybara to see those JS files?

A little more detail:

So the layout "reports.html.haml" refers to "src='/javascripts/...'" but when I run cucumber from within the vendor/plugins/reporter directory, Capybara doesn't find the JS files (I think). So should I be running cucumber from the application root, and including the Plugin's files, or is there some other place I need to include the JS files (for example, somewhere in the vendor/plugin/reporter hierarchy)?

Here is our current PLUGIN directory layout:

|~app/
| |~controllers/
| | `-reports_controller.rb
| |~models/
| | `-report.rb
| `~views/
|   |~layouts/
|   | `-reports.html.haml
|   `~reports/
|     |-edit.html.haml
|     |-index.html.haml
|     |-new.html.haml
|     |-show.html.haml
|     `-show.js.erb
|+autotest/
|+config/
|~features/
| |+step_definitions/
| |+support/
| `-reports.feature
|+generators/
|+lib/
|+spec/
|...
btelles
  • 5,390
  • 7
  • 46
  • 78
  • Capybara does not need to see the javascript files, you have to include them in your views? – Max Schulze Oct 26 '10 at 15:50
  • Right, so the layout "reports.html.haml" refers to "/javascripts/..." but when I run cucumber from within the vendor/plugins/reporter directory, Capybara doesn't find the JS files (I think). So should I be running cucumber from the application root, and including the Plugin's files, or is there some other place I need to include the JS files (for example, somewhere in the vendor/plugin/reporter hierarchy)? – btelles Oct 26 '10 at 15:57

3 Answers3

2

It sounds like what you need is to build your plugin as a Rails engine so that you can put the javascripts in the plugin, but still have them accessible from the parent application.

This is taken from a plugin I'm building with similar requirements. To include the javascripts you'd use something like javascript_include_tag 'my-plugin/javascripts/foo.js' assuming the file exists in [plugin_path]/public/javascripts/foo.js

module MyPlugin
  class Engine < Rails::Engine
    config.mount_at = '/my-plugin/'

    initializer "static assets" do |app|
      app.middleware.use ::ActionDispatch::Static, "#{root}/public"
    end
  end
end
Jon Wood
  • 2,589
  • 1
  • 20
  • 17
0

Capybara doesn't need to see the Javascript files directly - they'll need to be made available in the application's view. You'll have to deploy them there using a generator or rake task (since assets are meant to be served by the webserver directly, you can't use runtime paths.)

In order for Capybara so see the results of the Javascript, you'll need to use one of the Javascript enabled drivers - the default just fires up Rack and synthesizes queries at it. I've had the most success with Selenium, but env.js is meant to be faster if it's compatible since it doesn't need to fool around with an actual browser. Be aware that as soon as you need a browser compatibility plugin (jQuery, Prototype, Mootools, etc), env.js will barf.

If you're doing a lot of Javascript in your plugin, I highly recommend Jasmine - it's designed to work like RSpec but in Javascript and for testing the actual JS code is really fast as nice.

Judson
  • 744
  • 6
  • 12
  • Thanks Judson. I'm aware of Jasmine. And I'm aware of the use of various drivers that enable javascript support. Unfortunately the problem remains that even if try to use those, I still can't get the javascript to load on a cucumber test for a plugin (as the question implies). If you have any ideas how I might do that, please do let me know (I'd hate to see 150pts go to waste). – btelles Nov 02 '10 at 21:20
  • 2
    My experience with testing plugins is that you wind up having to test them in the context of an actual app - at least some features will require a full app as opposed to just the plugin. So: can you test the JS in a parent application? If the plugin is for Rails3, you have the option of building it as an application and including it as an Engine, too. – Judson Nov 03 '10 at 23:17
0

I have been using cucumber and capybara without any problem regarding the javascripts.

Could you please post a relevant stack trace and associated errors to see what's going on?

(Would have left a comment, but alas, I cannot yet)

  • Hmmm...I actually don't have a stack trace. The problem is that the page.body does not have the extra element that the JavaScript inserts. So the cucumber test just says "nope, that element doesn't exist". Also, keep in mind that I'm testing a Rails PLUGIN, not a regular Rails application, and the problem is that I think Capybara doesn't know where to find the JavaScript files. – btelles Nov 03 '10 at 17:50