为什么我不能让硒与水豚一起发挥作用

发布于 2024-11-24 15:22:03 字数 3761 浏览 0 评论 0原文

我试图测试用户单击一个进行 ajax 调用的按钮。当我在浏览器中手动单击它时,它的行为符合预期,即忽略按钮的默认行为,而是通过 ajax 获取结果,然后将结果添加到页面中。

但是当我使用水豚运行测试时,单击按钮后它会重定向到按钮操作。硒似乎没有发挥作用。我不明白为什么。

是我的配置吗?因为它在开发模式下工作,我假设这不是由于我的 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

Im trying to test a user clicking on a button which makes an ajax call. When i click it manuallly in my browser it behaves as expected i.e. default behaviour of the button is ignored and instead it gets the results via ajax which are then added to the page.

But when i run my tests using capybara, after clicking on the button it redirects to the buttons action. It seems selenium isnt kicking in. I cant figure out why.

Is it my config? Since it works in development mode Im assuming this isnt due to my jquery code so for brevity not displaying that.

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

or my 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

or the setup blocks in my integration test

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

み青杉依旧 2024-12-01 15:22:03

使用 Capybara,您不应该对链接(即使它看起来像按钮)和按钮(如“提交”)之间的区别感到困惑。您没有提供查看文件内容,但我猜您使用的是按钮,而不是链接。

对于水豚,您必须区分

visit '/'
click_button 'Login'

visit '/'
click_link 'Home'

另请参阅 Capybara-Documentation GitHub

With Capybara, you should not get confused with the difference between a link (even if it looks like a button) and a button (like "submit"). You did not provide the view file contents, but I guess, you are using a button, not a link.

With capybara, you have to differentiate

visit '/'
click_button 'Login'

or

visit '/'
click_link 'Home'

See also the Capybara-Documentation at GitHub

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文