views:

181

answers:

2

I have a, what seems to be, rather common scenario I'm trying to work through.

I have a site that accepts input through two different text fields. If the input is malformed or invalid, I receive a Javascript pop-up notification.

I will not always receive one, but I should in the event of (like I said earlier) malformed data, or when a search result couldn't be found.

How can I detect this in WatiN?

A quick Google search produced results that show how to click through them, but I'm curious as to whether or not I can detect when I get one?

In case anyone is wondering, I'm using WatiN to do some screen scraping for me, rather than integration testing :)

Thanks in advance!

Ian

A: 

Here's what I came up with.

I read this question several times before I came up with the obvious solution..

http://stackoverflow.com/questions/458177/can-i-read-javascript-alert-box-with-watin

This is the code I came up with.. While it does force a delay of 3 seconds if the alert doesn't happen, it works perfectly for my scenario.

Hope someone else finds this useful..

frame.Button(Find.ByName("go")).ClickNoWait();

System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
stopwatch.Start();

while (stopwatch.Elapsed.TotalMilliseconds < 3000d)
{
    if (alertDialogHandler.Exists())
    {
        // Do whatever I want to do when there is an alert box.
        alertDialogHandler.OKButton.Click();
        break;
    }
}
Ian P
+1  A: 

Thanks Ian, this helped me immensely in solving an issue I was having with the ie.WaitForComplete() function not working as I'd hoped

Jason