运行 SpecFlow 中带有 SequenceSteps 的挂钩文件

Run Hook file with SequenceSteps in SpecFlow

我正在尝试在 SpecFlow 和 TestStackWhite 中编写简单的测试,我已经创建了 Hook 文件来保存我的 BeforeScenario 和 AfterScenario 定义。问题是,当我尝试 运行 我的测试失败时。当我将代码部分 BeforeScenario 从 Hook 文件移动到我的 TestSequenceSteps.cs [Given(@"I have opened Test")] 时,一切都很好。 如何正确引用Hook文件?

TestSequence.feature

Feature: TestSequence
    Run full Test
    as a test sequence

@run
Scenario: Run test sequence in Test
    Given I have opened Test
    When I press Test button
    And  I press Continue button
    Then test sequnce is run

Hooks1.cs

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using TechTalk.SpecFlow;
using TestStack.White;
using TestStack.White.Factory;
using TestStack.White.UIItems.Finders;
using TestStack.White.UIItems.WindowItems;

namespace Test.Steps
{

    [Binding]
    public sealed class Hooks1
    {
        public static Window window;
        public static Application application;

        [BeforeScenario("run")]
        public void BeforeScenario()
        {
            application = Application.AttachOrLaunch(new ProcessStartInfo("C:\Program Files (x86)\Tests\" +
            "directory\Test.exe"));
            window = application.GetWindow
                (SearchCriteria.ByClassName("TGUI"), InitializeOption.NoCache);
        }

        [AfterScenario]
        public void AfterScenario()
        {
            window.Close();
        }
    }

TestSequenceSteps.cs

using System;
using TechTalk.SpecFlow;
using TestStack.White;
using TestStack.White.UIItems;
using TestStack.White.UIItems.Finders;
using NUnit.Framework;
using TestStack.White.UIItems.WindowItems;
using TestStack.White.UIItems.TabItems;
using System.Threading;
using System.Diagnostics;
using TestStack.White.Factory;
using System.Linq;

namespace Test.StepDefinitions
{

    [Binding]
    public class TestSequenceSteps
    {
        private static Window window;
        private static Application application;

        [Given(@"I have opened Test")]
        public void GivenIHaveOpenedTest()
        {

        }

        [When(@"I press Test button")]
        public void WhenIPressTestButton()
        {
            var button = window.Get<Button>(SearchCriteria.ByClassName("TButton"));
            button.Click();
        }

        [When(@"I press Continue button")]
        public void WhenIPressContinueButton()
        {
            var button = window.Get<Button>(SearchCriteria.ByText("ContinueButton"));
            Thread.Sleep(700);
            button.Click();
        }

        [Then(@"test sequnce is run")]
        public void ThenTestSequnceIsRun()
        {
            //ScenarioContext.Current.Pending();
        }
    }
}

您在访问 TestSequenceSteps 中的 window 字段时遇到 NullRef 异常,因为此字段从未分配过。 您不能在步骤 类 与静态字段之间共享状态。

要共享此状态,我会使用上下文注入 (http://specflow.org/documentation/Context-Injection/)。
应用该模式后,您的代码将如下所示:

public class SharedState
{
    public Window Window {get;set;}
    public Application Application {get;set;}
}


[Binding]
public class Hooks1
{
    private SharedState _sharedState;

    public Hooks1(SharedState sharedState)
    {
        _sharedState = sharedState;
    }


    [BeforeScenario("run")]
    public void BeforeScenario()
    {
        _sharedState.Application = Application.AttachOrLaunch(new ProcessStartInfo("C:\Program Files (x86)\Tests\directory\Test.exe"));
        _sharedState.Window = _sharedState.Application.GetWindow(SearchCriteria.ByClassName("TGUI"), InitializeOption.NoCache);
    }

    [AfterScenario]
    public void AfterScenario()
    {
        _sharedState.Window.Close();
    }
}

[Binding]
public class TestSequenceSteps
{
    private SharedState _sharedState;

    public TestSequenceSteps(SharedState sharedState)
    {
        _sharedState = sharedState;
    }

    [Given(@"I have opened Test")]
    public void GivenIHaveOpenedTest()
    {

    }

    [When(@"I press Test button")]
    public void WhenIPressTestButton()
    {
        var button = _sharedState.Window.Get<Button>(SearchCriteria.ByClassName("TButton"));
        button.Click();
    }

    [When(@"I press Continue button")]
    public void WhenIPressContinueButton()
    {
        var button = _sharedState.Window.Get<Button>(SearchCriteria.ByText("ContinueButton"));
        Thread.Sleep(700);
        button.Click();
    }

    [Then(@"test sequnce is run")]
    public void ThenTestSequnceIsRun()
    {
        //ScenarioContext.Current.Pending();
    }
}