Monday 4 February 2019

Zalenium – A Dockerized Selenium Grid

Over the years Selenium users have used Selenium Grid extensively to reduce the execution time, to perform cross browser testing, to perform multiple OS testing. Although grid provides us all these benefits, they come with their own cost. If you look at the issues that we are facing now with grid are:

                           
  • Have a stable grid to run UI tests with Selenium
  • Maintain it over time (keep up with new browser, Selenium and drivers versions)
  • For longer executions Java process will run out of memory.
  • Restarting the nodes in case of issues
  • Scaling up for more tests is tedious.

Zalenium provides the solution for above problems. It is a docker-selenium based solution which can start a Selenium Grid in seconds, a grid that scales up and down dynamically to run your tests in Firefox and Chrome. It works out of the box in docker and Kubernetes. Zalenium also provides the video recordings of your tests and live preview of your test execution.

Zalenium Setup:


1. Make sure that the latest Docker is up and running. (check here for instructions to install  and run)

2. Open command prompt/ terminal and run the below commands to pull the Docker images from Docker hub.
  • docker pull elgalu/selenium
  • docker pull dosel/zalenium

3. start the Zalenium-selenium-grid by executing the below command
  •     Mac:
 docker run --rm -ti --name zalenium -p 4444:4444 -p 5555:5555 \  
 -v /var/run/docker.sock:/var/run/docker.sock \  
 -v /tmp/videos:/home/seluser/videos \  
 dosel/zalenium start  
  •      Windows:
 docker run --rm -ti --name zalenium -p 4444:4444 -p 5555:5555 -v /var/run/docker.sock:/var/run/docker.sock -v /tmp/videos:/home/seluser/videos --privileged dosel/zalenium start  

4. Zalenium grid is ready now for the execution. Initially zalenium creates one chrome and one firefox container. If you need more containers, we have to specify them as parameters like below while launching our grid.

 docker run --rm -ti --name zalenium -p 4444:4444 -p 5555:5555 -v /var/run/docker.sock:/var/run/docker.sock -v /tmp/videos:/home/seluser/videos --privileged dosel/zalenium start --desiredContainers 4  

5. Now write a selenium test using remote webdriver. Look at the below script for reference.

 @Test  
 public void zaleniumDemoTest()  
 {  
 DesiredCapabilities capability = DesiredCapabilities.chrome();  
 WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capability);  
 driver.get("https://<www.yoursite.com>/");  
 driver.manage().window().maximize();  
 Thread.sleep(3000);  
 driver.quit();  
 }  

In above script, we need to specify the IP and port address of the machine where Zalenium is running as URL.

6. If you run the test now, it will execute in zalenium-grid.

Live Preview:

As discussed above, Zalenium provides live preview of your execution.  You can watch your test execution live once your tests are triggered. You can access the live preview from the below link:

To Auto-refresh, add ?refresh=numberOfSeconds to refresh the view automatically.

E.g. http://localhost:4444/grid/admin/live?refresh=20 will refresh the page every 20 seconds.

DashBoard:

  • Zalenium also provides very useful dashboard for monitoring the test results. From this dashboard you can replay those recorded videos. Below is the link for the dashboard.

              http://localhost:4444/dashboard/#

         

  • You can replace localhost for the IP/machine name where Zalenium is running
  • Click on Cleanup to remove all videos and logs from the local drive and the dashboard.Also reset the dashboard via http://localhost:4444/dashboard/cleanup?action=doReset or cleanup via http://localhost:4444/dashboard/cleanup?action=doCleanup

To stop the docker containers spun through Zalenium, simply exit Zalenium grid using the below command

 docker stop zalenium.  

Integrating with Cloud Solutions:

Zalenium usually creates containers only with Chrome and firefox. If you want to execute your tests in any other browser or any other platform, you can still use other cloud based platforms such as Saucelabs, Browser stack. Zalenium enables you to integrate these platforms with the existing zalenium-grid.

Below command helps to integrate with Saucelabs:
 export SAUCE_USERNAME=[your Sauce Labs username]  
 export SAUCE_ACCESS_KEY=[your Sauce Labs access key]  
 docker run --rm -ti --name zalenium -p 4444:4444 -p 5555:5555 \  
     -e SAUCE_USERNAME -e SAUCE_ACCESS_KEY \  
     -v /tmp/videos:/home/seluser/videos \  
     -v /var/run/docker.sock:/var/run/docker.sock \  
     dosel/zalenium start --sauceLabsEnabled true  

You can visit Zalenium Site for more details.