locked
How to find WPF controls in UIA(UI Automation )? RRS feed

  • Question

  • HI guys,

    I am currently working on test a WPF application, which contains lots of controls. And the controls are all loading Asynchronous,It's really Annoy me. Some times, I can't find them even I use UISpy.

    is there any solution for this?


    Best Regards,
    Lie

    • Changed type Lie You Wednesday, September 12, 2012 5:10 AM
    • Moved by Annabella Luo Monday, September 24, 2012 11:55 AM (From:Windows Presentation Foundation (WPF))
    Monday, September 10, 2012 2:36 AM

Answers

  • Hi Annabella,

    Help me to delete this post.


    Best Regards,
    Lie 鉴于以上版主的酱油行为,我谨代表MSDN用户表示强烈的鄙视和谴责。

    • Proposed as answer by Pete LakerMVP Wednesday, September 12, 2012 9:19 AM
    • Marked as answer by Lie You Monday, August 12, 2013 8:37 AM
    Wednesday, September 12, 2012 5:16 AM

All replies

  • Hi You,

    Whatever how the controls is loading, all shown out controls is in one VisualTree in one window, so that we can find the control along the VisualTree, as you said you can't find the control event though through the UISpy, so did the control is added into window before you want to find it out?

    Generally, we find control to automation like below code:

     void MainWindow_Loaded(object sender, RoutedEventArgs e)
            {
                Thread automateThread = new Thread(new ThreadStart(automationbutton));
                automateThread.Start();
            }
            private void automationbutton()
            {
                AutomationElement rootElement = AutomationElement.RootElement;
                if (rootElement != null)
                {
                    System.Windows.Automation.Condition condition = new PropertyCondition(AutomationElement.AutomationIdProperty, "myMainWindow");
                    AutomationElement wndELement = rootElement.FindFirst(TreeScope.Children, condition);
                    if (wndELement != null)
                    {
                        condition = new PropertyCondition(AutomationElement.NameProperty, "Button");
                            AutomationElement btnElement = wndELement.FindFirst(TreeScope.Descendants, condition);
                            InvokePattern btnPattern = btnElement.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
                            btnPattern.Invoke();
                    }
                }
            }

    Hope it helps.

    If you issue still exist, please post detail relate code here so that I can help you find the root cause of your issue.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, September 11, 2012 7:12 AM
  • I can catch the controls for first time.

    My situation is:

    1. lanuch the wpf app,

    2. waiting for loading controls by default.

    3. catch a control when it loads complete.

    4. goto other tab( which contains different controls and they are loading Asynchronous), and catch the control again.

    At this time, it's always failed. I know how to UIA, but thank you anyway.

    what i want to find is a solution for that situation. Please help me....


    Best Regards,
    Lie

    Tuesday, September 11, 2012 8:07 AM
  • Hi You,

    Please share relate code here so that we can better help you.

    Thank you.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, September 11, 2012 9:16 AM
  • Hi Annabella,

    Help me to delete this post.


    Best Regards,
    Lie 鉴于以上版主的酱油行为,我谨代表MSDN用户表示强烈的鄙视和谴责。

    • Proposed as answer by Pete LakerMVP Wednesday, September 12, 2012 9:19 AM
    • Marked as answer by Lie You Monday, August 12, 2013 8:37 AM
    Wednesday, September 12, 2012 5:16 AM