Trying to get selenium working in rails 3 - “WebMock::NetConnectNotAllowedError”

后端 未结 8 1472
遥遥无期
遥遥无期 2020-12-13 14:10

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 r

相关标签:
8条回答
  • 2020-12-13 14:27

    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
    
    0 讨论(0)
  • 2020-12-13 14:28

    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

    0 讨论(0)
  • 2020-12-13 14:28

    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
    
    0 讨论(0)
  • 2020-12-13 14:35

    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.

    0 讨论(0)
  • 2020-12-13 14:36

    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!

    0 讨论(0)
  • 2020-12-13 14:37

    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.

    0 讨论(0)
提交回复
热议问题