---
title: "脱离rails环境单独使用capybara以及使用capybara测试拖放"
author: "夜鸣猪"
date: "2012-01-17T12:49:03Z"
tags: []
categories: ["编程语言"]
slug: "rails-capybara-capybara"
draft: false
lastmod: "2012-01-17T12:56:30Z"
id: 1354271
url: "https://hlee.iteye.com/blog/1354271"
---

```ruby

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
    def test_google
      visit('/')
    end
  end
end

t = MyCapybaraTest::Test.new
t.test_google

```


注意只有内置的selenium，或者使用其他扩展才能测试javascript，例如drag and drop



```ruby

describe Capybara::Driver::Selenium do
  before do
    @driver = Capybara::Driver::Selenium.new(TestApp)
  end
 
  it_should_behave_like "driver"
  it_should_behave_like "driver with javascript support"
end

shared_examples_for "driver with javascript support" do
  before { @driver.visit('/with_js') }
 
  describe '#find' do
    it "should find dynamically changed nodes" do
      @driver.find('//p').first.text.should == 'I changed it'
    end
  end
 
  describe '#drag_to' do
    it "should drag and drop an object" do
      draggable = @driver.find('//div[@id=drag]').first
      droppable = @driver.find('//div[@id=drop]').first
      draggable.drag_to(droppable)
      @driver.find('//div[contains(., "dropped!")]').should_not be_nil
    end
  end
 
  describe "#evaluate_script" do
    it "should return the value of the executed script" do
      @driver.evaluate_script('1+1').should == 2
    end
  end
end


```



```ruby

#rspec版本
#Gemfile
source 'http://rubygems.org'
gem 'rspec'
gem 'capybara', :git => 'https://github.com/jnicklas/capybara.git'
gem 'launchy'
gem 'ruby-debug19'
#sign_in_spec.rb
require File.dirname(__FILE__) + '/../spec_helper'
  
describe "sign in", :type => :request do
  it "should sign me in" do  
    # Sign in and make assertions
  end  
end  
#spect_helper.rb
require 'rubygems'
require 'bundler/setup'
require 'ruby-debug'
require 'rspec'
require 'capybara/rspec'
Dir.glob(File.dirname(__FILE__) + '/factories/*', &method(:require))

# Capybara configuration
Capybara.default_driver = :selenium
Capybara.save_and_open_page_path = File.dirname(__FILE__) + '/../snapshots'

# RSpec configuration
RSpec.configure do |config|
  config.before(:all) do
    # Create fixtures
  end
  config.after(:all) do
    # Destroy fixtures
  end
  config.around(:each) do |example|
    begin
      example.run
    rescue Exception => ex
      save_and_open_page
      raise ex
    end
  end
end

```


[capybara相关API](http://rubydoc.info/github/jnicklas/capybara/master)
[这是一个cucumber配合capybara测试外部URL，不是本机网站的例子](https://github.com/searls/remote-capybara-cucumber-example)
[一些capybara的扩展驱动](https://github.com/jnicklas/capybara/wiki/Available-drivers)
[关于capybara](http://blog.plataformatec.com.br/tag/capybara/)