4

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!

eduardoreynoso
  • 210
  • 2
  • 9

2 Answers2

3

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!

eduardoreynoso
  • 210
  • 2
  • 9
  • Hi @eduardoreynoso, i have used above mentioned dockerfile. Using this I am able to start browsermob and grid. However when I tried ur code to initialise webdriver its failing. As i am providing ip of browsermob docker container instead of localhost. Can you please let me know how to fix this? – Sachin Nikam Apr 09 '19 at 11:33
2

You have to raise BMP in a container as well. And then link it with a grid. Check this article to get the idea and key implementation / configuration points.

Sergey Korol
  • 740
  • 5
  • 14
  • 1
    Thanks Sergey, I used the article and the link to your implementation I managed to get it working, once I have a minute I will post a full answer for other people to see. Thanks again! – eduardoreynoso Sep 06 '17 at 21:54