开发者

WatiN Handle Confirm dialog in Firefox

开发者 https://www.devze.com 2023-02-25 19:32 出处:网络
I found this code on SO to automatically dismiss a confirm dialog, but it is not working in Firefox. The problem is, var windowButton = new WindowsEnumerator().GetChildWindows(window.Hwnd, w => w.

I found this code on SO to automatically dismiss a confirm dialog, but it is not working in Firefox.

The problem is, var windowButton = new WindowsEnumerator().GetChildWindows(window.Hwnd, w => w.ClassName == "Button" && new WinButton(w.Hwnd).Title == "OK").FirstOrDefault();

Always returns null. Is there another way to get the handle of the dialog button in firefox?

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 G开发者_运维问答etOKButton(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);
 }
}


The controls on the Firefox alert() dialog are not enumerable. That is, they don't exist as separate windows like they do in IE. The best way to approach this is to create a new DialogHandler class that implements IDialogHandler. In the constructor, you can pass in the Firefox instance for which the dialog appears, and you can use the following codeto send JavaScript across to Firefox to manipulate the dialog:

FFDocument nativeDoc = firefox.NativeDocument as FFDocument;

// ClientPort has several WriteAndRead... functions, 
// and takes a variable list of arguments for the script 
// to be executed.
nativeDoc.ClientPort.WriteAndRead(script);

You can use the JavaScript below to click on the OK and Cancel buttons on an alert() or confirm() dialog.

private const string DialogIsConfirmScript = "typeof getWindows()[{0}].document.documentElement.getButton('accept') !== 'undefined' && typeof getWindows()[{0}].document.documentElement.getButton('cancel') !== 'undefined';";
private const string DialogIsAlertScript = "typeof getWindows()[{0}].document.documentElement.getButton('accept') !== 'undefined' && typeof getWindows()[{0}].document.documentElement.getButton('cancel') !== 'undefined' && getWindows()[{0}].document.documentElement.getButton('cancel').hidden;";
private const string ClickCancelButtonScript = "getWindows()[{0}].document.documentElement.getButton('cancel').click()";
private const string ClickOKButtonScript = "getWindows()[{0}].document.documentElement.getButton('accept').click()";
private const string WindowClassName = "MozillaDialogClass";

A more complete implementation, which wraps the native IE alert() and confirm() handling in a common interface and adds Firefox handling is available at http://pastebin.com/ZapXr9Yf

0

精彩评论

暂无评论...
验证码 换一张
取 消