尝试让 selenium 在 Rails 3 中工作 - “WebMock::NetConnectNotAllowedError”

发布于 2024-11-07 04:22:15 字数 6215 浏览 0 评论 0原文

我第一次尝试在 Rails 3 应用程序中使用 selenium。 (我之前在 Rails 2 中使用过它)。我在一个 rvm 中,当然它有自己的宝石集。

我首先使用水豚运行了一个任务,没有使用硒:

#./spec/requests/homepage_spec.rb
require 'spec_helper'

describe 'home page', :type => :request do
  it 'should welcome the user' do
    visit '/'
    page.should have_content('Buddy')
  end
end

运行得很好。然后我在描述选项中添加了一个“:js => true”选项,这应该使它使用selenium运行(据我所知)。并得到这个错误:

 Failure/Error: @page.start_new_browser_session
 WebMock::NetConnectNotAllowedError:
   Real HTTP connections are disabled. Unregistered request: POST http://localhost:4444/selenium-server/driver/ with body 'cmd=getNewBrowserSession&1=%2Afirefox&2=http%3A%2F%2Flocalhost%3A3000&3=&4=' with headers {'Accept'=>'*/*', 'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8', 'User-Agent'=>'Ruby'}

   You can stub this request with the following snippet:

   stub_request(:post, "http://localhost:4444/selenium-server/driver/").
     with(:body => "cmd=getNewBrowserSession&1=%2Afirefox&2=http%3A%2F%2Flocalhost%3A3000&3=&4=", 
          :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8', 'User-Agent'=>'Ruby'}).
     to_return(:status => 200, :body => "", :headers => {})

   ============================================================

这是我第一次使用水豚,我认为我的配置不知何故是错误的。也许缺少一些重要的宝石?我正在关注这篇博客文章:Capybara(和 Selenium)与 RSpec & ; Rails 3:快速教程

有什么想法吗?

PS,这是我的 Gemfile 内容:

source 'http://rubygems.org'
gem 'rails', '3.0.7'
gem 'haml'
gem 'heroku'
gem "heroku_backup_task"
gem 'authlogic', '3.0.2'
gem 'rails3-generators'
gem 'txtlocal', :git => 'git://github.com/epigenesys/txtlocal.git'
gem 'chronic'
gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'bcrypt-ruby'

group :development, :test do
  gem 'rspec'
  gem 'mocha'
  gem "rspec-rails", "~> 2.4"
  gem "webmock"
  gem 'database_cleaner', '0.5.2'  
  gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git'
  gem 'selenium-client'
end

和我的 Gemfile.lock:

GIT
  remote: git://github.com/epigenesys/txtlocal.git
  revision: 66aab6628d4385eda959e4a8bd083cc2c259fea0
  specs:
    txtlocal (0.0.1)
      json

GIT
  remote: git://github.com/jnicklas/capybara.git
  revision: 9cdf015e28637b7b0d94f5a69f9bc3605455ce22
  specs:
    capybara (1.0.0.beta1)
      mime-types (>= 1.16)
      nokogiri (>= 1.3.3)
      rack (>= 1.0.0)
      rack-test (>= 0.5.4)
      selenium-webdriver (~> 0.2.0)
      xpath (~> 0.1.4)

GEM
  remote: http://rubygems.org/
  specs:
    abstract (1.0.0)
    actionmailer (3.0.7)
      actionpack (= 3.0.7)
      mail (~> 2.2.15)
    actionpack (3.0.7)
      activemodel (= 3.0.7)
      activesupport (= 3.0.7)
      builder (~> 2.1.2)
      erubis (~> 2.6.6)
      i18n (~> 0.5.0)
      rack (~> 1.2.1)
      rack-mount (~> 0.6.14)
      rack-test (~> 0.5.7)
      tzinfo (~> 0.3.23)
    activemodel (3.0.7)
      activesupport (= 3.0.7)
      builder (~> 2.1.2)
      i18n (~> 0.5.0)
    activerecord (3.0.7)
      activemodel (= 3.0.7)
      activesupport (= 3.0.7)
      arel (~> 2.0.2)
      tzinfo (~> 0.3.23)
    activeresource (3.0.7)
      activemodel (= 3.0.7)
      activesupport (= 3.0.7)
    activesupport (3.0.7)
    addressable (2.2.5)
    arel (2.0.9)
    authlogic (3.0.2)
      activerecord (~> 3.0.7)
      activerecord (~> 3.0.7)
    bcrypt-ruby (2.1.4)
    builder (2.1.2)
    childprocess (0.1.8)
      ffi (~> 1.0.6)
    chronic (0.3.0)
    configuration (1.2.0)
    crack (0.1.8)
    database_cleaner (0.5.2)
    diff-lcs (1.1.2)
    erubis (2.6.6)
      abstract (>= 1.0.0)
    ffi (1.0.7)
      rake (>= 0.8.7)
    haml (3.1.1)
    heroku (2.1.2)
      launchy (>= 0.3.2)
      rest-client (~> 1.6.1)
      term-ansicolor (~> 1.0.5)
    heroku_backup_task (0.0.5)
      heroku (>= 1.13.7)
      rake
    i18n (0.5.0)
    json (1.5.1)
    json_pure (1.5.1)
    launchy (0.4.0)
      configuration (>= 0.0.5)
      rake (>= 0.8.1)
    mail (2.2.19)
      activesupport (>= 2.3.6)
      i18n (>= 0.4.0)
      mime-types (~> 1.16)
      treetop (~> 1.4.8)
    mime-types (1.16)
    mocha (0.9.12)
    nokogiri (1.4.4)
    polyglot (0.3.1)
    rack (1.2.2)
    rack-mount (0.6.14)
      rack (>= 1.0.0)
    rack-test (0.5.7)
      rack (>= 1.0)
    rails (3.0.7)
      actionmailer (= 3.0.7)
      actionpack (= 3.0.7)
      activerecord (= 3.0.7)
      activeresource (= 3.0.7)
      activesupport (= 3.0.7)
      bundler (~> 1.0)
      railties (= 3.0.7)
    rails3-generators (0.17.4)
      railties (>= 3.0.0)
    railties (3.0.7)
      actionpack (= 3.0.7)
      activesupport (= 3.0.7)
      rake (>= 0.8.7)
      thor (~> 0.14.4)
    rake (0.8.7)
    rest-client (1.6.1)
      mime-types (>= 1.16)
    rspec (2.6.0)
      rspec-core (~> 2.6.0)
      rspec-expectations (~> 2.6.0)
      rspec-mocks (~> 2.6.0)
    rspec-core (2.6.0)
    rspec-expectations (2.6.0)
      diff-lcs (~> 1.1.2)
    rspec-mocks (2.6.0)
    rspec-rails (2.6.0)
      actionpack (~> 3.0)
      activesupport (~> 3.0)
      railties (~> 3.0)
      rspec (~> 2.6.0)
    rubyzip (0.9.4)
    selenium-client (1.2.18)
    selenium-webdriver (0.2.0)
      childprocess (>= 0.1.7)
      ffi (>= 1.0.7)
      json_pure
      rubyzip
    sqlite3 (1.3.3)
    sqlite3-ruby (1.3.3)
      sqlite3 (>= 1.3.3)
    term-ansicolor (1.0.5)
    thor (0.14.6)
    treetop (1.4.9)
      polyglot (>= 0.3.1)
    tzinfo (0.3.27)
    webmock (1.6.2)
      addressable (>= 2.2.2)
      crack (>= 0.1.7)
    xpath (0.1.4)
      nokogiri (~> 1.3)

PLATFORMS
  ruby

DEPENDENCIES
  authlogic (= 3.0.2)
  bcrypt-ruby
  capybara!
  chronic
  database_cleaner (= 0.5.2)
  haml
  heroku
  heroku_backup_task
  mocha
  rails (= 3.0.7)
  rails3-generators
  rspec
  rspec-rails (~> 2.4)
  selenium-client
  sqlite3-ruby
  txtlocal!
  webmock

I'm trying to use selenium for the first time in a rails 3 app. (I've used it before with rails 2). I'm in an rvm which of course has its own gemset.

