C# 在SpecFlow中使用SequenceSteps运行钩子文件

C# 在SpecFlow中使用SequenceSteps运行钩子文件,c#,automated-tests,bdd,specflow,C#,Automated Tests,Bdd,Specflow,我正在尝试用SpecFlow和TestStackWhite编写简单的测试,我已经创建了Hook文件来保存我的beforecasenario和AfterScenario定义。问题是,当我尝试运行我的测试时,它失败了。当我将场景前的代码部分从Hook文件移动到TestSequenceSteps.cs[给定(@“我已打开测试”)]中时,一切都很好。 如何正确引用钩子文件 TestSequence.feature Feature: TestSequence Run full Test a

我正在尝试用SpecFlow和TestStackWhite编写简单的测试,我已经创建了Hook文件来保存我的beforecasenario和AfterScenario定义。问题是,当我尝试运行我的测试时,它失败了。当我将场景前的代码部分从Hook文件移动到TestSequenceSteps.cs
[给定(@“我已打开测试”)]
中时,一切都很好。 如何正确引用钩子文件

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();
        }
    }
}
使用系统;
使用TechTalk.SpecFlow;
使用TestStack.White;
使用TestStack.White.ui项;
使用TestStack.White.UIItems.Finders;
使用NUnit.Framework;
使用TestStack.White.UIItems.WindowItems;
使用TestStack.White.UIItems.TabItems;
使用系统线程;
使用系统诊断;
使用TestStack.White.Factory;
使用System.Linq;
命名空间Test.step定义
{
[有约束力]
公共类TestSequenceSteps
{
私有静态窗口;
私有静态应用程序;
[给定(@“我已打开测试”)]
public void GivenIHaveOpenedTest()
{
}
[当(@“我按下测试按钮”)]
按TestButton()时公共无效
{
var button=window.Get(SearchCriteria.ByClassName(“TButton”);
按钮。单击();
}
[当(@“我按下继续按钮”)]
按ContinueButton()时公共无效
{
var按钮=window.Get(SearchCriteria.ByText(“ContinueButton”);
睡眠(700);
按钮。单击();
}
[然后(@“运行测试序列”)]
public void then testsequenceisrun()
{
//ScenarioContext.Current.Pending();
}
}
}

访问TestSequenceSteps中的窗口字段时会出现NullRef异常,因为从未分配该字段。 不能使用静态字段在步骤类之间共享状态

要共享此状态,我将使用上下文注入()。
应用该模式后,代码如下所示:

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();
    }
}
公共类共享状态
{
公共窗口{get;set;}
公共应用程序应用程序{get;set;}
}
[有约束力]
公开课1
{
私人共享状态(u SharedState);;
公共挂钩1(SharedState SharedState)
{
_sharedState=sharedState;
}
[在场景(“运行”)之前]
场景()之前的公共无效
{
_sharedState.Application=Application.AttachOrLaunch(新的ProcessStartInfo(“C:\\ProgramFiles(x86)\\Tests\\directory\\Test.exe”);
_sharedState.Window=_sharedState.Application.GetWindow(SearchCriteria.ByClassName(“TGUI”),InitializeOption.NoCache);
}
[赛后]
赛后公众无效()
{
_sharedState.Window.Close();
}
}
[有约束力]
公共类TestSequenceSteps
{
私人共享状态(u SharedState);;
公共TestSequenceSteps(SharedState SharedState)
{
_sharedState=sharedState;
}
[给定(@“我已打开测试”)]
public void GivenIHaveOpenedTest()
{
}
[当(@“我按下测试按钮”)]
按TestButton()时公共无效
{
var button=_sharedState.Window.Get(SearchCriteria.ByClassName(“TButton”);
按钮。单击();
}
[当(@“我按下继续按钮”)]
按ContinueButton()时公共无效
{
var button=_sharedState.Window.Get(SearchCriteria.ByText(“ContinueButton”);
睡眠(700);
按钮。单击();
}
[然后(@“运行测试序列”)]
public void then testsequenceisrun()
{
//ScenarioContext.Current.Pending();
}
}

访问TestSequenceSteps中的窗口字段时会出现NullRef异常,因为从未分配该字段。 不能使用静态字段在步骤类之间共享状态

要共享此状态,我将使用上下文注入()。
应用该模式后,代码如下所示:

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();
    }
}
公共类共享状态
{
公共窗口{get;set;}
公共应用程序应用程序{get;set;}
}
[有约束力]
公开课1
{
私人共享状态(u SharedState);;
公共挂钩1(SharedState SharedState)
{
_sharedState=sharedState;
}
[在场景(“运行”)之前]
场景()之前的公共无效
{
_sharedState.Application=Application.AttachOrLaunch(新的ProcessStartInfo(“C:\\ProgramFiles(x86)\\Tests\\directory\\Test.exe”);
_sharedState.Window=_sharedState.Application.GetWindow(SearchCriteria.ByClassName(“TGUI”),InitializeOption.NoCache);
}
[赛后]
赛后公众无效()
{
_sharedState.Window.Close();
}
}
[有约束力]
公共类TestSequenceSteps
{
私人共享状态(u SharedState);;
公共TestSequenceSteps(SharedState SharedState)
{
_sharedState=sharedState;
}
[给定(@“我已打开测试”)]
public void GivenIHaveOpenedTest()
{
}
[当(@“我按下测试按钮”)]
按TestButton()时公共无效
{
var button=_sharedState.Window.Get(SearchCriteria.ByClassName(“TButton”);
按钮。单击();
}
[当(@“我按下继续按钮”)]
按ContinueButton()时公共无效
{
var button=_sharedState.Window.Get(SearchCriteria.ByText(“ContinueButton”);
睡眠(700);
按钮。单击();
}
[然后(@“运行测试序列”)]
public void then testsequenceisrun()
{
//ScenarioContext.Current.Pending();
}
}

错误是什么?它找不到按钮,下面是我按测试按钮->错误:对象引用未设置为对象实例时出现的错误。但我的应用程序已正确打开。错误是什么?它找不到按钮,以下是当我按下测试按钮->错误:对象引用未设置为对象的实例时出现的错误。但我的应用程序已正确打开。非常感谢!这是可行的,但我现在明白了什么是最重要的