Console.WriteLine("\nBegin WPF UIAutomation test run\n"); Console.WriteLine("Launching the application"); Process p = null;
p = Process.Start(@"C:\temp\bin\wpfApplication.exe");
int ct = 0;
do
{ Console.WriteLine("Looking for application process. . . "); ++ct;
Thread.Sleep(10);
}
while (p == null && ct < 50);
if (p == null)
throw new Exception("Failed to find application process"); else
Console.WriteLine("Found application process");
Console.WriteLine("\nGetting Desktop"); AutomationElement aeDesktop = null;
aeDesktop = AutomationElement.RootElement;
if (aeDesktop == null)
throw new Exception("Unable to get Desktop"); else
Console.WriteLine("Found Desktop\n");
Console.WriteLine("\nLooking for application main window. . . "); Thread.Sleep(5000);
AutomationElement application = AutomationElement.FromHandle(p.MainWindowHandle);
Console.WriteLine("\nCompleting controls. . . "); AutomationElement loginView = application.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.ClassNameProperty,
"LoginView"));
AutomationElement loginPart = loginView.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.ClassNameProperty,
"LoginPart"));
AutomationElement txtUserName = loginPart.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.AutomationIdProperty,
"username"));
AutomationElement txtPassword = loginPart.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.AutomationIdProperty,
"password"));
Console.WriteLine("\nSetting input to a030601"); ValuePattern vptxtUserName = (ValuePattern)txtUserName.GetCurrentPattern(ValuePattern.Pattern);
vptxtUserName.SetValue("testuser"); ValuePattern vptxtPassword = (ValuePattern)txtPassword.GetCurrentPattern(ValuePattern.Pattern);
vptxtPassword.SetValue("p@ssword1");
AutomationElement cmdLogin = loginPart.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.AutomationIdProperty, "authenticationButton"));
Console.WriteLine("\nClicking on Compute button"); InvokePattern ipLoginCmd =
(InvokePattern)cmdLogin.GetCurrentPattern(
InvokePattern.Pattern);
ipLoginCmd.Invoke();