0

I'm writing a simple extension library to Selenium Webdriver. I have my "wrapper" class WebDriverManager which defines event delegates as follows:

    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:

    [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. What's going on?

Jan Bońkowski
  • 538
  • 9
  • 22

1 Answers1

1

If the purpose of the test is to check that the events get fired, then:

  1. Seprate your tests to check each Event individually.
  2. Don't register to the event in the intialization method.
  3. Don't call the code that triggers the events in the initialization method.
  4. You need to both register to the event and trigger the code that raises it inside a dedicated test method.
  5. If you need to test your code with both 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.
  6. Don't rely on 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.

Hope it helps!

Itay Podhajcer
  • 2,616
  • 2
  • 9
  • 14