PageRenderTime 14ms CodeModel.GetById 10ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/V2.2/trunk/AcceptanceTestLibrary/AcceptanceTestLibrary/Common/Launcher/Desktop/WpfAppLauncher.cs

#
C# | 81 lines | 54 code | 11 blank | 16 comment | 0 complexity | 2888c4cf432d30759d9106e98d4a7add MD5 | raw file
 1//===================================================================================
 2// Microsoft patterns & practices
 3// Composite Application Guidance for Windows Presentation Foundation and Silverlight
 4//===================================================================================
 5// Copyright (c) Microsoft Corporation.  All rights reserved.
 6// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
 7// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
 8// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 9// FITNESS FOR A PARTICULAR PURPOSE.
10//===================================================================================
11// The example companies, organizations, products, domain names,
12// e-mail addresses, logos, people, places, and events depicted
13// herein are fictitious.  No association with any real company,
14// organization, product, domain name, email address, logo, person,
15// places, or events is intended or should be inferred.
16//===================================================================================
17using System;
18using System.Collections.Generic;
19using System.Linq;
20using System.Text;
21using System.Diagnostics;
22using System.Windows.Automation;
23using System.Threading;
24using AcceptanceTestLibrary.Common;
25using AcceptanceTestLibrary.ApplicationHelper;
26using AcceptanceTestLibrary.ApplicationObserver;
27
28namespace AcceptanceTestLibrary.Common.Desktop
29{
30    public class WpfAppLauncher : AppLauncherBase, IStateObserver
31    {
32        Process targetProcess;
33
34        public override List<AutomationElement> LaunchApp(string applicationName, string processName)
35        {
36            ApplicationProcessName = processName;
37            StateDiagnosis.Instance.StartDiagnosis(this);
38            List<AutomationElement> aeList = new List<AutomationElement>();
39
40            try
41            {
42                targetProcess = Process.Start(applicationName);
43
44                Thread.Sleep(4000);
45
46                AutomationElement aeWindow = AutomationElement.FromHandle(targetProcess.MainWindowHandle);
47                StateDiagnosis.Instance.StopDiagnosis(this);
48                aeList.Add(aeWindow);
49                return aeList;
50            }
51            catch (Exception)
52            {
53                UnloadApp(targetProcess);
54                return null;
55            }
56        }
57
58        public static Process GetCurrentAppProcess()
59        {
60            return Process.GetProcesses().First<Process>(proc => proc.ProcessName.Equals(ApplicationProcessName));
61        }
62
63        public static string ApplicationProcessName
64        { get; set; }
65
66        public override void UnloadApp(Process p)
67        {
68            p.Kill();
69            p.Dispose();
70        }
71
72        #region IStateObserver Members
73
74        public void Notify()
75        {
76            UnloadApp(GetCurrentAppProcess());
77        }
78
79        #endregion
80    }
81}