欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-04-01 20:42:48
...
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


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



#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