views:

666

answers:

2

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.

+1  A: 

For now we are using:

browser.DialogWatcher.CloseUnhandledDialogs = false

It has the following (ugly) issues:

  1. The error appears as a timeout of the next operation (using the message "Timeout while Internet Explorer busy").
  2. Because of the above there are unnecessary delays in the tests
  3. The instances that had the unexpected pop up are left opened (after the dispose).
eglasius
+1  A: 

Hi Freddy,

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

Jeroen van Menen