I first ran a task with capybara, without selenium:

#./spec/requests/homepage_spec.rb
require 'spec_helper'

describe 'home page', :type => :request do
  it 'should welcome the user' do
    visit '/'
    page.should have_content('Buddy')
  end
end

This ran fine. I then added a ":js => true" option to the options for describe, which is supposed to make it run using selenium (as far as i know). And got this error:

 Failure/Error: @page.start_new_browser_session
 WebMock::NetConnectNotAllowedError:
   Real HTTP connections are disabled. Unregistered request: POST http://localhost:4444/selenium-server/driver/ with body 'cmd=getNewBrowserSession&1=%2Afirefox&2=http%3A%2F%2Flocalhost%3A3000&3=&4=' with headers {'Accept'=>'*/*', 'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8', 'User-Agent'=>'Ruby'}

   You can stub this request with the following snippet:

   stub_request(:post, "http://localhost:4444/selenium-server/driver/").
     with(:body => "cmd=getNewBrowserSession&1=%2Afirefox&2=http%3A%2F%2Flocalhost%3A3000&3=&4=", 
          :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8', 'User-Agent'=>'Ruby'}).
     to_return(:status => 200, :body => "", :headers => {})

   ============================================================

It's my first time using capybara and i think my config is just wrong somehow. Maybe missing some essential gems? I was following this blog post: Capybara (and Selenium) with RSpec & Rails 3: quick tutorial

Any ideas?

PS, here's my Gemfile contents:

source 'http://rubygems.org'
gem 'rails', '3.0.7'
gem 'haml'
gem 'heroku'
gem "heroku_backup_task"
gem 'authlogic', '3.0.2'
gem 'rails3-generators'
gem 'txtlocal', :git => 'git://github.com/epigenesys/txtlocal.git'
gem 'chronic'
gem 'sqlite3-ruby', :require => 'sqlite3'
gem 'bcrypt-ruby'

group :development, :test do
  gem 'rspec'
  gem 'mocha'
  gem "rspec-rails", "~> 2.4"
  gem "webmock"
  gem 'database_cleaner', '0.5.2'  
  gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git'
  gem 'selenium-client'
end

and my Gemfile.lock:

GIT
  remote: git://github.com/epigenesys/txtlocal.git
  revision: 66aab6628d4385eda959e4a8bd083cc2c259fea0
  specs:
    txtlocal (0.0.1)
      json

GIT
  remote: git://github.com/jnicklas/capybara.git
  revision: 9cdf015e28637b7b0d94f5a69f9bc3605455ce22
  specs:
    capybara (1.0.0.beta1)
      mime-types (>= 1.16)
      nokogiri (>= 1.3.3)
      rack (>= 1.0.0)
      rack-test (>= 0.5.4)
      selenium-webdriver (~> 0.2.0)
      xpath (~> 0.1.4)

GEM
  remote: http://rubygems.org/
  specs:
    abstract (1.0.0)
    actionmailer (3.0.7)
      actionpack (= 3.0.7)
      mail (~> 2.2.15)
    actionpack (3.0.7)
      activemodel (= 3.0.7)
      activesupport (= 3.0.7)
      builder (~> 2.1.2)
      erubis (~> 2.6.6)
      i18n (~> 0.5.0)
      rack (~> 1.2.1)
      rack-mount (~> 0.6.14)
      rack-test (~> 0.5.7)
      tzinfo (~> 0.3.23)
    activemodel (3.0.7)
      activesupport (= 3.0.7)
      builder (~> 2.1.2)
      i18n (~> 0.5.0)
    activerecord (3.0.7)
      activemodel (= 3.0.7)
      activesupport (= 3.0.7)
      arel (~> 2.0.2)
      tzinfo (~> 0.3.23)
    activeresource (3.0.7)
      activemodel (= 3.0.7)
      activesupport (= 3.0.7)
    activesupport (3.0.7)
    addressable (2.2.5)
    arel (2.0.9)
    authlogic (3.0.2)
      activerecord (~> 3.0.7)
      activerecord (~> 3.0.7)
    bcrypt-ruby (2.1.4)
    builder (2.1.2)
    childprocess (0.1.8)
      ffi (~> 1.0.6)
    chronic (0.3.0)
    configuration (1.2.0)
    crack (0.1.8)
    database_cleaner (0.5.2)
    diff-lcs (1.1.2)
    erubis (2.6.6)
      abstract (>= 1.0.0)
    ffi (1.0.7)
      rake (>= 0.8.7)
    haml (3.1.1)
    heroku (2.1.2)
      launchy (>= 0.3.2)
      rest-client (~> 1.6.1)
      term-ansicolor (~> 1.0.5)
    heroku_backup_task (0.0.5)
      heroku (>= 1.13.7)
      rake
    i18n (0.5.0)
    json (1.5.1)
    json_pure (1.5.1)
    launchy (0.4.0)
      configuration (>= 0.0.5)
      rake (>= 0.8.1)
    mail (2.2.19)
      activesupport (>= 2.3.6)
      i18n (>= 0.4.0)
      mime-types (~> 1.16)
      treetop (~> 1.4.8)
    mime-types (1.16)
    mocha (0.9.12)
    nokogiri (1.4.4)
    polyglot (0.3.1)
    rack (1.2.2)
    rack-mount (0.6.14)
      rack (>= 1.0.0)
    rack-test (0.5.7)
      rack (>= 1.0)
    rails (3.0.7)
      actionmailer (= 3.0.7)
      actionpack (= 3.0.7)
      activerecord (= 3.0.7)
      activeresource (= 3.0.7)
      activesupport (= 3.0.7)
      bundler (~> 1.0)
      railties (= 3.0.7)
    rails3-generators (0.17.4)
      railties (>= 3.0.0)
    railties (3.0.7)
      actionpack (= 3.0.7)
      activesupport (= 3.0.7)
      rake (>= 0.8.7)
      thor (~> 0.14.4)
    rake (0.8.7)
    rest-client (1.6.1)
      mime-types (>= 1.16)
    rspec (2.6.0)
      rspec-core (~> 2.6.0)
      rspec-expectations (~> 2.6.0)
      rspec-mocks (~> 2.6.0)
    rspec-core (2.6.0)
    rspec-expectations (2.6.0)
      diff-lcs (~> 1.1.2)
    rspec-mocks (2.6.0)
    rspec-rails (2.6.0)
      actionpack (~> 3.0)
      activesupport (~> 3.0)
      railties (~> 3.0)
      rspec (~> 2.6.0)
    rubyzip (0.9.4)
    selenium-client (1.2.18)
    selenium-webdriver (0.2.0)
      childprocess (>= 0.1.7)
      ffi (>= 1.0.7)
      json_pure
      rubyzip
    sqlite3 (1.3.3)
    sqlite3-ruby (1.3.3)
      sqlite3 (>= 1.3.3)
    term-ansicolor (1.0.5)
    thor (0.14.6)
    treetop (1.4.9)
      polyglot (>= 0.3.1)
    tzinfo (0.3.27)
    webmock (1.6.2)
      addressable (>= 2.2.2)
      crack (>= 0.1.7)
    xpath (0.1.4)
      nokogiri (~> 1.3)

PLATFORMS
  ruby

DEPENDENCIES
  authlogic (= 3.0.2)
  bcrypt-ruby
  capybara!
  chronic
  database_cleaner (= 0.5.2)
  haml
  heroku
  heroku_backup_task
  mocha
  rails (= 3.0.7)
  rails3-generators
  rspec
  rspec-rails (~> 2.4)
  selenium-client
  sqlite3-ruby
  txtlocal!
  webmock

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

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

发布评论

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

评论(7

平生欢 2024-11-14 04:22:15

有了这个...

WebMock.disable_net_connect!(:allow_localhost => true)

您就可以允许真正的网络访问您的本地主机。当您需要在应用程序中使用 Selenium 并同时模拟外部资源时,它是完美的选择。

With this...

WebMock.disable_net_connect!(:allow_localhost => true)

you allow real web access to your localhost. It's perfect when you need to use Selenium for you application and, at the same time, mock external resources.

镜花水月 2024-11-14 04:22:15

这是因为你使用的是webmock。它会阻止所有出站 HTTP 请求。

如果不需要它,请将其从 Gemfile 中删除。如果您确实需要它,那么您可能需要根据您的需求更精确地配置它:

https://github.com/bblimke/webmock

It's because you are using webmock. It blocks all outbound HTTP requests.

If you don't need it, remove it from the Gemfile. If you do need it, then you may need to configure it more precisely to your needs:

https://github.com/bblimke/webmock

御弟哥哥 2024-11-14 04:22:15

我们使用它来启用水豚中的正常请求,并允许 selenium 的回调随处可见,因为它们在请求完成后被触发。

# spec/spec_helper.rb
RSpec.configure do |config|
  config.before(:all, type: :request) do
    WebMock.allow_net_connect!
  end  

  config.after(:all, type: :request) do
    selenium_requests = %r{/((__.+__)|(hub/session.*))$}
    WebMock.disable_net_connect! :allow => selenium_requests
  end
end

We use this to enable normal requests in capybara, and allow selenium's callbacks everywhere, because they are fired after requests are finished.

# spec/spec_helper.rb
RSpec.configure do |config|
  config.before(:all, type: :request) do
    WebMock.allow_net_connect!
  end  

  config.after(:all, type: :request) do
    selenium_requests = %r{/((__.+__)|(hub/session.*))$}
    WebMock.disable_net_connect! :allow => selenium_requests
  end
end
長街聽風 2024-11-14 04:22:15

改进@grosser代码

RSpec.configure do |config|
  config.before do
    WebMock.enable!
    if Capybara.current_driver != :rack_test
      selenium_requests = %r{/((__.+__)|(hub/session.*))$}
      WebMock.disable_net_connect! :allow => selenium_requests
      WebMock.disable_net_connect! :allow => "127.0.0.1:#{Capybara.current_session.driver.server_port}" # this only works for capybara selenium and capybara-webkit
    else
      WebMock.disable_net_connect!
    end
  end

  # for connections where we need to have network access we just tag it network
  config.before(:each, :network => true) do
    WebMock.disable!
  end
end

To improve on @grosser code

RSpec.configure do |config|
  config.before do
    WebMock.enable!
    if Capybara.current_driver != :rack_test
      selenium_requests = %r{/((__.+__)|(hub/session.*))$}
      WebMock.disable_net_connect! :allow => selenium_requests
      WebMock.disable_net_connect! :allow => "127.0.0.1:#{Capybara.current_session.driver.server_port}" # this only works for capybara selenium and capybara-webkit
    else
      WebMock.disable_net_connect!
    end
  end

  # for connections where we need to have network access we just tag it network
  config.before(:each, :network => true) do
    WebMock.disable!
  end
end
豆芽 2024-11-14 04:22:15

WebMock.disable_net_connect!允许_本地主机:true

WebMock.disable_net_connect! allow_localhost: true

巨坚强 2024-11-14 04:22:15

在我的具体情况下,我的问题是在 evergreen (javascript rspec) 中。

我添加:

WebMock.disable_net_connect! :allow_localhost => true

到我的environments/env*.rb,因为evergreen不加载rspec帮助程序。

In my specific case my problem was in evergreen (javascript rspec).

I added:

WebMock.disable_net_connect! :allow_localhost => true

To my environments/env*.rb because evergreen doesn't load rspec helpers.

清泪尽 2024-11-14 04:22:15

这里有好的建议。另一个更可微调的解决方案:

WebMock.disable_net_connect!(
  allow: [
    'localhost',
    'dynamo:8000',
    'pact-broker:81'
  ]
)

特别适合将 docker-compose 容器列入白名单!

Good suggestions here. Another, more fine-tuneable, solution:

WebMock.disable_net_connect!(
  allow: [
    'localhost',
    'dynamo:8000',
    'pact-broker:81'
  ]
)

Works especially well for whitelisting docker-compose containers!

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