Search code examples
pythonseleniumdockerbrowsermobbrowsermob-proxy

Running browsermob with selenium grid on docker


I am using the python client for browsermob to record traffic of my selenium tests. Selenium grid is in a docker container with images for chrome and firefox. I cant seem to configure the docker images properly to connect to the proxy and the grid. Here is the code that I use to create the proxy and the remote web driver:

server = browsermobproxy.Server('mylocalpathtobrowsermobbin')
server.start()
proxy = server.create_proxy()
proxy.new_har()
driver = webdriver.Remote(
                command_executor='http://127.0.0.1:4444/wd/hub',
                desired_capabilities={
                    'browserName': 'chrome',
                    'chromeOptions': {
                        'args': ["--proxy-server={}".format(proxy.proxy)]}
                    })

And this is my docker-compose file:

hub:
  image: selenium/hub
  ports:
     - "4444:4444"
chrome:
  image: selenium/node-chrome-debug
  volumes:
    - /dev/shm:/dev/shm
  links:
    - hub
  ports:
    - "5900:5900"

I am new to docker, I understand that I need to expose the port that the proxy uses to connect but I cannot get it working. Any help is appreciated, thanks!


Solution

  • To Answer my own question based on the response from Sergey: I pushed a browsermob-proxy image to docker hub: https://hub.docker.com/r/spothero/browsermob-proxy/

    created from this repository: https://github.com/sskorol/docker-browsermob-proxy

    All credit goest to Sergey for the docker file.

    My docker compose file:

    hub:
      image: selenium/hub
      ports:
         - "4444:4444"
    firefox:
      image: selenium/node-firefox
      links:
        -  hub
    chrome:
      image: selenium/node-chrome-debug
      volumes:
        - /dev/shm:/dev/shm
      links:
        - hub
      ports:
        - "5900:5900"
    browsermobproxy:
      image: spothero/browsermob-proxy:1.0.0
      ports:
        - "9090-9191:9090-9191"
      expose:
        - "9090-9191"
      links:
        - hub
        - firefox
        - chrome
    

    On the jenkins job I have a shell step:

    #!/bin/bash
    docker-compose up -d --force-recreate
    sleep 10s
    
    PROXY_IP_ADDRESS="$(docker inspect --format {{.NetworkSettings.IPAddress}} browsermobproxy_1)"
    export BROWSERMOB_CONTAINER_HOST=$PROXY_IP_ADDRESS
    

    I use an environment variable to pass the hosts to my test code. Here is the code that initializes the webdriver with the proxy:

    
        import browsermobproxy
        self.client = browsermobproxy.Client('localhost:9090')
        self.driver = webdriver.Remote(
            command_executor=settings.SELENIUM_GRID_HUB,
            desired_capabilities={
                'browserName': 'chrome',
                'chromeOptions': {
                    'args': ["--proxy-server={}".format(
                        os.environ.get('BROWSERMOB_CONTAINER_HOST'), self.client.port)]
                }
            })
    
    

    Hope this helps!