<http://tmichealson.blogspot.com/2007/01/handling-alerts-in-watin.html> I
referenced a class called UseDialogOnce and did not show the code for it.
This is an adaptation of the one that is in the WATIN Unit tests file
IeAndMainDocument.cs
using System;
using WatiN.Core;
using WatiN.Core.DialogHandlers;
namespace Framework.UnitTesting
{
public class UseDialogOnce : IDisposable
{
private DialogWatcher dialogWatcher;
private IDialogHandler dialogHandler;
public UseDialogOnce(DialogWatcher dialogWatcher,IDialogHandler
dialogHandler)
{
this.dialogWatcher = dialogWatcher;
this.dialogHandler = dialogHandler;
if (dialogWatcher != null)
{
dialogWatcher.Add(dialogHandler);
}
}
#region IDisposable Members
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
return;
}
protected virtual void Dispose(bool managedAndNative)
{
dialogWatcher.Remove(dialogHandler);
dialogWatcher = null;
dialogHandler = null;
}
9 comments:
I was googling for WatiN and came across your blog and read this post. Thanks for publishing the improved version, I have updated the UceOnceDialog in the WatiN code. And keep those WatiN blogs coming!
Jeroen van Menen
http://watin.sourceforge.net
I came up with a Generic version of this that can be used without the danger of having the DialogHandler still available outside the using block.
I think this should be a pretty nice approach for people using 2.0.
using System;
using WatiN.Core.DialogHandlers;
namespace Medicity.Fixtures
{
public class UseDialogOnce< T > : IDisposable where T : IDialogHandler, new()
{
private bool _disposed = false;
private T _handler;
private DialogWatcher _watcher;
public UseDialogOnce(DialogWatcher watcher)
{
_watcher = watcher;
_handler = new T();
_watcher.Add(_handler);
}
public T Handler
{
get
{
if (_disposed)
throw new ObjectDisposedException("UseDialogOnce");
return _handler;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool managedAndNative)
{
_watcher.Remove(_handler);
_watcher = null;
_handler = default(T);
_disposed = true;
}
}
}
There is a way to trap office dialog ?
I have a case that generate a report into a excel file and prompt the use to open or save it.
Looks interesting.
Will it solve the problem of the below not removing the handler?
browser.RemoveDialogHandler(loginhandler);
For a new person learning Watin\C# what do I do to use this? Anychange you could document the line with what it's doing and how I could use it?
Thanks,
I've not seen an instance of the DialogHandler still being attached to IE outside the using{} block. In fact, executing the following statement inside the using{} block returns "1":
? ie.DialogWatcher.Count
Outside the block, the same statement returns "0"
LogonDialogHandler loginDialogHandler = new LogonDialogHandler(userName, password);
using (new UseDialogOnce(ie.DialogWatcher, loginDialogHandler))
{
imgLogin.ClickNoWait();
ie.WaitForComplete();
}
Post a Comment