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
}
在行:公衆覆蓋BOOL HandleDialog(窗窗) 它需要一個值窗口,因爲我測試的網站,我沒有名稱或代碼那個窗戶。 請爲此提出替代
檢查[http://stackoverflow.com/questions/5476190/how-do-i-get-the-classname-of-the-active-window]出...可以幫助你... 謝謝, Sham_ – Sham
No ..我沒有這個窗口的源代碼,它的工作方式不是這樣..有沒有其他方法來處理Windows 7中的登錄對話框處理程序 – user3274713