我以前使用Java在Selenium中使用了WebEventListner,但我的工作场所使用c#。我在'register'e_driver.register(eventListener);遇到错误
请帮忙一些C#。
namespace TestFramework.Utils
{
public class WebEventListener
{
public static void beforeNavigateTo(String url, IWebDriver driver)
{
Console.WriteLine("Before navigating to: '" + url + "'");
}
public static void afterNavigateTo(String url, IWebDriver driver)
{
Console.WriteLine("Navigated to:'" + url + "'");
}
}
[OneTimeSetUp]
public void InitializeOnce()
{
PropertiesCollection.driver = BrowserFactory.PrepareBrowser(Config.browser);
driver = PropertiesCollection.driver;
e_driver = new EventFiringWebDriver(driver);
eventListener = new WebEventListener();
e_driver.register(eventListener);
driver = e_driver;
}
答案 0 :(得分:1)
C#的语法与Java根本不同。由于C# natively supports events的语法,EventFiringWebDriver
的.NET实现利用了它们。此外,事件名称会有所不同,以使.NET开发人员对.NET绑定感到“自然”。您想要的内容如下:
public IWebDriver CreateEventFiringDriver(IWebDriver driver)
{
// Assume driver is a valid IWebDriver instance.
var eventDriver = new EventFiringWebDriver(driver);
eventDriver.Navigating += NavigatingEventHandler;
eventDriver.Navigated += NavigatedEventHandler;
return eventDriver;
}
private void NavigatingEventHandler(object sender, WebDriverNavigationEventArgs e)
{
Console.WriteLine("Before navigating to: '" + e.Url + "'");
}
private void NavigatedEventHandler(object sender, WebDriverNavigationEventArgs e)
{
Console.WriteLine("Navigated to:'" + e.Url + "'");
}