PageRenderTime 50ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/V2.2/trunk/AcceptanceTestLibrary/AcceptanceTestLibrary/ApplicationHelper/BrowserSupportHandler.cs

#
C# | 109 lines | 79 code | 10 blank | 20 comment | 11 complexity | fd2ef9f74a741360183cc21c3c842123 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.Collections.Specialized;
  20. using System.Linq;
  21. using System.Text;
  22. using AcceptanceTestLibrary.ApplicationHelper;
  23. using System.Configuration;
  24. using System.Windows.Automation;
  25. using AcceptanceTestLibrary.Common.Launcher;
  26. using System.Reflection;
  27. using System.Diagnostics;
  28. using AcceptanceTestLibrary.Common.Launcher.Silverlight;
  29. using AcceptanceTestLibrary.Common.CrossBrowserSupport;
  30. using System.Collections;
  31. using System.IO;
  32. namespace AcceptanceTestLibrary.ApplicationHelper
  33. {
  34. public static class BrowserSupportHandler
  35. {
  36. static Object obj = new Object();
  37. static Dictionary<string, IBrowserLauncher> browserLauncherCol = new Dictionary<string,IBrowserLauncher>();
  38. //Load browsers from Config File - Config Section
  39. static NameValueCollection browserCollection = ConfigHandler.GetConfigSection("BrowserSupport/Browsers");
  40. public static List<AutomationElement> LaunchBrowser(string applicationPath, string applicationTitle)
  41. {
  42. List<AutomationElement> aeList = new List<AutomationElement>();
  43. IBrowserLauncher browserLauncher;
  44. //Load Browsers based on the Config Section
  45. if (browserCollection != null && browserCollection.Count > 0)
  46. {
  47. foreach (string browser in browserCollection)
  48. {
  49. browserLauncher = GetBrowserLauncherInstance(browserCollection[browser]);
  50. aeList.Add(browserLauncher.LaunchBrowser(applicationPath, applicationTitle));
  51. }
  52. }
  53. else //When there is no entry in the config file return null.
  54. {
  55. return null;
  56. }
  57. return aeList;
  58. }
  59. public static void UnloadBrowser(string applicationTitle)
  60. {
  61. //Close the Internet explorer browser
  62. IBrowserLauncher browserLauncher;
  63. //unload browsers based on the Config Section
  64. if (browserCollection != null && browserCollection.Count > 0)
  65. {
  66. foreach (string browser in browserCollection)
  67. {
  68. browserLauncher = GetBrowserLauncherInstance(browserCollection[browser]);
  69. browserLauncher.ApplicationTitleInBrowser = applicationTitle;
  70. UnloadProcess(browserLauncher.GetCurrentAppProcess());
  71. }
  72. }
  73. }
  74. private static IBrowserLauncher GetBrowserLauncherInstance(string T)
  75. {
  76. Assembly asm = Assembly.GetExecutingAssembly();
  77. lock (obj)
  78. {
  79. if (!browserLauncherCol.ContainsKey(T))
  80. {
  81. browserLauncherCol[T] = (IBrowserLauncher)asm.CreateInstance(T);
  82. }
  83. }
  84. return browserLauncherCol[T];
  85. }
  86. private static void UnloadProcess(Process p)
  87. {
  88. if (p != null)
  89. {
  90. if (!p.HasExited)
  91. {
  92. p.CloseMainWindow();
  93. }
  94. p.Dispose();
  95. p = null;
  96. }
  97. }
  98. }
  99. }