How to use Capybara in pure Ruby (without Rails)?
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
NoMethod error for Capybara without Rails,Rspec or Cucumber
feature
and scenario
are part of RSpec. This is not loaded by default when using Capybara.
Assuming you want to include the Capybara modified RSpec, instead of doing:
require 'capybara'
Do:
require 'capybara/rspec'
If you just want plain RSpec, you can of course do:
require 'capybara'
require 'rspec'
Using Capybara live without RSpec
You can manually create a Capybara session and use that to interact with your production website. For example, the following will go to Google and get the text:
require 'capybara'
session = Capybara::Session.new(:selenium)
session.visit('https://www.google.com')
puts session.text
Note that neither Capybara nor RSpec require the system under test to be a local Ruby project. For example, the following RSpec test goes to Google and checks that the word "Google" appears:
require 'capybara/rspec'
Capybara.current_driver = :selenium
Capybara.app_host = 'http://www.google.com'
feature "google", :js => true do
scenario "should have text" do
visit('/')
page.should have_content(/Google/)
end
end
TDD Ruby with Capybara: How to use Capybara to verify specific page values within specific page elements
You just need to expect on the element you wish to check inside - for instance
heading = page.find('li#campaign_1 .media-heading.name')
expect(heading).to have_content('Campaign_1')
Another option (more useful when you have a few things to check) is the within
method which scopes what page
refers
within 'li#campaign_1 .media-heading.name' do
# here page will refer to the .name element
expect(page).to have_content('Campaign_1')
end
Finally, you could just do it all in one expectation using the text
or exact_text
options
expect(page).to have_css('li#campaign_1 .media-heading.name', text: 'Campaign_1')
Combining a couple of those methods together would give
within 'li#campaign_1' do
expect(page).to have_css('.media-heading.name', text: 'Campaign_1'
expect(page).to have_css('.media-heading.country', text: 'United Kingdom')
end
Get raw content of screenshot in capybara, without saving image to disk
PhantomJS provides a renderBase64
method. API Reference
Poltergeist however provides no binding for it.
I just had a quick look, but I think you should be able to fork/patch Poltergeist to include the binding in driver.rb and browser.rb
Here is already a discussion about this: https://github.com/jonleighton/poltergeist/issues/189
Using Capybara to test pure JavaScript application
Here is the original answer from Jonas Nicklas.
You need to require 'capybara/rspec' and set :type => :request.
See the Capybara README section on "Using Capybara with RSpec".
/Jonas
Here is a working example on Github.
Related Topics
Ruby Strftime: Month Without Leading Zero
Ruby on Rails, Could Not Find a Valid Gem 'Rails'
Capistrano & Bash: Ignore Command Exit Status
How to Send Mail with Rails Without a Template
How Are Symbols Used to Identify Arguments in Ruby Methods
How to Make Object Instance a Hash Key in Ruby
Bundle Install: Error: Failed to Build Gem Native Extension. Nio4R Gem
Does Scala Scale Better Than Other Jvm Languages
How to Use an Actionview::Helper in a Ruby Script, Outside of Rails
Which Ruby Gems Support the Facebook API
How to Force Activerecord to Reload a Class
Ruby Koan 151 Raising Exceptions
Bundle Install Could Not Fetch Specs from Https://Rubygems.Org/
Finding All the Users That Have Duplicate Names