Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fail tests by default if a dialog appears in watin

Whenever a dialog appears and there is no attached handler, watin auto closes the dialog. This is helpful when you don't want to add code for different/several simple confirmations the application might have.

The issue is that using this default behavior can cause simple issues to go unnoticed, like a confirmation dialog appearing in the scenarios it shouldn't.

I am looking for a simple way to gracefully fail the tests when unhandled dialogs appear. With gracefully I mean the test stops right when the dialog appears with an exception, that gives a decent message that lets you know it was an unexpected dialog error.

like image 797
eglasius Avatar asked Oct 26 '25 15:10

eglasius


1 Answers

Another option could be to use the AlertAndConfirmDialogHandler. This handler does close every alert or confirm dialog that popups up but first it gets the text shown by the dialog and stores it. You can inspect this Alerts string array and see if the Count is zero. You could do this in the Teardown or FixtureTeardown of the test class.

Following a copy of a test from the WatiN unittest to show you how you can use this handler:

        [Test]
    public void AlertAndConfirmDialogHandler()
    {
        DialogWatcher dialogWatcher;

        Assert.AreEqual(0, Ie.DialogWatcher.Count, "DialogWatcher count should be zero before test");

        // Create handler for Alert and confirm dialogs and register it.
        var dialogHandler = new AlertAndConfirmDialogHandler();
        using (new UseDialogOnce(Ie.DialogWatcher, dialogHandler))
        {
            Assert.AreEqual(0, dialogHandler.Count);

            Ie.Button("helloid").Click();

            Assert.AreEqual(1, dialogHandler.Count);
            Assert.AreEqual("hello", dialogHandler.Alerts[0]);

            // remove the alert text from the queue by using Pop
            Assert.AreEqual("hello", dialogHandler.Pop());

            Assert.AreEqual(0, dialogHandler.Count);

            // Clear the queue
            Ie.Button("helloid").Click();

            Assert.AreEqual(1, dialogHandler.Count);

            dialogHandler.Clear();

            Assert.AreEqual(0, dialogHandler.Count);

            dialogWatcher = Ie.DialogWatcher;
        }

        Assert.AreEqual(0, dialogWatcher.Count, "DialogWatcher count should be zero after test");
    }

This also triggers me to make the AutoClose behavior more pluggable. It would be nice if one could register a dialoghandler that will be called if no other handlers can handle a dialog, instead of just auto closing the dialogs.

HTH Jeroen van Menen lead dev WatiN

like image 94
Jeroen van Menen Avatar answered Oct 29 '25 05:10

Jeroen van Menen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!