webmock
webmock copied to clipboard
Webmock stopping selenium from deleting session in specs
Apologies if this is the wrong place to post this. Having an issue where it seems that webmock is stopping the capybara selenium driver from deleting the session. Details below.
System
- Ruby v2.6
- WebMock gem v3.18.1
- Selenium Webdriver gem v4.1.0
- RSpec gem v3.11.0
- Capybara gem v3.36.0
Summary
Whilst successfully running a suite of tests on GitLab CI[1], WebMock intercepts a request from Capybara's Selenium driver to delete a session[2].
743 examples, 0 failures, 30 pending
/builds/abelsoninfo/aws/pips/pips-console/vendor/ruby/ruby/2.6.0/gems/webmock-3.18.1/lib/webmock/http_lib_adapters/net_http.rb:104:in `request': Real HTTP connections are disabled. Unregistered request: DELETE http://selenium__standalone-chrome:4444/wd/hub/session/0146cfc5158d585f445cfcdbda289733
Although the error appears after the tests have run I wonder if it occurs whilst the specs are running? This is due to seeing newlines appearing in the RSpec output on GitLab, but not when run locally:
...................................................................................................................................................................................................................................................................... ............................................................................................................................................................................................................................... ....................................................................................................................................................................................................................................******
In spec/support/capybara.rb I have allowed connections to any URL with selenium, or session in it:
selenium_session_requests = %r{/((__.+__)|(hub/session.*))$}
allowed_connections = [
'0.0.0.0', '127.0.0.1', 'https://chromedriver.storage.googleapis.com',
'localhost', /selenium/, selenium_session_requests
].freeze
RSpec.configure do |config|
config.before(:suite) do
WebMock.enable!
end
config.after(:suite) do
WebMock.disable!
end
Capybara.app_host = "http://#{Capybara.server_host}:#{Capybara.server_port}"
Capybara.javascript_driver = ENV.fetch('CAPYBARA_JAVASCRIPT_DRIVER', :chrome_headless).to_sym
WebMock.disable_net_connect!(allow: allowed_connections, net_http_connect_on_start: allowed_connections)
end
I have previously confirmed in the before(:suite)
block that a session URL, such as http://selenium__standalone-chrome:4444/wd/hub/session/0146cfc5158d585f445cfcdbda289733, would be allowed by WebMock.
Per the suggestion in [2] I stubbed the request - though I don't believe this is desired behaviour - but that still caused the same error.
Observed behaviour
WebMock does not allow the Selenium delete the session and complete the browser exit.
Desired behaviour
The session is deleted, the browser is closed, and no errors are raised.
[1] In .gitlab-ci.yml I have a docker service to enable the use of Selenium:
image: ruby:2.6
services:
- postgres:10.1
- selenium/standalone-chrome:latest
- redis:latest
The URL for that service is exposed to my specs via an environment variable:
- export SELENIUM_REMOTE_URL="http://selenium__standalone-chrome:4444/wd/hub"
[2] Here is the full output from the WebMock error:
/builds/abelsoninfo/aws/pips/pips-console/vendor/ruby/ruby/2.6.0/gems/webmock-3.18.1/lib/webmock/http_lib_adapters/net_http.rb:104:in request': Real HTTP connections are disabled. Unregistered request: DELETE http://selenium__standalone-chrome:4444/wd/hub/session/0146cfc5158d585f445cfcdbda289733 with headers {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json; charset=UTF-8', 'User-Agent'=>'selenium/4.1.0 (ruby linux)'} (WebMock::NetConnectNotAllowedError) You can stub this request with the following snippet: stub_request(:delete, "http://selenium__standalone-chrome:4444/wd/hub/session/0146cfc5158d585f445cfcdbda289733"). with( headers: { 'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json; charset=UTF-8', 'User-Agent'=>'selenium/4.1.0 (ruby linux)' }). to_return(status: 200, body: "", headers: {}) ============================================================ from /builds/abelsoninfo/aws/pips/pips-console/vendor/ruby/ruby/2.6.0/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver/remote/http/default.rb:124:in response_for' from /builds/abelsoninfo/aws/pips/pips-console/vendor/ruby/ruby/2.6.0/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver/remote/http/default.rb:77:in request' from /builds/abelsoninfo/aws/pips/pips-console/vendor/ruby/ruby/2.6.0/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver/remote/http/common.rb:59:in call' from /builds/abelsoninfo/aws/pips/pips-console/vendor/ruby/ruby/2.6.0/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver/remote/bridge.rb:588:in execute' from /builds/abelsoninfo/aws/pips/pips-console/vendor/ruby/ruby/2.6.0/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver/remote/bridge.rb:188:in quit' from /builds/abelsoninfo/aws/pips/pips-console/vendor/ruby/ruby/2.6.0/gems/selenium-webdriver-4.1.0/lib/selenium/webdriver/common/driver.rb:181:in quit' from /builds/abelsoninfo/aws/pips/pips-console/vendor/ruby/ruby/2.6.0/gems/capybara-3.36.0/lib/capybara/selenium/driver.rb:293:in quit' from /builds/abelsoninfo/aws/pips/pips-console/vendor/ruby/ruby/2.6.0/gems/capybara-3.36.0/lib/capybara/selenium/driver.rb:512:in `block in setup_exit_handler'
@james-ai did you find a solution to this?
@madleech unfortunately not, and I haven't been given time to look into it in a while. Are you experiencing the same issue?
@madleech what is the method and host that WebMock is blocking for you? What parameters have you passed to disable_net_connect!
?
@madleech unfortunately not, and I haven't been given time to look into it in a while. Are you experiencing the same issue?
@james-ai I am. I worked around it by adding:
# Add hook to allow capybara to cleaning terminate selenium session
RSpec.configure do |config|
config.after(:suite) do
WebMock.allow_net_connect!
end
end
@madleech what is the method and host that WebMock is blocking for you? What parameters have you passed to
disable_net_connect!
?
@bblimke The error I'm getting is:
Traceback (most recent call last):
8: from /app/vendor/bundle/ruby/2.7.0/gems/capybara-3.39.2/lib/capybara/selenium/driver.rb:519:in `block in setup_exit_handler'
7: from /app/vendor/bundle/ruby/2.7.0/gems/capybara-3.39.2/lib/capybara/selenium/driver.rb:293:in `quit'
6: from /app/vendor/bundle/ruby/2.7.0/gems/selenium-webdriver-4.9.0/lib/selenium/webdriver/common/driver.rb:173:in `quit'
5: from /app/vendor/bundle/ruby/2.7.0/gems/selenium-webdriver-4.9.0/lib/selenium/webdriver/remote/bridge.rb:187:in `quit'
4: from /app/vendor/bundle/ruby/2.7.0/gems/selenium-webdriver-4.9.0/lib/selenium/webdriver/remote/bridge.rb:619:in `execute'
3: from /app/vendor/bundle/ruby/2.7.0/gems/selenium-webdriver-4.9.0/lib/selenium/webdriver/remote/http/common.rb:59:in `call'
2: from /app/vendor/bundle/ruby/2.7.0/gems/selenium-webdriver-4.9.0/lib/selenium/webdriver/remote/http/default.rb:77:in `request'
1: from /app/vendor/bundle/ruby/2.7.0/gems/selenium-webdriver-4.9.0/lib/selenium/webdriver/remote/http/default.rb:119:in `response_for'
/app/vendor/bundle/ruby/2.7.0/gems/webmock-3.18.1/lib/webmock/http_lib_adapters/net_http.rb:104:in `request': Real HTTP connections are disabled. Unregistered request: DELETE http://127.0.0.1:9515/session/e5ff6c4369da9f8ef8e81c2779191152 with headers {'Accept'=>'application/json', 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3', 'Content-Type'=>'application/json; charset=UTF-8', 'User-Agent'=>'selenium/4.9.0 (ruby linux)'} (WebMock::NetConnectNotAllowedError)
You can stub this request with the following snippet:
stub_request(:delete, "http://127.0.0.1:9515/session/e5ff6c4369da9f8ef8e81c2779191152").
with(
headers: {
'Accept'=>'application/json',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'Content-Type'=>'application/json; charset=UTF-8',
'User-Agent'=>'selenium/4.9.0 (ruby linux)'
}).
to_return(status: 200, body: "", headers: {})
I'm using this in my rails_helper
:
WebMock.disable_net_connect!(allow_localhost: true, allow: ['datadog-agent'])
On a suspicion I searched the codebase for other WebMock.disable_net_connect!
and found a couple of parameter-less examples burried in the middle of someone's spec. They're obviously overwriting the original disable_net_connect!
call hence the selenium errors.
Sigh! So @james-ai maybe check to see if there are any hidden disable_net_connect!
calls in your specs?