Read more

Chromedriver: Connect local chromedriver with docker

Emanuel
June 29, 2023Software engineer at makandra GmbH

Debugging your integration tests, that run a headless Chrome inside a docker image, is tricky.

Illustration UI/UX Design

UI/UX Design by makandra brand

We make sure that your target audience has the best possible experience with your digital product. You get:

  • Design tailored to your audience
  • Proven processes customized to your needs
  • An expert team of experienced designers
Read more Show archive.org snapshot

In many cases you can connect your Chrome to a remote docker container like docker-selenium Show archive.org snapshot , which should be the preferred way when you try to inspect a page within your integration test.

Otherwise you might be able to start your docker container with --net=host and access your local chromedriver in the host address space host.docker.internal.

If both options above don't work for you here is a quick instruction, how you can bind a local chromedriver to your integration test via ssh. The setup assumes:

  • You have a docker image running your integration test and a docker image running the headless Chrome
  • You have an env variable CHROMEDRIVER_URL that defines the host and port of the docker image running the headless Chrome (e.g. chrome:4444)
  • The integration test is testing a Rails application (but it could be any other application, too)
  1. Start your integration test docker container and connect with it
apt update
apt-get install openssh-server -y
apt-get install vim
vim /etc/ssh/sshd_config # Edit `PermitRootLogin yes` and `GatewayPorts yes`
service ssh start
  1. Start a local chromedriver on your host machine
chromedriver # Note the chromedriver port number in the output
docker ps # Note the container name or ID
docker exec -itu 0 container_name_or_id passwd # Change the password to be able to connect via SSH later
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name_or_id # Not the docke image IP address
ssh root@ip_address -R 127.0.0.1:9999:localhost:chromedriver_port_number # Bind your local chromedriver port to port 9999 inside the docker container
  1. Now back inside the shell of the integration test container you can run your tests
export CHROMEDRIVER_URL=http://localhost:9999
bundle exec rspec specs/system/test_spec.rb # You might need to set remove `--headless` from your chromedriver options in e.g. selenium to see a browser window opening
Posted by Emanuel to makandra dev (2023-06-29 11:31)