locked
KeyDown event using UIAutomation framework RRS feed

  • Question

  • Hi Folks,

    I need to click on "OK" button after setting some text in a textbox. Challenge is manually when we are entering text on this textbox, it is triggering key down event which is causing "OK" button to get enabled.

    However when setting the text using UIAutomation library this "OK" button is not getting enabled.

    Please let me know how we can trigger KeyDown event using UIAutomation.

    Thanks,

    Aditya Sharma


    Aditya Sharma


    • Edited by Adinb007 Tuesday, April 14, 2020 9:54 AM
    Tuesday, April 14, 2020 9:53 AM

Answers

  • With sendkeys the "OK" button is now getting enabled.

    AutomationElement aeButton = parentElement.FindFirst(System.Windows.Automation.TreeScope.Element, new System.Windows.Automation.PropertyCondition(System.Windows.Automation.AutomationElement.NameProperty, parentElement.Current.Name));
                                      if (aeButton != null)
                                      {
                                          try
                                          {
                                              ValuePattern vpattern = (ValuePattern)aeButton.GetCurrentPattern(ValuePattern.Pattern);
                                              if (vpattern != null)
                                              {
                                                  System.Threading.Thread.Sleep(1000);
                                                  aeButton.SetFocus();
                                                  SendKeys.SendWait(value);
                     
                                                  result = true;
                                                  break;
                                              }
                                          }
                                          catch(Exception ex)
                                          {
                                              result = false;
                                        //richTextBox1.AppendText(ex.Message);
                                        richTextBox1.AppendText("Input text 1" + ex.Message);
                                        continue;
                                          }
                                      }


    Aditya Sharma

    • Marked as answer by Adinb007 Wednesday, April 29, 2020 4:15 PM
    Wednesday, April 29, 2020 4:15 PM

