Tag: Parallel

Parallel execution with Specflow 2.0 + NUnit + Selenium

In the last post we discussed how to work with Specflow 2.0 parallel execution, in this post we will discuss how to run Multiple tests in parallel in multiple browser same time using Specflow + NUnit + Selenium.

Problem with Selenium WebDriver object while running Specflow test in Parallel

Since we will be using selenium webdriver object in many Specflow step definitions and while running tests in parallel, we will encounter NullReferenceException , in order to resolve the problem, we need to use two important techniques

  • Context injection (dependency injection)
  • IOC Container

IOC Container

  • In software engineering, inversion of control (IoC) is a design principle in which custom-written portions of a computer program receive the flow of control from a generic framework.
  • A software architecture with this design inverts control as compared to traditional procedural programming: in traditional programming, the custom code that expresses the purpose of the program calls into reusable libraries to take care of generic tasks, but with inversion of control, it is the framework that calls into the custom, or task-specific, code.

-Source : Wikipedia

So how code looks like for IOC Container?

Here is the complete source code in GitHub https://github.com/executeautomation/SpecflowSeleniumParallel

Here are the complete videos of the above discussion

Video 1

Video 2

Thanks for reading the post and watching the video!!!

Please leave your comments and let me know if there is anything I should update in this post.

Karthik KK

Running Specflow test in Parallel using Specflow 2.0 and NUnit 3.x

In this post we will discuss one of the cool new feature introduced by Specflow 2.0, which is Parallel execution support out-of-box along with NUnit 3.x.

Does NUnit 3.x support parallel execution?

Yes it does, NUnit parallel execution support is discussed in greater details in Selenium automation with C# video series of ExecuteAutomation youtube channel

Parallel execution consideration

In order to achieve parallel test execution, there are some important consideration needed to be taken care before you ensure your test runs properly

Parallel execution with memory isolation (AppDomain)

  • Ensure your test code executing the application is not depends on static states (e.g. external cache, databases etc), if so then
  • This can be achieved by isolating test into AppDomain, this ensures that every test execution thread is hosted in a separate AppDomain and that each thread’s memory

Parallel execution without memory isolation (AppDomain)

  • If your tests do not depend on any static states (ie. do not store any test-specific information in static fields), you can run the tests in parallel without AppDomain isolation

What’s the code change in existing specflow code ?

It’s fairly very simple, just one line of additional change in AssemblyInfo.cs file and you are good to go and the change is this

Here is the complete video of the above discussion

Thanks for reading the post and watching the video!!!

Please leave your comments and let me know if there is anything I should update in this post.

Karthik KK

Code to run Selenium grid test on multiple docker containers

In the last post we discussed how to setup selenium grid containers with Docker, in this post we will discuss how to run test on multiple docker containers in parallel using docker command.

Here is the complete source code of this article


Sample snippet

Here is the complete video of the above discussion

Thanks for reading the post and watching the video!!!

Please leave your comments and let me know if there is anything I should update in this post.

Karthik KK

Selenium Grid Parallel test execution

In the last post we discussed how selenium grid works and how to configure selenium grid in greater detail. In this post we will move further and try to run multiple tests in parallel in different environments like

  • Different Operating systems
  • Different browsers

Distributed architecture

The distributed architecture we will work will look something like this

Parallel execution

Parallel execution of test can be achieved using testing framework TestNG

In TestNG there is an option called Parallel which you can see from here Read more

Performing Multiple Operation using Task Parallel Library instead of Threads

In this blog post I am not going to jump and start talking about Task Parallel Library (TPL), before starting that I am going to talk a little bit about threading and how to perform multiple operations using Threading via Multithreading. Later we will discuss on TPL and their relation operations.


As we all know, thread is an independent process which has its own memory and they will perform certain operation (if they are assigned to do so).

In this whole blog post, we are going to discuss on executing some query with multiple thread, so that we execute them in parallel, rather sequential.

Here is my function which will execute a query in my SQL server.

Here is the code to Create Threads. I am going to use ThreadStart delegate to perform execution of method

As you can see here if I execute the code, I will get the output as shown

But since the ExecuteNonQuery() method of SqlCommand is not Thread safe, we need to maintain thread safety.

Also, you can note that we have opened the connection of SQL server each and everytime within the method itself (ConnectDBAndExecuteQuery).

Thread Safety

There are chances that your thread may be interrupted by some other thread, hence we need to use the lock as shown, and here is the updated method.

We need to create a threadlock variable of type object 

Now we can ensure that our thread is safe from other threads.

Here is the data executed (inserted) time for both with and without thread lock result in table

As you could see for code without Thread Lock, there happened as many as 14 operation in single millisecond, whereas in code with thread lock we happened to have sporadically 11 operations in single millisecond.

Parallel Execution

I am going to do the same stuff executing them in Parallel using Task Parallel Library (TPL), but here creating and managing thread is not done by me, rather .Net and CLR will take care of it. Here is the code

As you can see in the code I am using Parallel.For to execute the ConnectDBAndExecuteQuery() method, the for loop will be iterated for the threadcount which I am passing as parameter, but here I am not creating the thread rather CLR will do instead.

Here is the output, here I have added Thread.CurrentThread.ManagedThreadId to show the ManagedThreadID

As you can see the output, most of the threads have been reused here instead of one creating newly. Here mostly thread ID with 10,11,12,13 are the threads which executed the whole operation 20 times, now the database table has these many records as shown

AS you can see, the record inserted time frame is very close to the Thread without lock we saw previous in our discussion. But the important point to note here is, the whole operation is performed by JUST 4 THREADS.

Using Parallel Programming we don’t have to care about managing threads and reusing them, since everything is taken care by .Net for us.

Limiting Parallel Threads with ParallelOptions

We can also limit the parallel threads from 4 to 2 or we can increase the same from 2 to 10 using ParallelOptions class

Here is the self-explanatory code

Now let’s change our code to have ParallelOptions in our Parallel.For method

Now the output looks like this

As you can see, only 2 threads are executing the method in parallel, which is 9 and 10.

I hope this post has given an idea on how to call multiple methods in parallel using TPL and Threads.


Karthik KK