複数のシナリオを1つのフィーチャーファイルでプログラミングしています。 私は1つの大きな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バインディング、そしてWebDriverを終了するAfterScenario。
これで、Scenario2を実行すると、TagBのBeforeScenarioも実行されます。 もちろん、私はこの振る舞いを望んでいませんが、なぜSpecFlowはこのコンテキストでTagBコードを実行していますか?そして、それをやめさせるにはどうすればいいですか?
編集
SeleniumTestsBaseClassこの基底クラスのコンストラクタでセレンwebdriverを初期化するために使用されます。これは、すべてのステップクラスが基本クラスから継承するため、スコープがステップクラスに追加された理由です。このスコープ属性を削除すると、1つのテストだけが実行されている場合でも、そこにあるすべてのステップファイルに対してWebDriverが開かれます。
今後の質問:WebDriverを必要以上に開いてしまう危険性なしにWebDriverを初期化するのに最適な場所は何ですか?
なぜステップクラスを「Feature1」にスコープしましたか?もしあなたがそれを削除すると、あなたはScenario2で実行されている 'BeforeScenarioTagA'しか取得できないと思います。しかし、特定の理由でステップクラスをフィーチャに適用したかどうかわからないので、それが有効な答えかどうかわかりません。 –
フィーチャ結合ステップ定義は反パターンbtwです - https://github.com/cucumber/cucumber/wiki/Feature-Coupled-Step-Definitions-%28Antipattern%29 –