2

I want to use capybara to drive a web browser for an application I have written. I don't want to run rspec tests, I want to use the browser to navigate and perform actions. How do I set this up to use capybara command like 'visit' ?

I have:

require 'selenium-webdriver'
require 'capybara'
Capybara.default_driver  =:selenium
d= Selenium::WebDriver.for :chrome
d.navigate.to('http://www.google.com')
visit('http://www.google.com')
sleep 3

navigate.to works but visit gives me:

main.rb:6:in `<main>': undefined method `visit' for main:Object (NoMethodError)

I've tried adding include Capybara::DSL but I get uninitialized constant Capybara::DSL (NameError)

1 Answer 1

3

This will work:

require 'capybara'
require 'capybara/dsl'
require 'selenium-webdriver'
Capybara.configure do |c| 
  c.run_server = false
  c.default_driver = :selenium
  c.app_host = 'http://www.google.com'
end
Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, browser: :chrome)
end

class RunIt
  include Capybara::DSL
  def runner
    visit '/' 
    sleep 1
  end
end

RunIt.new.runner
1
  • 2
    Thanks Michael, for taking time to share your knowledge..! Commented Jun 13, 2018 at 21:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.