하나의 기능 파일에 여러 개의 시나리오를 프로그래밍하고 있습니다. 하나의 큰 Steps 파일을 사용하여 시나리오 단계를 코드에 바인딩합니다.C# SpecFlow BeforeScenario 후크
예를 들어 제 기능 파일은 다음과 같다 :
Feature: Feature1
Description of feature1
@TagA @TagB
Scenario: Scenario1
Given Some form
When I press the submit button
Then I end up at a page
@TagA
Scenario: Scenario2
Given Some form
When I press cancel
Then I should end up at a different page
그리고 내 단계 파일은 다음과 같다 :
내 단계 파일이 주어진 몇 가지 일반적인을 가진 기본 클래스에서 상속using System;
using TechTalk.SpecFlow;
namespace UpdateServer.AcceptanceTests.StepFiles
{
[Binding]
[Scope(Feature = "Feature1")]
public class Feature1Steps : SeleniumTestsBaseClass
{
[BeforeScenario("TagA")]
public void BeforeScenarioTagA()
{
// prepare some stuff
}
[BeforeScenario("TagB")]
public void BeforeScenarioTagB()
{
// prepare some other stuff
}
[Given(@"Some form")]
public void GivenSomeForm()
{
// navigate to form
}
[When(@"I press the submit button")]
public void WhenIPressTheSubmitButton()
{
// press submit button
}
[When(@"I press cancel")]
public void WhenIPressCancel()
{
// press cancel button
}
[Then(@"I end up at a page")]
public void ThenIEndUpAtAPage()
{
// check url
}
[Then(@"I should end up at a different page")]
public void ThenIShouldEndUpAtADifferentPage()
{
// check url
}
}
}
공지 사항 , when, then bindings 그리고 Webdriver를 닫는 AfterScenario.
이제 시나리오 2를 실행하면 TagB의 BeforeScenario도 실행됩니다. OfCourse이 동작을 원하지 않습니다. 왜 SpecFlow가이 컨텍스트에서 TagB 코드를 실행합니까? 어떻게 그 일을 막을 수 있습니까?
편집은
SeleniumTestsBaseClass이 기본 클래스의 생성자에 셀레늄 WebDriver를 초기화하는 데 사용됩니다. 이것이 모든 단계 클래스가 기본 클래스에서 상속되기 때문에 범위가 단계 클래스에 추가 된 이유입니다. 이 범위 특성을 제거하면 WebDriver가 하나의 테스트 만 실행 중일 때도 모든 단계 파일에 대해 열립니다.
자주 묻는 질문 : WebDriver를 열 필요가 없는데도 WebDriver를 초기화하는 가장 좋은 장소는 무엇입니까?
왜 스텝 클래스를 'Feature1'으로 범위를 지정 했습니까? 만약 당신이 그것을 제거하면 당신은 단지 'BeforeScenarioTagA' 시나리오 2에서 실행됩니다 믿습니다. 그러나 특정 이유에 대해 단계 클래스를 기능에 적용했는지 여부는 알 수 없으므로 유효한 대답인지 여부는 알 수 없습니다. –
피쳐 결합 단계 정의는 반 패턴 btw입니다. - https://github.com/cucumber/cucumber/wiki/Feature-Coupled-Step-Definitions-%28Antipattern%29 –