SeleniumDocker Showcase

Softvisioner Sorin Veres Shares His First-Hand Tips

Bogdan

Bogdan

QA Automation at Softvision
Sorin joined Softvision in 2010 and since then he gained expertise in UI & backend automation, performance testing and build automation. He is also interested in developing his leadership skills and sharing his knowledge, leading to his new role of QA Community Lead in 2017. In his spare time, Sorin is passionate about mountain biking, hardware and traveling.
Bogdan

Latest posts by Bogdan

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

Environment

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.

Test scenario:

  • 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:

Fact code screenshot

To start execution I’m using the following shell script:

Binbash code

Save the script as: “run-tests.sh”. Here is my project folder structure:

Content of docker-compose.netcore.yml:

version 3

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:

Conclusion

Don’t forget that this feature is not officially supported. Using Docker containers will allow you to run tests on all platforms: Linux, Windows and  Mac OS. In this example C# was used but this is compatible with all languages supported by Selenium libabry (Java, Ruby, Python, JavaScript – Node). You can also enable test parallelization to reduce execution time. For Internet Explorer and Microsoft EDGE there is no support but you can use Vagrant images. If you want to use BDD for your test framework you are limited to some basic frameworks like Light BDD. Specflow doesn’t have support for .NET Core – more details here. The environment setup is easier than ever, the *.yml file contains all definitions and you can control which step to be executed. It’s easy to integrate with your CI environment and is platform independent.

Share This Article


Bogdan

Bogdan

QA Automation at Softvision
Sorin joined Softvision in 2010 and since then he gained expertise in UI & backend automation, performance testing and build automation. He is also interested in developing his leadership skills and sharing his knowledge, leading to his new role of QA Community Lead in 2017. In his spare time, Sorin is passionate about mountain biking, hardware and traveling.
Bogdan

Latest posts by Bogdan

1Comment
  • Sharada K
    Posted at 14:23h, 18 July Reply

    I was aware of only Selenium using Java, but this article  helped me to get an idea about how to perform testing with Selenium using C# and also helped me to understand about Docker.

Post A Comment