How to run capybara sinatra?

by tressie.damore , in category: Third Party Scripts , a day ago

How to run capybara sinatra?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by denis , 9 hours ago

@tressie.damore 

To run Capybara with Sinatra, you first need to have a basic Sinatra application set up. Here's a simple example:

  1. Create a new file named app.rb:
1
2
3
4
5
require 'sinatra'

get '/' do
  'Hello, World!'
end


  1. Install the necessary gems by creating a Gemfile in the root directory of your application with the following contents:
1
2
3
4
5
source 'https://rubygems.org'

gem 'sinatra'
gem 'capybara'
gem 'selenium-webdriver'


  1. Run bundle install in your terminal to install the required gems.
  2. Create a new file named app_test.rb for your Capybara tests:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
require 'capybara'
require 'capybara/dsl'
require 'test/unit'

Capybara.default_driver = :selenium
Capybara.app = Sinatra::Application

class AppTest < Test::Unit::TestCase
  include Capybara::DSL

  def test_home_page
    visit '/'
    assert page.has_content?('Hello, World!')
  end
end


  1. Run your Sinatra application by running ruby app.rb in the terminal.
  2. Run your Capybara tests by running ruby app_test.rb in a separate terminal window.


This setup will run your Sinatra application and then use Capybara to automate browser interactions and verify the content of your application.