PageRenderTime 79ms CodeModel.GetById 16ms RepoModel.GetById 0ms 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. //===================================================================================
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Text;
  21. using System.Diagnostics;
  22. using System.Windows.Automation;
  23. using System.Threading;
  24. using AcceptanceTestLibrary.Common;
  25. using AcceptanceTestLibrary.ApplicationHelper;
  26. using AcceptanceTestLibrary.ApplicationObserver;
  27. namespace AcceptanceTestLibrary.Common.Desktop
  28. {
  29. public class WpfAppLauncher : AppLauncherBase, IStateObserver
  30. {
  31. Process targetProcess;
  32. public override List<AutomationElement> LaunchApp(string applicationName, string processName)
  33. {
  34. ApplicationProcessName = processName;
  35. StateDiagnosis.Instance.StartDiagnosis(this);
  36. List<AutomationElement> aeList = new List<AutomationElement>();
  37. try
  38. {
  39. targetProcess = Process.Start(applicationName);
  40. Thread.Sleep(4000);
  41. AutomationElement aeWindow = AutomationElement.FromHandle(targetProcess.MainWindowHandle);
  42. StateDiagnosis.Instance.StopDiagnosis(this);
  43. aeList.Add(aeWindow);
  44. return aeList;
  45. }
  46. catch (Exception)
  47. {
  48. UnloadApp(targetProcess);
  49. return null;
  50. }
  51. }
  52. public static Process GetCurrentAppProcess()
  53. {
  54. return Process.GetProcesses().First<Process>(proc => proc.ProcessName.Equals(ApplicationProcessName));
  55. }
  56. public static string ApplicationProcessName
  57. { get; set; }
  58. public override void UnloadApp(Process p)
  59. {
  60. p.Kill();
  61. p.Dispose();
  62. }
  63. #region IStateObserver Members
  64. public void Notify()
  65. {
  66. UnloadApp(GetCurrentAppProcess());
  67. }
  68. #endregion
  69. }
  70. }