Watin 的 Windows 登录对话框 [英] Windows Logon Dialog by Watin

查看:20
本文介绍了Watin 的 Windows 登录对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个使用 WatiN 在 IE 上运行的自动化测试.此测试需要处理登录对话框.我应该如何让 Watin 处理它?<​​/p>

We have an automation test running on IE using WatiN. This test requires handling the logon dialog. How I should process it by Watin?

推荐答案

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

In Windows 7, the Logon handler became a WPF component and no longer a part of the IE. I think this even applies to IE8 or higher already.

以下是我们用于处理这些对话框的代码.这是我自己写的东西,所以可以随意使用它,只要在适当的时候给予信用.

Below is the code we use for handeling these dialogs. It's something I wrote myself, so feel free to use it, as long as credit is given where due.

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

这篇关于Watin 的 Windows 登录对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