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

你离开我真会死。 提交于 2019-11-27 16:23:52

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/3454422/c-sharp-watin-add-an-alertdialoghandler-to-click-ok-button-on-every-alert-dial

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!