BeforeFeature/AfterFeature does not work using SpecFlow and Coded UI
Asked Answered
O

1

12

I am not able to define a [BeforeFeature]/[AfterFeature] hook for my feature file. The application under test is WPF standalone desktop applications.

If I use [BeforeScenario]/[AfterScenario] everything works fine, the application starts without any problem, the designed steps are performed correctly and the app is closed.

Once I use the same steps with [BeforeFeature]/[AfterFeature] tags the application starts and the test fails with:

The following error occurred when this process was started: Object reference not set to an instance of an object.

Here is an example:

[Binding]
public class Setup
{   
    [BeforeScenario("setup_scenario")]
    public static void BeforeAppScenario()
    {
        UILoader.General.StartApplication();
    }

    [AfterScenario("setup_scenario")]
    public static void AfterAppScenario()
    {
        UILoader.General.CloseApplication();
    }

    [BeforeFeature("setup_feature")]
    public static void BeforeAppFeature()
    {
        UILoader.General.StartApplication();
    }

    [AfterFeature("setup_feature")]
    public static void AfterAppFeature()
    {
        UILoader.General.CloseApplication();
    }
}

StartApplication/CloseApplication were recorded and auto-generated with Coded UI Test Builder:

public void StartApplication()
{
    // Launch '%ProgramFiles%\...
    ApplicationUnderTest Application = ApplicationUnderTest.Launch(this.StartApplicationParams.ExePath, this.StartApplicationParams.AlternateExePath);
}

public class StartApplicationParams
{    
    public string ExePath = "C:\\Program Files..."
    public string AlternateExePath = "%ProgramFiles%\\..."
}

Noteworthy: I'm quite new with SpecFlow. I can't figure it out why my test fails with [BeforeFeature] and works fine with [BeforeScenario].

It would be great if somebody could help me with this issue. Thanks!

Oedipus answered 7/6, 2013 at 7:37 Comment(1)
Could you also post the stack trace of the exception please? Which line is erroring / is it external code / what is the last line of your code to run?Italia
B
19

I ran into a similar problem recently. Not sure if this can still help you, but it may be of use for people who stumble upon this question.

For BeforeFeature\AfterFeature to work, the feature itself needs to be tagged, tagging just specific scenarios will not work.

Your feature files should start like this:

@setup_feature
Feature: Name Of Your Feature

@setup_scenario
Scenario: ...
Both answered 21/8, 2013 at 19:30 Comment(2)
what version of specflow this is supported? It isn't working for me on 2.4.1Azotemia
Same for me, using 2.4.1 doesn't work at all.Kendricks

© 2022 - 2024 — McMap. All rights reserved.