Reputation: 12993
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
Upvotes: 2
Views: 1730
Reputation: 12993
Here's what I came up with.
I read this question several times before I came up with the obvious solution..
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;
}
}
Upvotes: 2