Cod C# si XAML (UI Automation)

Program.cs

using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Automation;

namespace UiAutomation
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (Process.Start(@"..\..\..\SampleWpf\bin\Debug\SampleWpf.exe") == null)
                {
                    Console.WriteLine("Nu a fost gasit procesul");
                    return;
                }

                var window = GetWindowElement();
                if (window == null)
                {
                    Console.WriteLine("Nu a fost gasita fereastra aplicatiei");
                    return;
                }

                EnterMessage(window, "Mesaj de test 1");
                ClickButton(window);
                EnterMessage(window, "Mesaj de test 2");
                ClickButton(window);
                EnterMessage(window, "Mesaj de test 3");
                ClickButton(window);
                Console.WriteLine("Al doilea element este: {0}", GetSecondElementFromListbox(window));
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: {0}", e.InnerException != null ? e.Message + ": " + e.InnerException.Message : e.Message);
            }

            Console.WriteLine("Apasa o tasta pentru a termina aplicatia");
            Console.ReadKey();
        }

        private static AutomationElement GetWindowElement()
        {
            var desktop = AutomationElement.RootElement;
            if (desktop == null)
                throw new Exception("Nu a fost gasit elementul Desktop");

            AutomationElement window;
            var count = 0;
            do
            {
                window = desktop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.AutomationIdProperty, "AppWnd"));
                Thread.Sleep(100);
            } while (window == null && ++count < 50);

            return window;
        }

        private static void EnterMessage(AutomationElement window, string message)
        {
            // Obtine casuta de editare
            const string id = "MsgEdit";
            var textBox = window.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, id));
            if (textBox == null)
            {
                Console.WriteLine("Nu a fost gasit obiectul cu ID-ul '{0}'", id);
                return;
            }

            // Introduce textul in casuta de editare
            var pattern = (ValuePattern)textBox.GetCurrentPattern(ValuePattern.Pattern);
            pattern.SetValue(message);
            Thread.Sleep(1000);
        }

        private static void ClickButton(AutomationElement window)
        {
            // Obtine butonul
            const string id = "Btn";
            var button = window.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, id));
            if (button == null)
            {
                Console.WriteLine("Nu a fost gasit obiectul cu ID-ul '{0}'", id);
                return;
            }

            // Apasa butonul
            var pattern = (InvokePattern)button.GetCurrentPattern(InvokePattern.Pattern);
            pattern.Invoke();
            Thread.Sleep(1000);
        }

        private static string GetSecondElementFromListbox(AutomationElement window)
        {
            // Obtine elementul lista
            const string id = "Lst";
            var listbox = window.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AutomationIdProperty, id));
            if (listbox == null)
            {
                Console.WriteLine("Nu a fost gasit obiectul cu ID-ul '{0}'", id);
                return string.Empty;
            }

            // Returneaza al doilea element din lista
            var pattern = (ItemContainerPattern)listbox.GetCurrentPattern(ItemContainerPattern.Pattern);
            var item1 = pattern.FindItemByProperty(null, AutomationElement.ControlTypeProperty, ControlType.ListItem);
            var item2 = pattern.FindItemByProperty(item1, AutomationElement.ControlTypeProperty, ControlType.ListItem);
            return item2.Current.Name;
        }
    }
}

Inapoi