Run Selenium (C#) tests in a dockerized environment
Docker allows applications to run in an isolated environment with limited resources. Perhaps this is one of the reasons that made Docker so popular.
Codeship says: “The real area where Docker disrupts is in resource efficiency.” There are many tools used for virtualization but they need a lot of resources if you want to create an isolated environment.
Let’s say that you want to create 10 virtual machines. To boot this you need 10 operating systems with minimum requirements (CPU, Memory, HDD). You have to do the same settings for each VM. With Docker, you can allocate resources for a single VM and you can have a single operating system that will manage everything.
If you are interested in learning more about Docker vs. VM benchmarks, you can find more details here.
What is Infrastructure as Code?
Infrastructure as Code, or IaC, refers to the “practice of scripting the provisioning of hardware and operating system requirements concurrently with the development of the application itself.” (Source: http://rancher.com/containers-making-infrastructure-code-easier/).
With the latest version of Selenium and .NET Core, you can create a framework that is platform independent. In this short article, we will create from scratch a project using .NET Core with Selenium C# and Docker.
We will create a dockerized environment for Selenium hub (2 nodes Firefox / Chrome + hub) and another container for our solution.
Because there is no official support for .NET Core in the current release of Selenium some changes were required. More details here.
What I changed in the WebDriver DLL:
https://github.com/SeleniumHQ/selenium/commit/942b8a437bd1f646878331112828ada28588ec32 and I had to re-build the solution on my local machine. Now I’m able to use Selenium Remote WebDriver with .NET Core and I can create the environment.
We will have two docker-compose files. The first one will create the .NET Core container. The second docker-compose file will create:
- Chrome node
- Firefox node
- Selenium hub
Content of docker-compose.selenium.yml:
Build the containers using:
To verify that the containers are running:
In Visual Studio create a new XUnit Test Project (.NET Core). As I said earlier this project uses custom WebDriver DLLs. You will have to reference WebDriver and WebDriver.Support DLLs that were compiled earlier.
- Go to http://google.ro
- Verify that the search box is enabled
- Verify that the Search button is enabled
- Search for “test”
Here is the code:
To start execution I’m using the following shell script:
Save the script as: “run-tests.sh”. Here is my project folder structure:
Content of docker-compose.netcore.yml:
Build the container using:
Using VNC Viewer you can connect to your container to verify that the test is started.
To remove the container you can run: