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

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!

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

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.

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

Related

Can't run ruby automation file with cucumber - cannot load such file -- capybara (LoadError)

When I run cucumber, I get this error
cannot load such file -- capybara (LoadError)
According to this answer that's because i don't have poltergeist installed.
And if I run gem install poltergeist, I get
ERROR: While executing gem ... (ArgumentError)
wrong number of arguments (given 1, expected 0)
If I run gem list capybara, I just get
* LOCAL GEMS *
For last, this is what part of my env.rb file looks like
Learn more or give us feedback
require 'selenium-webdriver'
require 'capybara'
require 'capybara/poltergeist'
require 'capybara/cucumber'
$browser = ENV['browser'] ||:chrome
$browser = $browser.to_sym
$driver = ENV['driver'] || :selenium
$driver = $driver.to_sym
puts "Driver: #{$driver}"
puts "Browser: #{$browser}"
# For browser Firefox, geckodriver is required: https://github.com/mozilla/geckodriver/releases
# echo $PATH
# cd into geckodriver directory (probably Downloads) and extract from zip/tar
# mv geckodriver /usr/local/bin/ (or whatever your $PATH is)
# Run Firefox with:
# cucumber /dir/some.feature driver=selenium browser=firefox
Capybara.default_driver = $driver
Capybara.register_driver :selenium do |app|
Capybara::Selenium::Driver.new(app, :browser => $browser,)
end
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, {js_errors: false, phantomjs_options: ['--ssl-protocol=auto']})
$platform = 'poltergeist'
end
Any help will be appreciated
You don’t want or need Poltergeist anymore, it’s obsolete. Instead add capybara to your Gemfile, or if not using a Gemfile run gem install capybara. Also you should be doing require ‘capybara/dsl‘ instead of ‘require ‘capybara’`
See : https://github.com/teamcapybara/capybara/blob/master/README.md#setup

uninitialized constant SitePrism from page declariation

I'm trying to set up a basic framework using Capybara, Cucumber and SitePrism, but I keep getting the error, "uninitialized constant SitePrism (NameError)" when I kick off a test.
Gemfile:
gem 'cucumber', '2.3.3'
gem 'capybara', '2.6.2'
gem 'selenium-webdriver', '2.53.0'
gem 'rspec'
gem 'site_prism'
gem 'mime-types', '>2.6', '<2.99.1'
Env.rb
require 'capybara'
require 'capybara/rspec'
require 'capybara/dsl'
require 'capybara/cucumber'
require 'selenium-webdriver'
require 'site_prism'
require 'cucumber'
require_rel '../features/pages'
require_rel '../features/classes'
World(Capybara::DSL)
World(Capybara::RSpecMatchers)
Login page
class LoginPage < SitePrism::Page
end
Login class
class Login
def initialize
#current_page = LoginPage.new
end
The error is being thrown on the line with "class LoginPage < SitePrism::Page". RubyMine can't find the SitePrism declaration to go to either. Am I missing something in the setup?
Your error looks 'require' related, but here is how I got it to work via:
https://github.com/thuss/standalone-cucumber
Haven't used cucumber in a while, but the way I see "pages" currently implemented in my Rails project:
Create a file in "features/support/pages"
Follow the namespace conventions
Use modules, then import via the World() method.
Maybe this might work:
features/support/pages/login_page.rb
module Pages
module LoginPage
class LoginPageObj < SitePrism::Page
end
def login_obj
LoginPageObj.new
end
end
end
World(Pages::LoginPage)
Env file:
require 'capybara'
require 'capybara/cucumber'
require 'site_prism'
Capybara.configure do |config|
config.default_driver = :selenium
config.app_host = 'http://www.google.com'
end
World(Capybara::DSL)
World(Capybara::RSpecMatchers)
Notice how I didn't have to explicitly require any pages class, it looks like Cucumber might require it for you?
Note this is without RubyMine (I dont use it). If it works without RubyMine, I'd point fingers to that.
So the reason you were getting this problem is because of the files being auto-loaded. Just ensure you require the gem files first so the namespaces are understood

Capybara/Selenium - NS_ERROR_MALFORMED_URI for app on http://localhost

I'm trying to set up RSpec, Capybara & Selenium to test a PHP application running on the traditional localhost:80. Whenever I run the suite, Selenium fails and complains about a malformed URI.
Here's my spec_helper.rb.
require 'bundler/setup'
require 'rspec'
require 'capybara/rspec'
Capybara.server_port = 80
Capybara.app_host = 'http://localhost'
Capybara.run_server = false
Capybara.default_driver = :selenium
And here's my only spec:
require File.dirname(__FILE__) + '/../spec_helper'
describe "visting the website", :type => :request do
it "should display an html page" do
visit ( '/' )
page.should have_selector( 'html' )
end
end
But it bails. What am I missing?
Failures:
1) visting the website should display an html page
Failure/Error: visit ( '/' )
Selenium::WebDriver::Error::UnknownError:
Component returned failure code: 0x804b000a (NS_ERROR_MALFORMED_URI) [nsIIOService.newURI]
# ./spec/requests/sign_in_spec.rb:6:in `block (2 levels) in <top (required)>'
My URI doesn't look malformed to me... visit works if I pass in 'http://localhost' but that's not ideal.
Here's my Gemfile for good measure
gem 'rspec'
gem 'capybara', :git => 'https://github.com/jnicklas/capybara.git'
gem 'launchy'
gem 'ruby-debug19'
Thanks for any help.
#andrykonchin was right - switching back to stable worked
You need install bundler if it is not install
gem install bundler
Then you need to create a file called gemfile in the root directory (For my simple example I am using just capybara and no rspec)
source "http://rubygems.org"
gem "capybara" , "1.1.3"
Create a basic ruby file as follows
require 'bundler/setup'
require 'capybara/dsl'
Capybara.server_port = 80
Capybara.app_host = 'http://google.com'
Capybara.run_server = false
Capybara.default_driver = :selenium
class Browser
include Capybara::DSL
end
w = Browser.new
w.visit("/")
Use the command
bundle install
Now you will be using the stable version of Capybara
For more information on bundler look at http://gembundler.com/

