webmock icon indicating copy to clipboard operation
webmock copied to clipboard

Allow Selenium connections

Open mzedeler opened this issue 6 years ago • 5 comments

I'm trying to test an application using both Webmock and Selenium.

In order to lock down the environment, Webmock has been set up to disallow all network traffic, including connections to localhost because I have some local services running that should be mocked as well.

The issue is that I haven't found a good way of allowing the Selenium traffic. I can't get the port number from the Selenium driver (it seems very hard to retrieve).

The ideal solution would be if Webmock could allow network requests where the headers match some pattern, so I can specify a filter for the Selenium user agent.

Configuring Webmock to not mock the specific HTTP driver that is being used by Selenium (#75) seems like a kludge because it depends on an implementation detail and I have no guarantee that I don't have other modules that uses the same driver.

mzedeler avatar Nov 20 '18 11:11 mzedeler

@mzedeler could you configure selenium to run on a fixed port number?

bblimke avatar Nov 21 '18 15:11 bblimke

No. That really isn't possible because we may have several concurrent jobs at the same server, but I found a different solution:

    def is_selenium_request(request)
      request.headers['User-Agent'].match(/^selenium\//i) || (
        # The selenium driver sends a final "shutdown" request where the user
        # agent header is wrong, so allow that too.
        request.uri.host == '127.0.0.1' &&
        request.uri.path == '/shutdown' &&
        request.headers['User-Agent'] == 'Ruby'
      )
    end

    def disable_net_connect
      WebMock::StubRegistry.instance.reset!
      WebMock.disable_net_connect!(allow_localhost: true)
      WebMock.stub_request(:any, //)
        .with { |request| !is_selenium_request(request) }
        .to_raise('Caught non-Selenium HTTP request')
    end

I couldn't find any place in the documentation where it was clear that with is actually filtering the requests (I had to look at the source), but the above works.

mzedeler avatar Nov 22 '18 10:11 mzedeler

@mzedeler Could you explain how this integrates into a system test? Are we supposed to add this to the tests that make a selenium request?

jmarsh24 avatar Feb 21 '23 09:02 jmarsh24

@mzedeler thank you for the suggested solution.

This problem keeps coming back, not only for Selenium but for other drivers used by Capybara drivers as well. I'd like to solve that. I imagine something like WebMock.allow_selenium_requests! or/and WebMock.allow_capybara_requests!. On top of that, WebMock could detect that the blocked request is from Capybara driver e.g. Selenium and suggest one of these commands to fix it.

I actually doubt anyone wants these requests ever to be blocked, therefore the next major release could actually allow these by default and instead have something like WebMock.dont_allow_selenium_requests_by_default!

bblimke avatar Feb 20 '24 10:02 bblimke

I am sorry that I didn't respond to your original question, @jmarsh24. I am currently not using WebMock and it has been a really long time since I worked with this.

mzedeler avatar Feb 20 '24 17:02 mzedeler