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

前端 未结 1 893
攒了一身酷
攒了一身酷 2020-12-16 02:17

Hello

Those who have used WatiN likely also used DialogHandlers.

Well can someone teach me how can i assign a DialogHandl

相关标签:
1条回答
  • 2020-12-16 02:53

    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.

    0 讨论(0)
提交回复
热议问题