Subsecciones


Sample app with Sinatra Rspec Capybara and Selenium

Donde

Introducción

Véase la sección Capybara 18.

Capybara starts a browser.

Gemfile

[~/sinatra/sinatra-selenium/capybara-selenium(master)]$ cat Gemfile
source 'https://rubygems.org'

gem "thin" 
gem "sinatra"

group :development, :test do
  gem "rspec", ">= 1.2.1"
  gem "capybara", ">= 1.1.2"
  gem "selenium-webdriver"
end

Rakefile

[~/sinatra/sinatra-selenium/capybara-selenium(master)]$ cat Rakefile 
task :default => :spec

desc "run examples"
task :spec do
  sh "rspec -I. spec/my_sinatra_app_specs.rb"
end

task :server do
  sh "ruby my_sinatra_app.rb"
end

# Had many problems installing nokogiri 1.6.4 on my mac.
# solved this way:

# gem install nokogiri -- --use-system-libraries
#        [--with-xml2-config=/path/to/xml2-config]
#        [--with-xslt-config=/path/to/xslt-config]
desc "Install nokogiri 1.6.4 (11/2014)"
task :nokogiri do
 command = "gem install nokogiri -- --use-system-libraries "+
   "--with-xml2-config=/usr/local/Cellar/libxml2/2.9.1/lib "+
   "--with-xslt-config=/usr/local/Cellar/libxml2/2.9.1/include/"
  sh command
end

La aplicación: my_sinatra_app.rb

[~/sinatra/sinatra-selenium/capybara-selenium(master)]$ cat my_sinatra_app.rb 
require 'sinatra'
get '/' do
  "Hello Sinatra :)"
end

Las Pruebas: spec/my_sinatra_app_specs.rb

[~/sinatra/sinatra-selenium/capybara-selenium(master)]$ cat spec/my_sinatra_app_specs.rb 
require_relative '../my_sinatra_app.rb' # this load the file you are testing
require 'spec_helper.rb' # It will load the configuration you set in spec_helper.rb

describe 'make API call to load path' do 
  it "should load the home page" do
    visit 'http://0.0.0.0:4567'
    expect(page).to have_content("Sinatra")
  end
end

spec/spec_helper.rb

[~/sinatra/sinatra-selenium/capybara-selenium(master)]$ cat spec/spec_helper.rb 
require 'capybara' # loading capybara
include Capybara::DSL 
# It contain all the methods you use for writing test. 

=begin
it will tell capybara to use selenium. It should be noted that  
By default, Capybara uses the :rack_test driver, which is fast but
limited: it does not support JavaScript, nor is it able to access HTTP 
resources outside of your Rack application, such as remote
APIs and OAuth services.
=end
Capybara.default_driver = :selenium 

def app
  Sinatra::Application 
end

ENV['RACK_ENV'] = 'test'

RSpec.configure do |config|
#  config.expect_with :rspec do |expectations|
#    expectations.include_chain_clauses_in_custom_matcher_descriptions = true
#  end
#
#  config.mock_with :rspec do |mocks|
#    mocks.verify_partial_doubles = true
#  end

  config.run_all_when_everything_filtered = true
  config.filter_run :focus
  config.order = :random

=begin

  config.disable_monkey_patching!

  config.warnings = true

  if config.files_to_run.one?
    # Use the documentation formatter for detailed output,
    # unless a formatter has already been configured
    # (e.g. via a command-line flag).
    config.default_formatter = 'doc'
  end

  config.profile_examples = 10


  Kernel.srand config.seed
=end
end

Ejecución del Servidor

[~/sinatra/sinatra-selenium/capybara-selenium(master)]$ rake server
ruby my_sinatra_app.rb
== Sinatra/1.4.5 has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.6.3 codename Protein Powder)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop

Ejecución de las pruebas

~/sinatra/sinatra-selenium/capybara-selenium(master)]$ rake 
rspec -I. spec/my_sinatra_app_specs.rb
including Capybara::DSL in the global scope is not recommended!
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}

make API call to load path
  should load the home page

Finished in 5.78 seconds (files took 0.66653 seconds to load)
1 example, 0 failures

Randomized with seed 53508

Casiano Rodriguez León 2015-01-07