WatiN LogonDialogHandlers not working correctly in Windows 7

后端 未结 7 1094
旧巷少年郎
旧巷少年郎 2020-12-07 02:01

I have recently updated to Windows 7, VS2010 and IE8. We have an automation suite running tests against IE using WatiN. These tests require the logon dialog handler to be us

7条回答
  •  有刺的猬
    2020-12-07 02:15

    I tried to use the two automation examples above and found that they did not handle the scenario when the other credentials have been remembered in which case you only see the password in box. In this case you need to programmatically click the "Use another account" section. So I modified the supplied code to do that and it is now working OK. Here's the modified code:

    public static Browser Win7Login(string username, string password, string url)
        {
            var ieProcess = Process.Start("iexplore.exe", url);
            ieProcess.WaitForInputIdle();
    
            Thread.Sleep(2000);
    
            var ieWindow = AutomationElement.FromHandle(ieProcess.MainWindowHandle);
    
            var conditions = new AndCondition(new PropertyCondition(AutomationElement.IsEnabledProperty, true),
                                       new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
            var listCondition = new AndCondition(new PropertyCondition(AutomationElement.IsEnabledProperty, true),
                                            new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.ListItem));
            var editCondition = new AndCondition(new PropertyCondition(AutomationElement.IsEnabledProperty, true),
                                        new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Edit));
            var buttonConditions = new AndCondition(new PropertyCondition(AutomationElement.IsEnabledProperty, true),
                                         new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button));
    
            var c = ieWindow.FindAll(TreeScope.Children, conditions);
    
            foreach (AutomationElement child in c)
            {
                if (child.Current.ClassName == "#32770")
                {
                    // find the list
                    var lists = child.FindAll(TreeScope.Children, listCondition);
                    // find the buttons
                    var buttons = child.FindAll(TreeScope.Children, buttonConditions);
    
                    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, editCondition)
                                                       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(username);
                        }
                        if (edit.Current.Name.Contains("Password"))
                        {
                            edit.SetFocus();
                            var passwordPattern = edit.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
                            if (passwordPattern != null) passwordPattern.SetValue(password);
                        }
                    }
                    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 IE.AttachTo(Find.By("hwnd", ieWindow.Current.NativeWindowHandle.ToString()), 30);
        }
    

    Thanks to the others who got me most of the way there.

提交回复
热议问题