Thursday, January 21, 2021

Test Execution with Docker and Container

In this blog, we will see what is docker and container and how it helps in automation script execution.


What is a Docker?

Docker is a computer program that performs operating-system-level virtualization.

Docker is a tool designed to make it easier to create, deploy and run applications by using containers.

Docker is a software containerization platform, meaning you can build your application, package them along with their dependencies into a container and then these containers can be easily shipped to run on other machines.


What is Container and Image?

An image is a lightweight, standalone, executable package of software that includes everything needed to run an application, system tools, system libraries and settings

Container is the runtime instance of an image.


Difference between Containers and Virtual machines?

Virtualization is the technique of importing a Guest operating system on top of a Host operating system
Containerization is however more efficient because there is no guest operating system here and utilizes a host's operating system, share relevant libraries and resources as and when needed unlike virtual machines
Containers are lightweight and faster than virtual machines.


Why Docker and Container based environment for executing automation scripts?

It’s much quicker to get up and running using the pre-made containers than to try and set Selenium up from scratch. You don’t need to install Java.

You don’t need to install all the necessary browsers. Perhaps you don’t want to install Firefox or want to test with a specific older build of Chrome or one with specific plugin or capabilities?

Selenium can (and likely will) crash on you. Using containers means you can spin up a new Selenium instance when you need it, discard and then start fresh. Alternatively, if you do plan on leaving it running for extended periods of time, if/when it crashes you can just set to reboot.

Team members can use the same container regardless of what operating system they use so there are fewer discrepancies between environments. 

Installing Docker and Containers -

System Requirements

  • Windows 10 64-bit: Pro, Enterprise, or Education (Build 16299 or later).
  • For Windows 10 Home, see Install Docker Desktop on Windows Home.
  • Hyper-V and Containers Windows features must be enabled.
  • The following hardware prerequisites are required to successfully run Client Hyper-V on Windows 10:
  • 64 bit processor
  • 4GB system RAM
  • BIOS-level hardware virtualization support must be enabled in the BIOS settings.
Software Requirements

  • Docker - Download and install from "https://hub.docker.com/editions/community/docker-ce-desktop-windows". Once the docker hub is downloaded and install successfully go the command prompt and run command 
  • For web base test automation you will need to install Docker image for selenium grind. Go to "https://hub.docker.com/" and search for selenium in the search option. Download the selenium selenium/standalone-chrome. Make sure to download the one provided by Selenium. We can also download the same using docker command "docker pull selenium/standalone-chrome:<version#>". This command can be executed when docker is up and running. You can mention command as "docker pull selenium/standalone-chrome:latest" to get the latest version of selenium standalone for chrome Refer https://github.com/SeleniumHQ/docker-selenium 
  • Eclipse or similar IDE with TestNG plugin 
  • Plugin and dependencies for java-maven project


















 

To get Standalone Server Up and Running


   $ docker run -d -p 4444:4444 selenium/standalone-chrome:<v#>
        Note: v# is for version you want to download
  • -d runs the container in the background (detached)
  • -p 4444:4444 maps the local port 4444 to the port 4444 used by    the Selenium Server in the container
  • :latest is tag/version of the image to use
The above will get the latest released image for the Chrome Standalone Server. Alternatively, you can specify a specific version by using the relevant tag. For example:
    $ docker run -d -p 4444:4444 selenium/standalone-chrome:<v#>
        Note: v# is for version you want to download

After the pre-requisites are fulfilled, follow below steps

  • Create dockerUp.bat file to start docker.
  • Add below code in the batch file
     docker-compose -f docker-compose.yaml up >>output.txt
    
All the logs will be stored in output.txt file. Make sure the location of .yaml and .txt file is same
  • Create dockerDown.bat file to stop docker
     docker-compose -f docker-compose.yaml down

Using your preferred browser, go to http://localhost:4444/grid/console to bring up the console and confirm that you do, in fact, have a grid set up and running with single Chrome node.




If you want to add more nodes to your grid you can simply repeat the commands to add individual nodes as needed. For example:

$ docker run -d --link selenium-hub:hub selenium/node-chrome:latest
$ docker run -d --link selenium-hub:hub selenium/node-chrome:latest
$ docker run -d --link selenium-hub:hub selenium/node-chrome:latest

  • Create startDocker.java file. This code will be used to run the dockerUp.bat and dockerDown.bat files from our IDE before with out API or Selenium automation script so the automation scripts can be executed in docker-container.

   startDocker.java 




  • Create stopDocker.java file. This script will be used to run the  dockerDown.bat files from our IDE after our Selenium automation script so the docker and container are close successfully.

 stopDocker.java




Now with TestNG plugin you can run your automation scripts using @before annotation for above stateDocker.java and once the tests are executed you can use the @after annotation for your stopDocker.java to release all the resource utilized in the execution. All these flow can be easily managed / controlled with testng.xml 


Please feel free to utilize and share above contents.


2 comments: