How to debug tests running on Docker containers

The Blog To Learn Selenium and Test Automation

How to debug tests running on Docker containers

Our previous article Selenium Docker: Parallel execution made easy explains how to execute selenium tests in Docker.

Important point to note and understand – the test scripts are running in local machine or invoked from local machine. docker container provides only the browser

The setup explained is good for production. However during development of test scripts to automate test cases, you may want to debug step by step execution of the script. standalone-chrome-debug or standalone-firefox-debug images could be used to inspect visually what the browser is doing inside docker.

Running this debug variant node or standalone image will help us see the browser in action inside docker using VNC viewer. This debug image starts VNC server on port 5900 by default. Users can map this default port to any free external port as required.

Selenium hub is started using below command.

docker run -d -P -p "4444:4444" --name selenium-hub selenium/hub

Selenium nodes are started using below command

docker run -d -P -p 5900:5900 --link selenium-hub:hub selenium/node-chrome-debug:3.141.59-yttrium<br>

docker run -d -P -p 5901:5900 --link selenium-hub:hub selenium/node-firefox-debug:3.141.59-yttrium

Please note the chrome node image name has debug word at the end – node-chrome-debug or node-firefox-debug.

Check whether the server is running and all the nodes are connected by going to url – http://localhost:4444/grid/console, if you running docker images in your local machine. Otherwise, replace localhost accordingly.

You can acquire the port that the VNC server is exposed to by running below commands.

$ docker port <container-name|container-id> 5900
[root@allselenium ~]$ docker ps
CONTAINER ID  IMAGE  COMMAND  CREATED  STATUS  PORTS  NAMES
2afb33a0b654  selenium/node-firefox-debug:3.141.59-yttrium  "/opt/bin/entry_po…"  4 seconds ago  Up 4 seconds  0.0.0.0:5901-&gt;5900/tcp  objective_haibt
00f0b9ebddbe  selenium/node-chrome-debug:3.141.59-yttrium  "/opt/bin/entry_po…"  2 hours ago  Up 2 hours  0.0.0.0:5900-&gt;5900/tcp  zealous_wright
e4690c661f4c  selenium/hub  "/opt/bin/entry_po…"  3 hours ago  Up 3 hours  0.0.0.0:4444-&gt;4444/tcp  selenium-hub
[root@allselenium ~]$ docker port 2afb33a0b654 5900
0.0.0.0:5901

Please be careful with an unintended mouse click or keyboard interrupt when you are viewing browser in action using VNC viewer client. Select ‘view only’ option while connecting using client.

Link to install VNC viewer client – https://www.realvnc.com/en/connect/download/viewer/macos/

Once VNC viewer client is installed, you can connect by entring ip:port combination.

VNC viewer connect
VNC Viewer Connect

You may get Unencrypted connection error and this can be skipped by clicking on ‘Continue’ button.

unencrypted connection error
Unencrypted connection error

When you are prompted for the password, enter default password – secret.

VNC viewer authentication
VNC Viewer Authentication

If you want to run VNC without password authentication you can set the environment variable VNC_NO_PASSWORD=1

Run below script to test the setup and you should see browser in VNC viewer client.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

desiredCapabilities={
 "browserName":"chrome"
}
driver = webdriver.Remote(command_executor='http://localhost:4444/wd/hub', desired_capabilities = desiredCapabilities)
driver.get('https://www.google.com')
element = driver.find_element_by_link_text('Privacy')
ActionChains(driver) \
    .key_down(Keys.CONTROL) \
    .click(element) \
    .key_up(Keys.CONTROL) \
    .perform()
print(driver.title)
driver.quit()

Hope this article is informative!!. Please let us know your thoughts in comment section.

 

Leave a Reply

Your email address will not be published. Required fields are marked *