我需要帮助扩展我已经使用WebDriver构建的Selenium框架。我想在我的代码之上添加使用EventFiringWebDriver
的eventHandler,为WebDriver为记录目的触发的每个操作添加事件。
首先,我将尝试清楚地呈现代码流。
测试开始......
第1步。CspTestBase
public class CspTestBase : CommonElements
{
[BeforeScenario]
public void Init()
{
Initialize(); <- Code to init driver.
// more code here.
LoginPage.Goto();
LoginPage.LoginAs(TestConfig.Username).WithPassword(TestConfig.Password).Login();
}
[AfterScenario]
public void CleanUp()
{
Close();
}
当它跳转到Init
方法时。此类包含直接需要WebDriver实例的所有方法。
public static class Driver
{
public static IWebDriver Instance { get; private set; }
public static void Initialize()
{
Instance = new TestDriverFactory().CreateDriver(); <- Next step
Instance.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
Instance.Manage().Cookies.DeleteAllCookies();
//Instance.Manage().Window.Maximize();
}
然后创建WebDriver Instance
时:
public class TestDriverFactory
{
public IWebDriver CreateDriver()
{
return new TestBase().Create(
new LocalDriverConfig(
TestConfig.Browser));
}
}
当它最终创建实际的驱动程序时:
public class TestBase
{
private static IWebDriver _driver;
//string env;
private DesiredCapabilities _capabilities;
public IWebDriver Create(LocalDriverConfig config)
{
switch (config.Browser)
{
case "Chrome":
_driver = new ChromeDriver(@"C:\Utilities");
break;
case "Firefox":
SetFirefoxCapabilities();
//_driver = new FirefoxDriver(_capabilities);
break;
...
private void SetFirefoxCapabilities()
{
...
_capabilities = DesiredCapabilities.Firefox();
//FirefoxOptions options = new FirefoxOptions();
//options.SetLoggingPreference(LogType.Browser, LogLevel.Warning);
_capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile);
//capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, options);
EventFiringWebDriver firingDriver = new EventFiringWebDriver(new FirefoxDriver(binary, profile, TimeSpan.FromSeconds(60)));
// copy the profile for ommiting duplicate references
firingDriver.ExceptionThrown += firingDriver_TakeScreenshotOnException;
_driver = firingDriver;
}
private void firingDriver_TakeScreenshotOnException(object sender, WebDriverExceptionEventArgs e)
{
TakeScreenshot();
}
正如您所看到的,我已经使用EventFiringWebDriver
,但我希望能够正确完成它以尽可能多地保存我的工作。
我做了一些阅读并查找了解决方案,但这些都很简单或者是Java。任何帮助都感激不尽。