SpecFlowOSS / SpecFlow

#1 .NET BDD Framework. SpecFlow automates your testing & works with your existing code. Find Bugs before they happen. Behavior Driven Development helps developers, testers, and business representatives to get a better understanding of their collaboration
https://www.specflow.org/
Other
2.24k stars 754 forks source link

IDisposable is not called after scenario completion with Specflow.Autofac #2512

Closed gkalnytskyi closed 2 years ago

gkalnytskyi commented 2 years ago

SpecFlow Version

3.9.22

Which test runner are you using?

NUnit

Test Runner Version Number

4.0.0

.NET Implementation

.NET 5.0

Project Format of the SpecFlow project

Sdk-style project format

.feature.cs files are generated using

SpecFlow.Tools.MsBuild.Generation NuGet package

Test Execution Method

Visual Studio Test Explorer

SpecFlow Section in app.config or content of specflow.json

N/A

Issue Description

I am trying to figure out why Dispose() method of IDisposable is not executed after scenario has completed when Autofac is used as DI container for Specflow (with Specflow.Autofac package). This causes Chrome Driver process to keep running even after execution of scenario has completed.

This behavior only happens when BrowserDriver class (as described in Specflow documentation) is registered as SingleInstance() in container builder method (see). On the other hand, Dispose is invoked and Chrome Driver is successfully closed when BrowserDriver is registered as InstancePerLifeTimeScope.

My observations tell me that new container builder is created for each of the scenarios, also Autofac documentation states that it should be calling Dispose() method when the IDisposable dependency is no longer in use (see). So, there shouldn't be any notable differences between SingleInstance() and InstancePerLifetimeScope() in context of Specflow.

The other point is that Specflow documentation recommends using SingleInstance() lifetime for step classes, which might "force" people to make lifetime of BrowserDriver also to be SingleInstance(), as it would be captured anyway, thus causing the number of chromedriver processes to multiply.

Am I misunderstanding anything about IDisposable pattern, lifetimes, Autofac container behavior, or Specflow.Autofac registration?

Steps to Reproduce

See attached repository with project to reproduce (DriverLifetime tag has the most succinct example).

  1. Register BrowserDriver class as SingleInstance().
  2. Run NavigateSelectBuyAndCheck test in Visual Studio.
  3. In Task Manager on Details tab Observe that chromedriver.exe process is still running.

Link to Repro Project

https://github.com/gkalnytskyi/BrowserStackDemoTest

rossmasday commented 2 years ago

@gkalnytskyi I noticed this in my PR: https://github.com/SpecFlowOSS/SpecFlow/pull/2579 which should resolve this issue

gkalnytskyi commented 2 years ago

@rossmasday , I'll double check after the release. Thank you.

gkalnytskyi commented 2 years ago

Confirmed fixed in v3.9.74. Haven't checked other versions between v3.9.22 and v3.9.74.

github-actions[bot] commented 2 years ago

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.