Start United States USA — software Want to Know How to Run Selenium Tests in Docker?

Want to Know How to Run Selenium Tests in Docker?

108
0
TEILEN

This article shows how Docker lets you run tests in containers and isolate the tests in development and deployment. Follow the tutorial below!
Join the DZone community and get the full member experience. Automation testing and Continuous Integration (CI) are integral parts of the development and test activity. Selenium test automation is one such approach that helps in the end-to-end testing of the web product. The not-so-preferred way of performing tests using the Selenium framework involves installing the required web browser and its corresponding browser drivers. In this blog, we deep dive into how to run Selenium tests in Docker in order to accelerate the Selenium test automation activity. When it comes to Selenium automation testing, it is important that a test run in one execution environment does not hinder the execution of tests run in another test environment (s). Hence, automation tests should be run in isolation, and Docker helps in realizing this ‘essential’ requirement. Running Selenium tests in Docker containers helps in performing code reviews (at a faster pace), thereby realizing better quality code, attaining high test coverage, and developing an awesome-quality product. Docker provides the flexibility to execute tests in containers or isolate the tests in development and deployment. There are umpteen reasons to use Selenium with Docker for web application testing. Docker is an open-source containerization platform that makes it easy to create, deploy, and run applications in a secure manner using containers. Docker provides virtualization at the Operating System (OS) level. All the software parts in Docker are organized in Containers. By default, Dockers come with the strongest isolation capabilities, and the applications run safely and securely in the relevant containers. Also, Docker runs as a discrete process on the host machine. Along with the flexibility to run Docker on local machines that run Windows, Mac, or Linux, it can also be run on cloud infrastructure like AWS, Azure, etc. Essentially, the Docker image contains everything that is required for running the application as a container. Docker image includes the following: The corresponding image can be deployed to any Docker environment as a Docker Container and can further facilitate running Selenium tests in Docker containers. Container in Docker is a stand unit of software that packages the code and all the required dependencies so that the application can run more quickly and reliably from one computing environment to another. As the Docker container image is a standalone, lightweight, executable software package, it is easy to install and run the relevant software (and services) using the same. To run the container, you do not need to boot up the guest operating system. The installed Docker containers can also be shared with different users so that they can quickly get started with the actual work using the Container image. Containerized software, which is available for Windows and Linux-based applications, is agnostic of the underlying infrastructure. Many open-source serverless frameworks also leverage the advantages offered by the Docker container technology. Containerization is often compared to Virtual Machines, but there is a glaring difference between both of them. Docker runs on the host operating system (e.g., Windows, Linux, etc.), and Docker containers contain the dependencies like libraries, configuration files, etc. This also includes the application itself. Docker Containers are light-weights since they do not contain any guest operating system. Shown below is a Docker container that consists of the Selenium Hub and Firefox and Chrome nodes. On the other hand, virtual machines have their own guest operating system and run a Hypervisor that is primarily responsible for running VMs. Docker also has a Hyper-V backend, but it is recommended to use WSL (Windows Subsystem for Linux) for better performance. Since VMs have their own guest OS, the size of a VM is much bigger and requires more resources to run the VM. Before running Selenium tests in Docker containers, we need to install and setup Docker on Windows. The pre-requisite step to run a Selenium test in a Docker container is installing Docker on the host machine. In our case, we would be installing Docker on a Windows machine. First, you have to download the Docker Desktop for Windows – the community version of Docker for Microsoft Windows. Docker desktop for Windows 10 is a native Windows application that provides an easy-to-use development environment for building, running, and shipping dockerized apps. It supports both Linux and Windows-based containers. In our demo, we would make use of the Linux-based containers. Double-clicking on the ‘Docker for Windows Installer’ runs the installer. Installation is pretty straightforward. Docker for Windows requires the WSL 2 (Windows Subsystem for Linux) support. Select the necessary options for enabling Hyper-V Windows Features and Installing required Windows components for WSL 2. You get the following message once the installation is successful. Prior to running Selenium tests in Docker containers, you might encounter some issues related to enabling Hyper-V and data execution protection in the BIOS when starting the Docker. The support for hardware-assisted virtualization should be first enabled in the BIOS. The set of steps would vary from one manufacturer to another but entering the BIOS and enabling Virtualization should do the trick for you. After rebooting the system, open the Performance Tab in the Task Manager, and you would see that Virtualization is enabled in the machine. Depending on the machine configuration, you can run either of the two commands on the terminal (as an Administrator) to enable Hyper-V support and data execution protection on the host machine: dism.exe /Online /Enable-Feature:Microsoft-Hyper-V /All bcdedit /set hypervisorlaunchtype auto Restart the system to apply the changes. In certain cases, you would be required to enable Hyper-V from the ‘Windows Feature.’ For doing the same, navigate to ‘Control Panel’ -> ‘Programs and Features’ -> ‘Turn Windows features On or Off’ and enable the items – Hyper-V and Windows Hypervisor Platform. Restart the machine so that changes take effect. Now the Docker instance should start successfully if WSL 2 is installed on the host machine. Though we have enabled Hyper-V on the host machine, it is recommended to use WSL 2 with Docker instead of Hyper-V. WSL 2 provides better performance than the Hyper-V backend. In case you have enabled WSL 2 support in Docker settings, but WSL 2 is not installed on the machine, you would witness the “WSL 2 is not installed” error. Enable the option ‘Windows Subsystem for Linux’ in the Windows feature section in Control. As stated in the WSL installation guide for Windows 10, first, we have to enable the Windows Subsystem for Linux. This can be done by running the following command on the terminal (as an Administrator): Before installing WSL 2, you might need the Virtual Machine feature (which is an optional step). Download the WSL2 Linux kernel update package for the x64 machine and install it on the host machine. Set WSL 2 as the default version using the command: wsl –set-default-version 2 Restart the machine for the changes to take effect. With this, you are all set to use the Docker Desktop with WSL 2 backend. In the Docker Desktop settings, enable the option ‘Use the WSL 2 based engine’ so that WSL 2 is used instead of Hyper-V backend. Run the command docker – version to check whether Docker is installed and the instance has started running: In this tutorial on how to run Selenium tests in Docker, we will see some of the major benefits that make Selenium with Docker an ideal combination for web app testing: Scalable and Reliable In a normal Selenium Grid setup, the configuration involves hosting multiple VMs (Virtual Machines) as ‘Nodes’ and connecting the ‘Nodes’ to a single ‘Hub.’ When setting up the Selenium Grid, you have to download the Selenium Server jar (with Selenium 3) and run the same on the machine used to set up the Grid. Apart from this, the browsers and their respective browser drivers should be available on VMs (or Machines) that act as Selenium Nodes. This approach is neither scalable nor reliable, as the infrastructure would require continuous maintenance and up-gradation to ensure that it is usable from a test point of view. Also, running Selenium Grid adds to unnecessary computing overhead. Docker images that are used for cross browser testing with Selenium contain several nodes in a single image. Also, Docker images share certain system resources, thereby leading to less resource utilization when compared to virtual machines. Test locally hosted sites. Docker containers have options to access local development sites. In case you are using Windows or Linux, using the option –host when running the Docker containers helps in testing locally hosted sites. Less overhead of installations. When using Selenium with Docker, there is no need to install the necessary browsers and browser drivers on the host machine. Overall, it is significantly quicker to get started with Selenium web automation testing using pre-made Docker containers than installing & setting up the Selenium Grid. You can term it as ‘minimal installation and maximum benefits’! A range of Docker images (with Selenium) on the Docker Hub can be used by running a few commands on the terminal. Some of these images on the Docker Hub were developed and maintained by Selenium HQ. Here is a broad category of images that are available on the Docker Hub and used for web automation testing: Above all, the Base image is used for building your own images. Later in the blog, we would demonstrate how to run selenium tests in Docker using a standalone container and a grid of containers. Improved Security. Docker is secure and portable, and the Dockers containers run in an isolated environment. Selenium tests can be executed in a much more secure manner with the Docker container when compared to the local Selenium Grid. For improved scalability, Selenium tests on Docker can be in the subsections below. Lesser chances of discrepancies The installed Docker container can be shared with other users, and the behavior remains unchanged irrespective of the underlying environment. For example, tests conducted on a Widows container would work seamlessly on a Linux-based container. Hence, all the team members could make use of the same container, irrespective of the operating system in use. No more Selenium Crash worries If you are a local Selenium Grid, there is a high probability of witnessing a Selenium crash. It could be annoying, and the only solution is restarting the Grid so that the required nodes can connect to the Grid. With the Docker container, you can leave the crash worries at bay since it is just about discarding the old instance and spinning up a new Selenium instance when needed.

Continue reading...