簡體   English   中英

登錄對話框處理程序Windows 7

[英]logon dialog handler windows 7

using System.Windows.Automation;
using System.Linq;
using WatiN.Core.DialogHandlers;
using WatiN.Core.Native.Windows;

namespace DialogHandlers
{
public class Windows7LogonDialogHandler : LogonDialogHandler
{
    #region Private Fields

    private readonly string _mUsername;
    private readonly string _mPassword;
    private readonly AndCondition _mListCondition = new AndCondition(new PropertyCondition(AutomationElement.IsEnabledProperty, true), new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem));
    private readonly AndCondition _mEditCondition = new AndCondition(new PropertyCondition(AutomationElement.IsEnabledProperty, true), new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
    private readonly AndCondition _mButtonConditions = new AndCondition(new PropertyCondition(AutomationElement.IsEnabledProperty, true), new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button));

    #endregion

    #region Constructor

    public Windows7LogonDialogHandler(string username, string password)
        : base(username, password)
    {
        _mUsername = username;
        _mPassword = password;
    }

    #endregion

    #region Public Members

    public override bool HandleDialog(Window window)
    {
        if (CanHandleDialog(window))
        {
            var win = AutomationElement.FromHandle(window.Hwnd);
            var lists = win.FindAll(TreeScope.Children, _mListCondition);
            var buttons = win.FindAll(TreeScope.Children, _mButtonConditions);
            var another = (from AutomationElement list in lists
                           where list.Current.ClassName == "UserTile"
                           where list.Current.Name == "Use another account"
                           select list).First();
            another.SetFocus();

            foreach (var edit in from AutomationElement list in lists
                                 where list.Current.ClassName == "UserTile"
                                 select list.FindAll(TreeScope.Children, _mEditCondition)
                                     into edits
                                     from AutomationElement edit in edits
                                     select edit)
            {
                if (edit.Current.Name.Contains("User name"))
                {
                    edit.SetFocus();
                    var usernamePattern = edit.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
                    if (usernamePattern != null) usernamePattern.SetValue(_mUsername);
                }
                if (edit.Current.Name.Contains("Password"))
                {
                    edit.SetFocus();
                    var passwordPattern = edit.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
                    if (passwordPattern != null) passwordPattern.SetValue(_mPassword);
                }
            }
            foreach (var submitPattern in from AutomationElement button in buttons
                                          where button.Current.AutomationId == "SubmitButton"
                                          select button.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern)
            {
                submitPattern.Invoke();
                break;
            }
            return true;
        }
        return false;
    }

    public override bool CanHandleDialog(Window window)
    {
        return window.ClassName == "#32770";
    }

    #endregion
}

}

在下面的代碼行:public overlay bool HandleDialog(窗口窗口)它需要一個窗口值,並且在我測試一個網站時,我沒有該窗口的名稱或代碼。 請為此提出建議

您到底在找什么? 它是一個具有兩個文本字段和一個提交憑據的按鈕的窗口(對話框)嗎? 該對話框是否從您的Web應用程序啟動?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM