Thursday, January 25, 2007

Handling alerts in WATIN

Update: I posted a response awhile ago as a new blog entry http://tmichealson.blogspot.com/2007/03/use-once-dialog.html



One of the most common user interaction on a website is the javascript
"alert" funcions.
With a syntect of:

alert('message');

This function displays a message to the user and requires that they click a
button labeled OK before continuing.

Creating an automated test for a website that uses this poses a couple of
problems. First the is no DOM object
for the window, and second the window runs under a different thread then the
main browser window. Luckly any
web automation tool worth its salt will address both these issues and WATIN
is no exeption. The following code is an
example of how to use the alertdialog handler.


using( IE ie = new IE("http://hostname/pagename.htm"))
{


AlertDialogHandler alertDialogHandler = new AlertDialogHandler ();
using (new UseDialogOnce(ie.DialogWatcher, alertDialogHandler ))
{


/*************************************
* -- code to generate alert -- *
* *
* must use the "nowait" to allow *
* the code to goto the next line *
* *
*************************************/

alertDialogHandler.WaitUntilExists();


alertDialogHandler.OKButton.Click();

IE.WaitForComplete();

}

}

11 comments:

Anonymous said...

Hi,

where does this UseDialogOnce() come from?

Anonymous said...

it looks like a custom class, designed to automatically remove the IDialogHandler from the DialogWatcher after using it (so it doesn't intercept any future alert boxes). It's missing from the post, but it'd need to be something like:

public class UseDialogOnce :IDisposable
{
private DialogWatcher _watcher;
private IDialogHandler _handler;

public UseDialogOnce(DialogWatcher watcher,IDialogHandler handler)
{
if (watcher == null) throw new ArgumentNullException("watcher");
if (handler == null) throw new ArgumentNullException("handler");

_watcher = watcher;
_handler = handler;

watcher.Add(handler);
}


#region IDisposable Members
private bool disposed = false;
public void Dispose()
{
Dispose(true);
//Prevent the GC to call Finalize again, since you have already
//cleaned up.
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
//Make sure Dispose does not get called more than once,
//by checking the disposed field
if (!this.disposed) {
if (disposing) {
//Clean up managed resources
_watcher.Remove(_handler);
}
//Now clean up unmanaged resources
}
disposed = true;
}
#endregion
}

Trevor Michealson said...

I've left an update to this post

http://tmichealson.blogspot.com/2007/03/use-once-dialog.html

Rishi said...

Hi,

The Code that will generate in this should be a click on a link or a button or an Image, etc. When I have this type of situation, the code works well. But, I have a textbox, on it's lost focus as a part of the control validation, the alert dialog is thrown.
The issues in this case is, there is no ClickNoWait(). I am doing a:
txtField.TypeText("test");
Now on this there is no ClickNoWait().

What should be done?

Rishi said...

So can it be done without using the ClickNoWait() ?
Please reply I am stuck up on this from a very long time.

Thanks & Regards,

Mike said...

Is there any way to have WatiN not do anything if an alert pops up? I am not using WatiN for testing, but instead to open a browser from an application, and submit a form. I would prefer it if WatiN just stopped controlling the browser all together once the form was submitted.

Do I have any options? Thanks.

Anonymous said...

Hey Trev,

Thanks very much for posting this... It was VERY helpful, I am running my WatiN automation test through NUnit and I was having a problem when one of the tests failed, it seemed that tests after that one couldn't find a dialog i was expecting (I thought it was a problem with something locally on my machine that was causing this because i couldn't find anything on the net about it.......) Turns out the problem was that I wasn't disposing the "Failed Test's dialog handler" before adding the next test's dialog handler..... after reading your post I used the DialogWatcher.RemoveAll function before adding the next handler and all my problems were solved..... Thanks!

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

Amiable fill someone in on and this post helped me alot in my college assignement. Say thank you you seeking your information.

Anonymous said...

Easily I acquiesce in but I think the list inform should secure more info then it has.