undefined method `have_content' using Cucumber / Capybara / sinatra

I'm trying to validate the start of an initial page for a Sinatra application but am struggling to get the testing framework working. Googling around suggests I add cucumber/rails/rspec or similar, but do I really need to add rails related libraries when not using Rails?
Here's my Gemfile
source :rubygems
gem 'sinatra', '1.3.1'
gem 'json', '1.6.3'
group :test do
gem 'rspec', '2.7.0'
gem 'cucumber', '1.1.3'
gem 'capybara', '1.1.2'
gem 'rack-test', '0.6.1'
end
and my steps:
Given /^a room needs to be surveyed$/ do
end
When /^I start a survey$/ do
survey_tool.start_a_survey
end
Then /^the system will prompt me for a survey summary$/ do
survey_tool.validate_start_a_survey_page
end
and my world extensions
module KnowsTheUserInterface
class UserInterface
include Capybara::DSL
def start_a_survey()
visit '/start_a_survey'
end
def validate_start_a_survey_page ()
page.should have_content('Welcome')
end
end
def survey_tool
#survey_tool ||=UserInterface.new
end
end
World(KnowsTheUserInterface)
and my env
require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'survey_tool')
require 'capybara/cucumber'
require 'capybara/rspec'
Capybara.app = Sinatra::Application
Sinatra::Application.set :environment, :test
The error I receive is
Scenario: Start a survey # features/start_a_survey.feature:4
Given a room needs to be surveyed # features/step_definitions/start_a_survey_steps.rb:1
When I start a survey # features/step_definitions/start_a_survey_steps.rb:5
Then the system will prompt me for a survey summary # features/step_definitions/start_a_survey_steps.rb:9
undefined method have_content' for #<KnowsTheUserInterface::UserInterface:0x007f910465fc38> (NoMethodError)
./features/support/world_extensions.rb:10:invalidate_start_a_survey_page'
./features/step_definitions/start_a_survey_steps.rb:10:in /^the system will prompt me for a survey summary$/'
features/start_a_survey.feature:7:inThen the system will prompt me for a survey summary'
I think the problem here is that you do not have the rspec expectations included. A Capybara node has a has_content? method.
It is the rspec expectation that, when given 'have_content' as an argument, calls 'has_content?' on the target.
So try adding 'rspec-expectations' to your Gemfile, and adding require 'rspec/expectations' into your module.

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

I've been using the following code to test run using Capybara with IE8 in pure Ruby, i.e. NOT a rails app but every time I run the script IE8 pops up but then Firefox pops up and the tests run through Firefox leaving IE8 sitting in the background:
$:.unshift(File.dirname(__FILE__) + '/../../lib')
begin require 'rspec/expectations'; rescue LoadError; end
require 'rubygems'
require 'capybara'
require 'capybara/dsl'
Capybara.run_server = false
Capybara.app_host = 'http://www.google.com'
require 'selenium-webdriver'
Selenium::WebDriver.for :internet_explorer
Capybara.default_driver = :selenium
require 'cukesalad'
begin require 'rspec/expectations'; rescue LoadError; require 'spec/expectations'; end
This isn't too old maybe you can find some useful information from it:
Run Capybara & Cucumber Features In Internet Explorer On Remote Windows

Resources