All Articles

How to use Selenium with Zalenium on your Mac

If you have never heard of Zalenium before: It’s a tool that helps you to run your selenium tasks in the background and records videos from your tasks. It displays the results on a dashboard and you can view your running tasks in the browser. I really like the video part, because if your selenium tasks throw an error you can rewatch them and see exactly what happened.

Zalenium

GIF from https://opensource.zalando.com/zalenium/

Unfortunately it’s a not that straight forward to use it on a Mac, because the manual was created for linux servers. This makes totally sense, because the main target of Zalenium is to use it on a server. But if you want to use it on your mac, you can easily use docker and docker-compose.

So, create a directory and a file named docker-compose.yml. Then paste the following code into the file:

version: "2.1"

services:
    zalenium:
        image: "dosel/zalenium"
        container_name: zalenium
        hostname: zalenium
        tty: true
        volumes:
            - /tmp/videos:/home/seluser/videos
            - /var/run/docker.sock:/var/run/docker.sock
        ports:
            - 4444:4444
        command: >
            start --desiredContainers 2
                  --maxDockerSeleniumContainers 8
                  --screenWidth 1024 --screenHeight 768
                  --timeZone "Europe/Berlin"
                  --videoRecordingEnabled true
                  --sauceLabsEnabled false
                  --browserStackEnabled false
                  --testingBotEnabled false
                  --startTunnel false
        environment:
            - HOST_UID
            - HOST_GID
            - SAUCE_USERNAME
            - SAUCE_ACCESS_KEY
            - BROWSER_STACK_USER
            - BROWSER_STACK_KEY
            - TESTINGBOT_KEY
            - TESTINGBOT_SECRET

If you want to change the resolution settings you can edit them on line 20. On line 13 you can also set where to store the recorded videos. To start the service you have to open your Terminal application and change to the directory where you have created your docker compose file. In the directory just type: docker-compose up

Now the service loads all the necessary images and starts after a while. The next start will be much faster. If you get any errors, please check that nothing else is running on port 4444.

After the service has started you can go to http://localhost:4444/grid/admin/live and see the running machines. To let your selenium test know that you want to use the zalenium machines, you have to set your driver to a remote driver. Here is an example in python:

chrome_settings = webdriver.DesiredCapabilities.CHROME.copy()
remote_chrome_driver = webdriver.Remote(desired_capabilities=chrome_settings)

Remote automatically uses the correct URL, but it’s import that you set the desired_capabilities parameter to the browser you want to use. After running your tests you will see the results and videos at http://localhost:4444/grid/dashboard.