c# - Watin 的 Windows 登录对话框

标签 c# watin

我们有一个使用 WatiN 在 IE 上运行的自动化测试。此测试需要处理登录对话框。 Watin 应该如何处理?

最佳答案

在 Windows 7 中,登录处理程序成为 WPF 组件,不再是 IE 的一部分。我认为这甚至适用于 IE8 或更高版本。

下面是我们用来处理这些对话框的代码。这是我自己写的东西,所以请随意使用它,只要注明出处即可。

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
    }
}

关于c# - Watin 的 Windows 登录对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9079108/

相关文章:

c# - 如何在 silverlight C# 中制作 SuckEffect

c# - 是否有完全托管的 (.NET) Lua 解释器?

C# 单元测试 - 奇怪的项目目录

.net - Browser.AttachTo<T>() 导致间歇性异常 "Timeout while Internet Explorer busy"

c# - 如何在 Magick.NET Read 中设置位深度

javascript - 母版页中的 Jquery 日期选择器

testing - WatiN 测试项目最佳实践

等待 UnauthorizedAccessException 错误

c# - Watin 不会手动触发 .change() 事件

testing - 如何捕获从框架打开的弹出窗口?