我正在为Selenium Webdriver编写一个简单的扩展库。我有我的"包装" class 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(); }
在我的目标项目中,我添加了对包含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();
}
测试被正确触发并传递,但在&#34;输出&#34;代表没有消息。我还尝试按照Visual Studio的建议将OnStart()
的正文更改为OnStartTesting?.Invoke()
,但结果没有任何变化。发生了什么事?
答案 0 :(得分:1)
如果测试的目的是检查事件是否被触发,那么:
Event
。Chrome
和Firefox
测试代码,请将它们分成不同的测试方法,这样您就可以对OnStartTesting
和OnTerminateTesting
进行测试{1}}以及Chrome
和OnStartTesting
OnTerminateTesting
。Firefox
进行测试,而是尝试在测试方法中创建一个标志,然后由匿名委托设置为特定事件设置事件handelr,然后{{1}在那个价值中。希望它有所帮助!