All replies

  • You can call EnableWindow on the Button

    after you have entered text in the TextBox

    Tuesday, April 14, 2020 10:19 AM
  • Hi,

    Please see the below code for your reference, as suggested i have used enable window fucntion (highlighted in bold) but still it is not working:-

    [DllImport("user32.dll")]
            public static extern bool EnableWindow(IntPtr hwnd, bool bEnable);

     public bool InputText(string parentclassname, string controlClassName,string windowTitle,string AutxID,string CtrlName,string value)
            {
                windowTitle = windowTitle.ToLower();

                bool result = false;

                try
                {
                    IntPtr parentHandle = FindWindow(parentclassname, windowTitle);
                    IntPtr parentHandle2 = FindWindow(parentclassname, "SAP Logon At PE1 - ECC Production");

                    var parentElements = AutomationElement.FromHandle(parentHandle).FindAll(TreeScope.Descendants, Condition.TrueCondition);
                    foreach (AutomationElement parentElement in parentElements)
                    {
                        if(parentElement.Current.ClassName!="")
                        {    
                          if (parentElement.Current.ClassName == controlClassName && (parentElement.Current.AutomationId == AutxID || parentElement.Current.Name == CtrlName))
                              {
                                      AutomationElement aeButton = parentElement.FindFirst(System.Windows.Automation.TreeScope.Element, new System.Windows.Automation.PropertyCondition(System.Windows.Automation.AutomationElement.NameProperty, parentElement.Current.Name));

                                      if (aeButton != null)
                                      {
                                          try
                                          {
                                              ValuePattern vpattern = (ValuePattern)aeButton.GetCurrentPattern(ValuePattern.Pattern);
                                              if (vpattern != null)
                                              {
                                                  System.Threading.Thread.Sleep(1000);
                                                  EnableWindow(parentHandle2, true);
                                                  vpattern.SetValue(value);
                                                  EnableWindow(parentHandle2, false);
                                            result = true;
                                                  break;
                                              }
                                          }
                                          catch
                                          {
                                              result = false;
                                              continue;
                                          }
                                      }

                                  }
                        }
                  }
                }
                catch (Exception ex)
                {
                    result = false;
                }
                return result;
            }


    Aditya Sharma


    • Edited by Adinb007 Tuesday, April 14, 2020 11:22 AM
    Tuesday, April 14, 2020 11:20 AM
  • You call it on "parentHandle2", which is probably not the Button...

    See the hierarchy with Spy++

    Tuesday, April 14, 2020 11:40 AM
  • Do i need to give handle for the button ?

    How do i get handle for the button ?

    Through FindWindow method, we can only give windows title. 


    Aditya Sharma


    • Edited by Adinb007 Tuesday, April 14, 2020 12:11 PM
    Tuesday, April 14, 2020 12:09 PM
  • With FindWindowEx or GetDlgItem or EnumChildWindows...

    For example, this test enables the "Next" Button (ID = 1 from Spy++ on Windows 10) of "Find" window in (running and with a text) Notepad =>

    (English or French)

    IntPtr hWndNotepad = FindWindow("Notepad", null);
    if (hWndNotepad != IntPtr.Zero)
    {
        SwitchToThisWindow(hWndNotepad, true);
        SendKeys.Send("^(f)");
        System.Threading.Thread.Sleep(500);                    
        IntPtr hWndDialogBox = FindWindow("#32770", "Find");
        if (hWndDialogBox == IntPtr.Zero)
            hWndDialogBox = FindWindow("#32770", "Rechercher");
        IntPtr hWndNextButton = GetDlgItem(hWndDialogBox, 1);
        EnableWindow(hWndNextButton, true); 
    }

    Declarations :

    [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]       
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    
    [DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
            
    [DllImport("User32.dll", SetLastError= true)]                           
    public static extern bool SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
                    
    [DllImport("User32.dll", SetLastError = true)]                       
    public static extern IntPtr GetDlgItem(IntPtr hDlg,  int nIDDlgItem);
                                                         
    [DllImport("User32.dll", SetLastError = true)]                       
    public static extern bool EnableWindow(IntPtr hWnd, bool bEnable);   


    • Edited by Castorix31 Tuesday, April 14, 2020 2:07 PM
    Tuesday, April 14, 2020 12:44 PM
  • Id for that button is of the type string.

    Aditya Sharma

    Tuesday, April 14, 2020 1:59 PM
  • A control ID is always a number, in classic Win32 apps

    It can be fixed, like in Notepad, or generated

    You can then use FindWindowEx for example to find it by class name, but it depends on the hierarchy of controls

    The hierarchy of controls in Notepad for my test, where I got Control ID = 1 :


    • Edited by Castorix31 Tuesday, April 14, 2020 2:12 PM
    Tuesday, April 14, 2020 2:04 PM
  • Hi,

    Please see the below screenshot, control ID in my case is alphanumeric and keeps changing.



    • Edited by Adinb007 Friday, April 24, 2020 11:01 PM
    Friday, April 24, 2020 9:49 PM
  • It is in Hexadecimal

    If it changes, use one of the other methods, like FindWindowEx, to identify the control


    • Edited by Castorix31 Friday, April 24, 2020 11:35 PM
    Friday, April 24, 2020 11:34 PM
  • With sendkeys the "OK" button is now getting enabled.

    AutomationElement aeButton = parentElement.FindFirst(System.Windows.Automation.TreeScope.Element, new System.Windows.Automation.PropertyCondition(System.Windows.Automation.AutomationElement.NameProperty, parentElement.Current.Name));
                                      if (aeButton != null)
                                      {
                                          try
                                          {
                                              ValuePattern vpattern = (ValuePattern)aeButton.GetCurrentPattern(ValuePattern.Pattern);
                                              if (vpattern != null)
                                              {
                                                  System.Threading.Thread.Sleep(1000);
                                                  aeButton.SetFocus();
                                                  SendKeys.SendWait(value);
                     
                                                  result = true;
                                                  break;
                                              }
                                          }
                                          catch(Exception ex)
                                          {
                                              result = false;
                                        //richTextBox1.AppendText(ex.Message);
                                        richTextBox1.AppendText("Input text 1" + ex.Message);
                                        continue;
                                          }
                                      }


    Aditya Sharma

    • Marked as answer by Adinb007 Wednesday, April 29, 2020 4:15 PM
    Wednesday, April 29, 2020 4:15 PM