Auto Scale Selenium Grid with Runtime Container- Zalenium
Tags: selenium, java, kubernetes, docker
Published: 2019-10-15 03:53:43

In the traditional approach where we create different containers and connect them to selenium hub, problem is if we need to create a new container node, we need to go back and connect to hub. This is time consuming process.
So here comes Zalenium, this is capable of creating node containers at run time and destroy once execution is over.
Zalenium works with Kubernetes, to get auto-scaling and auto-healing capabilities. So Zalenium use Kubernetes along with Docker. Zalenium will keep on registerting new node whenever required. We need not to manually run the image and then link it. We also need not to have any fix number of containers. So, basically Zalenium will do everything for us. It will keep our test environment quite scalable.
It can also run your test on different cloud testing providers like Sauce Labs, BrowserStack, TestingBot, CrossBrowserTesting, LambdaTest etc.
Right now, Zalenium supports only Firefox and Chrome browsers. Zalenium will also keep updating our browsers and drivers as per new releases automatically. 
By default it will register 2 nodes, and whenever you need more, it will create and destroy at runtime.

This is how to run this for the docker version.
    # Pull docker-selenium
    docker pull elgalu/selenium  
    # Pull Zalenium
    docker pull dosel/zalenium
    # Run it!
    docker run --rm -ti --name zalenium -p 4444:4444 \
      -v /var/run/docker.sock:/var/run/docker.sock \
      -v /tmp/videos:/home/seluser/videos \
      --privileged dosel/zalenium start
    # Point your tests to http://localhost:4444/wd/hub and run them
    # Stop
    docker stop zalenium

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/q3zc6h6m4xg5lsjspeds.png)

After running the commands, you can check:
The grid console.  http://localhost:4444/grid/console
The dashboard after running your first test. http://localhost:4444/dashboard
Live Preview of Execution. http://localhost:4444/grid/admin/live
In-case localhost does not work, check on what IP docker is running. In my case it is : 192.168.99.100
You can also interact with Live Previews with VNC.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/a50yxmcasb4dpnvurmuw.png)

Now, its time to execute Selenium tests. The Selenium project used for execution is available here: https://github.com/sonucogni/Zalenium

About this project:
It's very simple Selenium project. It has three classes, and we are navigating to different websites and capturing page title in those classes. We have used parallel thread count as 3, in out TestNG file. So, all three classes will be launched simultaneously. You need not to specify Driver path or any other details in the code, as Zalenium will take care for everything.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/zise952lsarndhm34pcv.png)

Since i wanted to show auto scaling feature, and by default it has created only two container so, the third one will be created at run time. Run the test as TestNG suite.

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/u4paevfr4my5kol7vt4u.png)

References: 
https://opensource.zalando.com/zalenium/
https://github.com/zalando/zalenium