2014-02-10 81 views
0
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 
} 

}登錄對話框處理的Windows 7

在行:公衆覆蓋BOOL HandleDialog(窗窗) 它需要一個值窗口,因爲我測試的網站,我沒有名稱或代碼那個窗戶。 請爲此提出替代

+0

檢查[http://stackoverflow.com/questions/5476190/how-do-i-get-the-classname-of-the-active-window]出...可以幫助你... 謝謝, Sham_ – Sham

+0

No ..我沒有這個窗口的源代碼,它的工作方式不是這樣..有沒有其他方法來處理Windows 7中的登錄對話框處理程序 – user3274713

回答

0

你到底在看什麼?它是一個窗口(對話框),它有兩個文本字段和一個提交證書的按鈕?該對話框是從您的網絡應用程序啓動的嗎?

+0

是的。當我打開一個網站時,它首先給出一個登錄提示,其中包含兩個文本框以輸入用戶名和密碼以及兩個確定和取消按鈕,並且沒有用於登錄提示的源代碼,所以我必須在我的c#代碼中使用watin處理該提示。 – user3274713

+0

對於使用WatiN的窗戶,沒有完整的解決方案。你需要使用不同的工具。或作爲臨時解決方案。嘗試發送鍵(假設焦點將在對話框中)。 System.Windows.Forms.SendKeys.SendWait( 「{TAB}」); System.Windows.Forms.SendKeys.SendWait(密碼); System.Windows.Forms.SendKeys.SendWait( 「{TAB}」); System.Windows.Forms.SendKeys.SendWait( 「{} ENTER」);. – Sham

+0

我正在使用具有登錄對話框的Web應用程序。 – user3274713