As you know most of my articles are about WebDriver. Today, I am going to write about a common problem that people usually hit. Have you experienced this- your tests finish and then all the sudden the browser is still opened? The next time you try to clean the build folder you cannot because the current driver's exe is still in use- such a pity, from now on your builds start failing. Many people complain about the flakiness of WebDriver and this is one of the reasons. Here I am going to propose to you a solution, as they say in Mortal Combat- "Finish Him!" (kill all of the processes).

While ago when we were working on the first version of the BELLATRIX test automation framework, I did this research while I was searching for a proper solution to the problem.

Test Code C#

Once again I will use one of my favorite test pages- SearchEngine. Below you can find the page object that we will use in the tests.

SearchEngineMainPage.Actions

SearchEngineMainPage.Elements

SearchEngineMainPage.Asserts

We use partial classes. You can read more about this modified/improved version of the pattern in my article- Page Objects with Partial Classes and Properties- WebDriver C#

Partial Page Objects in Tests

Nothing special really. We initialize the driver for each test and then perform the test case. By the way, we can reuse the browser if is started in the assembly initialize method. This way the test run's speed will be significantly improved.

However, imagine that we have instead a single test- 2000 or even more tests. Yeah. I bet that you will hit the problem I mentioned earlier. How often you will experience it, depends on the driver type you use, some are more stable than others. For example, I noticed that it occurs more often for InternetExplorerDriver and OperaDriver.

The Solution- Kill Them All

How to Use it in Tests?

Above we reuse the driver instance for all tests in the current test run. Finally, when all of them finish, we finish for good the driver. Bellow, you will find how the magic works.

DisposeDriverService- Finish Him