[英]C# Custom Event is not fired
I'm writing a simple extension library to Selenium Webdriver. 我正在为Selenium Webdriver写一个简单的扩展库。 I have my "wrapper" class WebDriverManager
which defines event delegates as follows: 我有我的“包装”类WebDriverManager
,它定义事件委托如下:
public delegate void OnStartEventHandler();
public delegate void OnTerminateEventHandler();
public delegate void OnCheckpointEventHandler();
public event OnStartEventHandler OnStartTesting;
public event OnTerminateEventHandler OnTerminateTesting;
public event OnCheckpointEventHandler OnCheckpointTesting;
/// <summary>
/// Method that should be fired inside method with [OneTimeSetUp] attribute
/// </summary>
public void OnStart() { if (OnStartTesting != null) OnStartTesting(); }
/// <summary>
/// Method that should be fired inside method with [OneTimeTearDown] attribute
/// </summary>
public void OnTerminate() { if (OnTerminateTesting != null) OnTerminateTesting(); }
/// <summary>
/// Method that should be fired inside method with either [SetUp] or [TearDown] attribute
/// </summary>
public void OnCheckpoint() { if (OnCheckpointTesting != null) OnCheckpointTesting(); }
In my target project I add a reference to library that contains WebDriverManager
class and write a simple method: 在我的目标项目中,添加对包含WebDriverManager
类的库的引用,并编写一个简单的方法:
[OneTimeSetUp]
public void SetUp()
{
// wdmChrome and wdmFirefox are instances of WebDriverManager
wdmChrome.OnStartTesting += () => { Console.WriteLine("Starting testing Chrome browser"); };
wdmFirefox.OnStartTesting += () => { Console.WriteLine("Starting testing Firefox browser"); };
wdmChrome.OnTerminateTesting += () => { Console.WriteLine("Terminating test of Chrome browser"); };
wdmFirefox.OnTerminateTesting += () => { Console.WriteLine("Terminating test of Firefox browser"); };
wdmChrome.OnStart();
wdmFirefox.OnStart();
// other stuff that initializes webdriver
}
[OneTimeTearDown]
public void TearDown()
{
wdmChrome.OnTerminate();
wdmFirefox.OnTerminate();
wdmChrome.WebDriver.Close();
wdmFirefox.WebDriver.Close();
}
Test gets fired properly and passes, but in "Output" there are no messages from delegates. 测试被正确触发并通过,但是在“输出”中没有来自委托的消息。 I also tried to change body of OnStart()
to OnStartTesting?.Invoke()
as suggested by Visual Studio, but nothing changed in result. 我还尝试按照Visual Studio的建议将OnStart()
主体更改为OnStartTesting?.Invoke()
,但结果没有任何变化。 What's going on? 这是怎么回事?
If the purpose of the test is to check that the events get fired, then: 如果测试的目的是检查事件是否被触发,则:
Event
individually. 分开测试以单独检查每个Event
。 Chrome
and Firefox
, seprate them to diffrent test mthods, so you'll have OnStartTesting
and OnTerminateTesting
tests for Chrome
and OnStartTesting
and OnTerminateTesting
for Firefox
. 如果您需要同时使用Chrome
和Firefox
来测试代码,请将它们分开以进行不同的测试,那么您将具有针对Chrome
OnStartTesting
和OnTerminateTesting
测试以及针对Firefox
OnStartTesting
和OnTerminateTesting
。 Console.WriteLine()
for your tests, instead, try creating a flag inside the test method, which then is set by anonymous delegate set a the event handelr for a specific event and then Assert
in that value. 不要依赖于Console.WriteLine()
为你的测试,而是尝试创建的测试方法,然后由匿名委托集中一个事件handelr特定事件,然后里面的标志Assert
该值。 Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.