Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# WatiN - Add an AlertDialogHandler to click ok button on every Alert dialog window

Tags:

c#

watin

Hello

Those who have used WatiN likely also used DialogHandlers.

Well can someone teach me how can i assign a DialogHandler that will handle any Alert Box window.alert(), of a specific IE instance under WatiN control .

The DialogHandler only has to click in the OK button for very alert dialog box, in that case i think we need an AlertDialogHandler that basically only has to click the OK button.

AlertDialogHandler.OKButton.Click()

I've search the web and found a few examples.. But they work for a small period of time or the time you specify, i need one that will work forever, until i choose to stop it by clicking a button.

This as been bugging my head for hours, any help is appreciated. Thanks.

Note: Sometimes the alert dialog window has two buttons. Thats why i really need to click the OK button, not just Close the dialog window.

like image 700
Fábio Antunes Avatar asked Aug 11 '10 00:08

Fábio Antunes


1 Answers

Create class:

public class OKDialogHandler : BaseDialogHandler
{
    public override bool HandleDialog(Window window)
    {
        var button = GetOKButton(window);
        if (button != null)
        {
            button.Click();
            return true;
        }
        else
        {
            return false;
        }
    }

    public override bool CanHandleDialog(Window window)
    {
        return GetOKButton(window) != null;
    }

    private WinButton GetOKButton(Window window)
    {
        var windowButton = new WindowsEnumerator().GetChildWindows(window.Hwnd, w => w.ClassName == "Button" && new WinButton(w.Hwnd).Title == "OK").FirstOrDefault();
        if (windowButton == null)
            return null;
        else
            return new WinButton(windowButton.Hwnd);
    }
}

After creating instance of IE, attach dialog handler to it:

ie.AddDialogHandler(new OKDialogHandler());

This dialog handler will handle all windows, that contains a button with "OK" caption, by clicking on that button.

like image 119
prostynick Avatar answered Oct 20 '22 00:10

prostynick