Simplifying Dependency Management with SpecFlow's IObjectContainer: Part 1
Tags: specflow, selenium, dotnetcore, automation
Published: 2023-05-29 18:33:10


## Using Concrete Class Example

**Introduction:**

In modern software development, managing dependencies is crucial for building scalable and maintainable applications. In this blog post, we will explore the importance of dependency management and how SpecFlow's IObjectContainer can streamline the process. We'll dive into your provided code example to understand why using IObjectContainer is beneficial, how to register dependencies using reflection, and the purpose of using a container for dependency registration in SpecFlow steps. While we will use a Selenium Page Object Model (POM) example for illustrating the concepts, it's important to note that the approach can be used for any type of object registration based on the testing demands.

**Why Use IObjectContainer?**

SpecFlow's IObjectContainer serves as a dependency injection container, allowing for the easy management of dependencies in your SpecFlow tests. It helps achieve loosely coupled and modular code, promoting better testability, maintainability, and extensibility. By leveraging IObjectContainer, you can centralize dependency registration and resolve dependencies effortlessly.

**How to Register Dependencies with Reflection**

In the following code example, the `SeleniumDemo.SpecFlow.Hooks.ContainerExtension` class provides an extension method called `RegisterTypes`, which facilitates the registration of concrete classes using reflection.

```csharp
public static class ContainerExtension
{
  public static void RegisterTypes<TBase>(this IObjectContainer container, params object[] args) where TBase : class
  {
    var typeOfBase = typeof(TBase);

    var derivedTypes = typeOfBase.Assembly.GetTypes()
      .Where(t => typeOfBase.IsAssignableFrom(t) && t.IsClass && !t.IsAbstract);

    Parallel.ForEach(derivedTypes, derivedType =>
    {
      var obj = Activator.CreateInstance(derivedType, args);
      container.RegisterInstanceAs(obj);
    });
  }
}
```

The `RegisterTypes` method takes a generic parameter, `TBase`, representing the base class from which the derived classes will be registered. It uses reflection to scan the assembly for types that are assignable from `TBase`, concrete, and non-abstract.

By iterating over the derived types, the `RegisterTypes` method creates an instance of each derived type with the specified arguments and registers it with the IObjectContainer using the `RegisterInstanceAs` method. This process eliminates the need for manual registration of each derived class, making it easier to maintain and extend the test suite.

**Usage of Container for Dependency Registration in SpecFlow Steps**

In SpecFlow, steps are the building blocks of behavior-driven tests. Each step definition class represents a set of steps that can be executed during a scenario. By leveraging the IObjectContainer within the `StepBase` class, you can effortlessly resolve dependencies required by these steps.

```csharp
[Binding]
public class LoginSteps : StepBase
{
  public LoginSteps(IObjectContainer container) : base(container)
  {
  }

  [Given(@"I navigate to login page")]
  public void GivenINavigateToLoginPage()
  {
    GetPage<LoginPage>().NavigateToLoginPage();
  }

  [Given(@"I provide '(.*)' and '(.*)'")]
  public void GivenIProvide(string username, string password)
  {
    GetPage<LoginPage>().Login(username, password);
  }
}
```

In the `LoginSteps` class, the constructor accepts an `IObjectContainer` parameter, allowing the container to be injected into the step definitions. This enables the use of the `GetPage<TPage>` method, which resolves instances of concrete page classes derived from `BasePage`.

By utilizing the container to resolve page dependencies, you achieve better encapsulation and separation of concerns. The step definitions remain focused on the test logic, while the container handles the instantiation and management of page objects. This promotes maintainability and reusability, as any changes to the page classes can be easily propagated through the container.

**Auto Creation and Disposal of Driver Object with Container**

In this code example, the `Hooks` class demonstrates how to leverage the IObjectContainer for automatic creation and disposal of the driver object.

```csharp
[Binding]
public class Hooks
{
  private readonly IObjectContainer _container;

  protected Hooks(IObjectContainer container)
  {
    _container = container;
  }

  [Before(Order = 0)]
  public void BrowserSetup()
  {
    var driver = Driver.CreateBrowserSession();
    _container.RegisterInstanceAs(driver);
    _container.RegisterTypes<BasePage>(_container.Resolve<IWebDriver>());
  }

  [After(Order = 99)]
  public void BrowserTearDown()
  {
    _container.Resolve<IWebDriver>().Dispose();
  }
}
```

The `BrowserSetup` method is executed before the scenarios and is responsible for creating the browser session. It calls the `Driver.CreateBrowserSession` method to obtain the driver object. The driver object is then registered with the IObjectContainer using the `RegisterInstanceAs` method, making it available for dependency resolution throughout the test execution.

The `BrowserTearDown` method is executed after the scenarios and is responsible for disposing of the driver object. It retrieves the driver object from the container using the `Resolve` method and calls the `Dispose` method to properly clean up resources.

By leveraging the IObjectContainer for driver object management, you ensure that the driver object is automatically created and disposed of, eliminating the need for manual management in each test scenario.

**Conclusion**

Dependency management is a critical aspect of software development, and SpecFlow's IObjectContainer provides a powerful tool for simplifying this process. By utilizing reflection and the container's registration capabilities, you can automate the registration of concrete classes, making your test suite more maintainable and extensible. Incorporating dependency injection into your SpecFlow steps promotes modular design and enhances collaboration between testers and developers. Embracing IObjectContainer empowers you to focus on writing expressive and efficient behavior-driven tests while enjoying the benefits of well-managed dependencies.

The Selenium Page Object Model example used in this blog post highlights how IObjectContainer can simplify dependency management for BDD approaches, but it's important to note that the approach can be applied to any type of object registration based on the testing demands. With SpecFlow and IObjectContainer, you never have to register a dependency manually again, allowing for a more efficient and seamless testing experience. Additionally, the provided code example demonstrates how to automatically create and dispose of the driver object using the IObjectContainer, further simplifying the setup and teardown process in your tests.

By embracing SpecFlow's IObjectContainer and leveraging its capabilities for dependency management, you can enhance the maintainability, extensibility, and efficiency of your behavior-driven tests.

GitHub: [Selenium Specflow DI Concrete example](https://github.com/mhossen/SpecflowDependencyDemo/tree/selenium_specflow_di_concrete)