2011-07-19 64 views
3

我試圖測試用戶點擊一個按鈕,使ajax調用。當我在瀏覽器中手動點擊它時,它的行爲與預期相同,即忽略按鈕的默認行爲,而是通過ajax獲取結果,然後將結果添加到頁面中。爲什麼不能讓硒與水豚踢

但是,當我運行我的測試使用水豚,點擊按鈕後,它重定向到按鈕操作。看來硒沒有踢進去。我不知道爲什麼。

它是我的配置嗎?由於它在開發模式下工作Im假設這不是由於我的jQuery代碼,所以爲了簡潔起見不顯示。

的Gemfile

source 'http://rubygems.org' 

gem 'rails', '3.1.0.rc4' 

# Bundle edge Rails instead: 
# gem 'rails',  :git => 'git://github.com/rails/rails.git' 

gem 'sqlite3' 
gem 'omniauth', '~>0.2.0' 
gem 'pusher' 
gem 'youtube_it' 
gem 'simple_form' 

# Asset template engines 
gem 'sass-rails', "~> 3.1.0.rc" 
gem 'coffee-script' 
gem 'uglifier' 


gem 'jquery-rails' 

# Use unicorn as the web server 
# gem 'unicorn' 

# Deploy with Capistrano 
# gem 'capistrano' 

# To use debugger 
# gem 'ruby-debug19', :require => 'ruby-debug' 

group :test do 

    gem "shoulda" 
    gem "factory_girl_rails" 
    # Pretty printed test output 
    gem 'turn', :require => false 
    gem 'mocha' 
end 

group :development do 
    gem 'rails3-generators' 
    gem "autotest" 
end 

group :development, :test do 
    gem "capybara", :git => 'git://github.com/jnicklas/capybara.git' 
    gem "launchy" 
    gem "haml-rails" 
    gem "database_cleaner" 
end 

或我test_helper

ENV["RAILS_ENV"] = "test" 
require File.expand_path('../../config/environment', __FILE__) 
require 'rails/test_help' 
require 'shoulda/rails' 
require "capybara/rails" 


class ActiveSupport::TestCase 
    # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order. 
    # 
    # Note: You'll currently still have to declare fixtures explicitly in integration tests 
    # -- they do not yet inherit this setting 
    fixtures :all 

    OmniAuth.config.test_mode = true 

    # Add more helper methods to be used by all tests here... 
    def login_in(user) 
    @request.session[:user_id] = user.id 
    end 


    def should_redirect_unauthorized 
    assert_redirected_to root_path 
    assert_match /you need to login/i, flash[:alert] 
    end 
end 


module ActionController 
    class IntegrationTest 
    include Capybara::DSL 

    self.use_transactional_fixtures = false 

    setup do 
     DatabaseCleaner.strategy = :truncation 
     DatabaseCleaner.start #workaround for capybara/selenium. See capybara docs 
    end 


    teardown do 
     DatabaseCleaner.clean #workaround for capybara/selenium. See capybara docs 
    end 

     #signup using twitter, facebook for authentication 
    def signup_using(provider) 
     OmniAuth.config.add_mock(provider.to_sym, {'uid' => "123456"}) 

     visit '/' 
     page.click_link("#{provider}_auth") 

     assert_match /\/users\/\d+\/edit/, current_path 
     assert page.find("#flash").has_content?("Welcome to") 
    end 

    #login into existing account using twitter, facebook 
    def login_using(service) 
     OmniAuth.config.add_mock(service.provider.to_sym, {'uid' => service.uid}) 
     visit '/' 
     page.click_link("#{service.provider}_auth") 
     assert page.find("#flash").has_content?("Welcome back") 
     assert_equal rooms_path, current_path 
    end 

    def login_and_visit_room(service, room) 
     login_using(service) 
     visit_room(room) 
    end 

    def visit_room(room) 
     visit room_path(room) 
     assert_equal room_path(@room.id), current_path 
    end  
    end 
end 

或在我的集成測試的設置塊

require 'test_helper' 


class PlaylistStoriesTestTest < ActionDispatch::IntegrationTest 
    fixtures :all 

    setup do 
    Capybara.current_driver = :selenium 
    @user = Factory(:user) 
    @service = @user.services.create(:provider => "twitter", :uid => "123456") 
    @room = Factory(:room) 
    end 

    .... 

teardown do 
    Capybara.use_default_driver 
     DatabaseCleaner.clean #workaround for capybara/selenium. See capybara docs 
    end 
end 

回答

0

隨着水豚,你不應該感到困惑與之間的區別鏈接(即使它看起來像一個按鈕)和一個按鈕(如「提交」)。你沒有提供視圖文件的內容,但我想,你正在使用一個按鈕,而不是一個鏈接。

隨着水豚,你必須區分

visit '/' 
click_button 'Login' 

visit '/' 
click_link 'Home' 

又見Capybara-Documentation在GitHub上