BeforeScenario 和 AfterScenario 挂钩在 specflow 中不起作用
BeforeScenario and AfterScenario hooks not working in specflow
我的 SeleniumSteps.cs 代码中有以下代码
我正在尝试让 AfterScenario 在调试这些测试时启动
using PrivateDomain;
using Machine.Specifications;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using TechTalk.SpecFlow;
namespace Functional.Steps
{
public class SeleniumSteps : PrivateDomain.Steps.SeleniumSteps
{
#region Hooks
[BeforeScenario]
public void Before()
{
// before
}
[AfterTestRun, Scope(Tag = null)]
public new static void AfterTestRun()
{
// after testrun
}
[AfterScenario]
public void AfterScenarioErrorScreenshot()
{
// after scenario
}
#endregion
}
}
using OpenQA.Selenium;
using TechTalk.SpecFlow;
namespace PrivateDomain.Steps
{
[Binding]
[Scope(Tag = "Selenium")]
public class SeleniumSteps
{
protected static IWebDriver webDriver;
public SeleniumSteps();
public virtual IWebDriver WebDriver { get; }
[AfterTestRun]
[Scope(Tag = null)]
public static void AfterTestRun();
[AfterScenarioAttribute(new[] { })]
public virtual void AfterScenario();
}
}
我的功能文件如下所示:
(删除了详细信息)
@Customer_Portal
Feature: Account Management - Registration
In order to create an account
As a customer
I want to register my details with the application
Scenario: Register
# Registration Form
When I navigate to "/Customer/Account/Register"
// more code...
Scenario: Required Fields
// more code...
Scenario: Invalid Contact Details
// more code...
Scenario: Insufficient Password Strength
// more code...
Scenario: Password Mismatch
// more code...
Scenario: Already Registered
// more code...
Scenario: Invalid Activation
// more code...
Scenario: Already Activated
// more code...
当我调试测试时,我可以看到调试器命中了 AfterTestRun 部分。
但是,BeforeScenario
或 AfterScenario
均未被行使。
谁能告诉我哪里做错了?
首先,正如 Sandesh 在他的回答中指出的那样,您的 SeleniumSteps
subclass 缺少 [Binding]
属性。仅在基础 class 中有 [Binding]
是不够的,您必须将它应用于每个 class 钩子方法或步骤定义(绑定),因为这就是 specflow 的方式在引擎盖下搜索钩子和绑定。它就像范围标识符。如果您错过了将 [Binding]
属性放置到 class,specflow 将不会在该 class 中搜索潜在的钩子方法或绑定。 Link 文档:https://specflow.org/documentation/Hooks/
这个link也很有用。检查 RunOfTheShipe 给出的答案:Specflow test step inheritance causes "Ambiguous step definitions"
您的 SeleniumSteps 中缺少 [Binding] 属性
namespace Functional.Steps
{
[Binding]
public class SeleniumSteps : PrivateDomain.Steps.SeleniumSteps
{
#region Hooks
[BeforeScenario]
public void Before()
{
// before
}
}
}
我的 SeleniumSteps.cs 代码中有以下代码 我正在尝试让 AfterScenario 在调试这些测试时启动
using PrivateDomain;
using Machine.Specifications;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using TechTalk.SpecFlow;
namespace Functional.Steps
{
public class SeleniumSteps : PrivateDomain.Steps.SeleniumSteps
{
#region Hooks
[BeforeScenario]
public void Before()
{
// before
}
[AfterTestRun, Scope(Tag = null)]
public new static void AfterTestRun()
{
// after testrun
}
[AfterScenario]
public void AfterScenarioErrorScreenshot()
{
// after scenario
}
#endregion
}
}
using OpenQA.Selenium;
using TechTalk.SpecFlow;
namespace PrivateDomain.Steps
{
[Binding]
[Scope(Tag = "Selenium")]
public class SeleniumSteps
{
protected static IWebDriver webDriver;
public SeleniumSteps();
public virtual IWebDriver WebDriver { get; }
[AfterTestRun]
[Scope(Tag = null)]
public static void AfterTestRun();
[AfterScenarioAttribute(new[] { })]
public virtual void AfterScenario();
}
}
我的功能文件如下所示: (删除了详细信息)
@Customer_Portal
Feature: Account Management - Registration
In order to create an account
As a customer
I want to register my details with the application
Scenario: Register
# Registration Form
When I navigate to "/Customer/Account/Register"
// more code...
Scenario: Required Fields
// more code...
Scenario: Invalid Contact Details
// more code...
Scenario: Insufficient Password Strength
// more code...
Scenario: Password Mismatch
// more code...
Scenario: Already Registered
// more code...
Scenario: Invalid Activation
// more code...
Scenario: Already Activated
// more code...
当我调试测试时,我可以看到调试器命中了 AfterTestRun 部分。
但是,BeforeScenario
或 AfterScenario
均未被行使。
谁能告诉我哪里做错了?
首先,正如 Sandesh 在他的回答中指出的那样,您的 SeleniumSteps
subclass 缺少 [Binding]
属性。仅在基础 class 中有 [Binding]
是不够的,您必须将它应用于每个 class 钩子方法或步骤定义(绑定),因为这就是 specflow 的方式在引擎盖下搜索钩子和绑定。它就像范围标识符。如果您错过了将 [Binding]
属性放置到 class,specflow 将不会在该 class 中搜索潜在的钩子方法或绑定。 Link 文档:https://specflow.org/documentation/Hooks/
这个link也很有用。检查 RunOfTheShipe 给出的答案:Specflow test step inheritance causes "Ambiguous step definitions"
您的 SeleniumSteps 中缺少 [Binding] 属性
namespace Functional.Steps
{
[Binding]
public class SeleniumSteps : PrivateDomain.Steps.SeleniumSteps
{
#region Hooks
[BeforeScenario]
public void Before()
{
// before
}
}
}