so_brave 发表于 2012-01-19 23:25

脱离rails环境单独使用capybara以及使用capybara测试拖放

脱离rails环境单独使用capybara以及使用capybara测试拖放








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

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').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

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').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

#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
这是一个cucumber配合capybara测试外部URL,不是本机网站的例子
一些capybara的扩展驱动
关于capybara

梦境照进现实 发表于 2012-01-19 23:25

谢谢分享

sychangchun 发表于 2012-01-25 23:11

没有搞过啊。谢谢分享啊。
页: [1]
查看完整版本: 脱离rails环境单独使用capybara以及使用capybara测试拖放