Neal Enssle
Neal Enssle

Reputation: 363

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:

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!

Upvotes: 36

Views: 15867

Answers (4)

Artur INTECH
Artur INTECH

Reputation: 7276

You don't have to use DSL:

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'capybara'
  gem 'selenium-webdriver'
end

require 'capybara'

session = Capybara::Session.new(:selenium_headless)
session.visit('https://google.com')
session.click_on('test')

When using capybara/dsl, the Session is initialized automatically for you.

https://www.rubydoc.info/gems/capybara/Capybara/Session

If you're using Capybara for automation (rather than testing), consider using additional options for performance reasons:

Capybara.register_driver :chrome do |app|
  options = Selenium::WebDriver::Options.chrome
  options.add_argument('--headless=new')
  options.add_argument('--window-size=1920,1080')
  options.add_argument('--no-sandbox')
  options.add_argument('--disable-extensions')
  options.add_argument('--disable-notifications')
  options.add_argument('--disable-popup-blocking')
  options.add_argument('--disable-web-security')
  options.add_argument('--disable-dev-shm-usage')
  options.add_argument('--disable-site-isolation-trials')
  options.add_argument('--disable-features=Translate')

  # https://www.selenium.dev/documentation/webdriver/drivers/options/#pageloadstrategy
  options.page_load_strategy = :eager

  Capybara::Selenium::Driver.new(app, browser: :chrome, options:)
end
Capybara.default_driver = :chrome

Upvotes: 0

G. I. Joe
G. I. Joe

Reputation: 1653

Please check this CapybaraRspec101 example and fork it.

It's a small example for acceptance tests on http://www.hi5.com using from scratch:

  • Capybara
  • Rspec
  • Selenium-webdriver

All instructions are in the repo

Upvotes: 4

Pål Brattberg
Pål Brattberg

Reputation: 4698

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

Upvotes: 31

jnicklas
jnicklas

Reputation: 2225

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.

Upvotes: 5

Related Questions