[Watin-users] IE11 - Windows Authenticantion (scheduled task)
Brought to you by:
jvmenen
From: André V. <vel...@gm...> - 2014-03-28 19:04:59
|
Hi everybody, after struggling a lot, I was able to logon at some page using the Windows Security Dialog. The problem is that I can only authenticate when running my tests "manually". When I try to run them using the scheduled task they run at background and the security dialog cannot be found. Does anybody know what should I do? Thanks in advance, *that's my code: public class Windows7LogonDialogHandler : BaseDialogHandler { #region Private Fields private readonly string _mUsername; private readonly string _mPassword; Helpers.LogWriter _mLog; private readonly AndCondition _mListItemsCondition = 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, > Helpers.LogWriter log) { _mUsername = username; _mPassword = password; _mLog = log; } #endregion > #region Public Members public override bool HandleDialog(Window window) { //verificando se há janela de autenticação if (CanHandleDialog(window)) { var win = AutomationElement.FromHandle(window.Hwnd); var lists = win.FindAll(TreeScope.Descendants, > _mListItemsCondition); for (int i = 0; i < lists.Count; i++) { if (lists[i].Current.Name == "Windows Security" || > lists[i].Current.Name == "Segurança do Windows") { var childs = > lists[i].FindAll(TreeScope.Descendants, PropertyCondition.TrueCondition); for (int x = 0; x < childs.Count; x++) { _mLog.Write("#type: " + > childs[x].Current.ControlType); _mLog.Write("#class: " + > childs[x].Current.ClassName); _mLog.Write("#name: " + childs[x].Current.Name); } } } var buttons = win.FindAll(TreeScope.Children, > _mButtonConditions); _mLog.Write("#buttons found: " + buttons.Count); var another = (from AutomationElement list in lists where list.Current.ClassName == "UserTile" where list.Current.Name == "Use another > account" select list).FirstOrDefault(); if (another != null) another.SetFocus(); var _edits = from AutomationElement list in lists where > list.Current.ClassName.Contains("UserTile") select list.FindAll(TreeScope.Children, > _mEditCondition) into edits from AutomationElement edit in edits select edit; if (_edits == null || _edits.Count() == 0) { _mLog.Write("Elementos para realição de login não > encontrado..."); return false; } _mLog.Write("Realizando login..."); foreach (var edit in _edits) { if (edit.Current.Name.Contains("User name") || > edit.Current.Name.Contains("Nome de usuário")) { edit.SetFocus(); var usernamePattern = > edit.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern; if (usernamePattern != null) > usernamePattern.SetValue(_mUsername); } if (edit.Current.Name.Contains("Password") || > edit.Current.Name.Contains("Senha")) { 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 } *Att,* *André S. Vellinha* |