Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Capybara in pure Ruby (without Rails)?

I'm trying to get Capybara running in a simple Ruby script -- i.e. without/outside of Rails. Here's the script:

require 'rubygems' require 'capybara' require 'capybara/dsl'  include Capybara  Capybara.current_driver = :selenium Capybara.app_host = 'http://www.google.com'  visit('/') 

The problem is that when I run this I get this error:

NameError: uninitialized constant Capybara::Session  at top level    in dsl.rb at line 52 method gem_original_require in custom_require.rb at line 36 method require  in custom_require.rb at line 36 at top level    in capybara_test.rb at line 3 method gem_original_require in custom_require.rb at line 31 method require  in custom_require.rb at line 31 at top level    in capybara_test.rb at line  

What am I doing wrong?

Some more info:

  • Mac OS X 10.5
  • ruby 1.8.6 (2009-06-08 patchlevel 369) [universal-darwin9.0]
  • capybara (0.3.9)

Thanks!

Neal

Note: Per the comment from jnicklas I tried this, which matches the new README more closely:

require 'rubygems' require 'capybara' require 'capybara/dsl'  Capybara.default_driver = :selenium Capybara.app_host = 'http://www.google.com'  module MyCapybaraTest   include Capybara    def test_google     visit('/')   end end 

Unfortunately, I'm still seeing the same error:

NameError: uninitialized constant Capybara::Session 

Thoughts?

Thanks!

like image 298
Neal Enssle Avatar asked Aug 14 '10 16:08

Neal Enssle


People also ask

What is Capybara in Ruby?

Capybara is a web-based test automation software that simulates scenarios for user stories and automates web application testing for behavior-driven software development. It is written in the Ruby programming language.

What is Capybara selenium?

Capybara helps you test web applications by simulating how a real user would interact with your app. It is agnostic about the driver running your tests and comes with Rack::Test and Selenium support built in. WebKit is supported through an external gem.


2 Answers

Here's something that seems to work for me:

require 'rubygems' require 'capybara' require 'capybara/dsl'  Capybara.run_server = false Capybara.current_driver = :selenium Capybara.app_host = 'http://www.google.com'  module MyCapybaraTest   class Test     include Capybara::DSL     def test_google       visit('/')     end   end end  t = MyCapybaraTest::Test.new t.test_google 
like image 158
Pål Brattberg Avatar answered Sep 18 '22 12:09

Pål Brattberg


It goes to show that even incorrect documentation lives forever. The Capybara README used to recommend to include Capybara in the global namespace, this is a really bad idea, and messes up any number of random things. You should include Capybara in your own module or class and use that instead.

Check out the README for current best practices.

like image 39
jnicklas Avatar answered Sep 20 '22 12:09

jnicklas