Manage the Containers in Java Code

Hello, today, I am going to tell you how you can manage your Docker-based Selenium Grid containers via Java code. The main reason behind it is the need of Selenium Grid for parallel execution. In our pipeline, there’s already a Selenoid and Selenoid-UI image working but for local issues, we don’t want to keep production busy. So we created a small java class to create a local hub.

Here’s what our class does.

  • First,  we check if the container is up and running. We do this by running the “docker inspect -f {{.State.Running}} CONTAINERID” command. This command returns as true or false as a response. This is done by the checkAlive(String containerId) method.
  • Then if the state is false, we run “docker run CONTAINERID” command to run our containers. This is done by startDocker(“”) method.
  • There’s also a method to stop the docker container. This runs “docker stop CONTAINERID”

We also have some helper methods that parse the result of command-line interfaces. The reason is that when you run some commands with the Runtime object, you need the response to understand the response.

Those are the main functions that we use in our BaseTest.

@BeforeSuite
public void BeforeSuite() {
   Docker.startDocker("selenoid");
   Docker.startDocker("selenoid-ui");
}

@AfterSuite
public static void AfterSuite(){
   Docker.stopDocker("selenoid");
   Docker.stopDocker("selenoid-ui");
}

As the methods are parameterized, you can run any Docker image without having any problem.

Whole Source Code

package Utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Docker {


   public static void startDocker(String containerId){
       if(!checkAlive(containerId)){
           runCommand("docker start "+containerId);
           System.out.println("Trying to start "+containerId+" container");
       }else
           System.out.println("Selenoid is up-and-running. I don't do nothing");
   }


   public static void stopDocker(String containerId){
       if(checkAlive(containerId)){
           System.out.println("Trying to stop "+containerId+" container");
           runCommand("docker stop "+containerId);
       } else
           System.out.println("Selenoid is not working. I don't do nothing");
   }

   private static boolean checkAlive(String containerId){
       System.out.println("Checking the container "+containerId);
       Process p = runCommand("docker inspect -f {{.State.Running}} "+containerId);
       return readCommandResult(p);
   }

   private static Process runCommand(String command){
       Process p = null;
       try {
           p = Runtime.getRuntime().exec(command);
       } catch (IOException e) {
           e.printStackTrace();
       }
       return p;
   }


   private static boolean readCommandResult(Process proc){
       String s;
       boolean result = false;
       try {
           BufferedReader stdInput = new BufferedReader(new
                   InputStreamReader(proc.getInputStream()));

           BufferedReader stdError = new BufferedReader(new
                   InputStreamReader(proc.getErrorStream()));
           try {
               Thread.sleep(5000);
           } catch (InterruptedException e) {
               e.printStackTrace();
           }

           // read the output from the command
           while ((s = stdInput.readLine()) != null) {
               System.out.println("Container is working: "+s);
               if(s.equalsIgnoreCase("true") || s.equalsIgnoreCase("false"))
                   result = Boolean.parseBoolean(s);
           }

           // read any errors from the attempted command
           while ((s = stdError.readLine()) != null) {
               System.out.println(s);
               result = false;
           }
       }
       catch (IOException e) {
           e.printStackTrace();
       }
       return result;
   }
}

Note: Also, we can use external libraries which wrap docker commands for us. For e.g., we could make use of Testcontainers to basically control the starting and stopping of a selenium grid. https://www.testcontainers.org A sample from their site that shows how to use the test containers: https://github.com/testcontainers/testcontainers-java-examples/blob/master/selenium-container/src/test/java/SeleniumContainerTest.java  As we can see from this, the test containers library abstracts out a lot of stuff for us and makes it easy to work with containers for our tests. Thanks to Krishan Mahadevan for his valuable feedback.

[fusion_widget_area name=”avada-custom-sidebar-seleniumwidget” title_size=”” title_color=”” background_color=”” padding_top=”” padding_right=”” padding_bottom=”” padding_left=”” hide_on_mobile=”small-visibility,medium-visibility,large-visibility” class=”” id=””][/fusion_widget_area]

Thanks.
Canberk Akduygu

2 thoughts on “Manage the Containers in Java Code”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.