PageRenderTime 74ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/V4/AcceptanceTestLibrary/AcceptanceTestLibrary/Common/Launcher/Silverlight/BrowserLauncherBase.cs

#
C# | 89 lines | 59 code | 11 blank | 19 comment | 3 complexity | 6897700182be3b42a25122109ac553ba 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.Windows.Automation;
  22. using System.Diagnostics;
  23. using System.Threading;
  24. using AcceptanceTestLibrary.ApplicationHelper;
  25. namespace AcceptanceTestLibrary.Common.Launcher.Silverlight
  26. {
  27. public abstract class BrowserLauncherBase : IBrowserLauncher
  28. {
  29. public virtual AutomationElement LaunchBrowser(string silverlightApplicationPath, string applicationTitle)
  30. {
  31. try
  32. {
  33. //This method will contain the implementation for launching the silverlight application
  34. ApplicationTitleInBrowser = applicationTitle;
  35. Process appProcess = Process.Start(GetBrowserPath(), silverlightApplicationPath);
  36. ProcessId = appProcess.Id;
  37. Thread.Sleep(15000);
  38. //Get the current process and return that
  39. Process targetProcess = GetCurrentAppProcess();
  40. if (!(targetProcess.HasExited || targetProcess.MainWindowHandle == IntPtr.Zero))
  41. {
  42. return (AutomationElement.FromHandle(targetProcess.MainWindowHandle));
  43. }
  44. else
  45. {
  46. return null;
  47. }
  48. }
  49. catch (Exception)
  50. {
  51. return null;
  52. }
  53. }
  54. public virtual Process GetCurrentAppProcess()
  55. {
  56. try
  57. {
  58. //Finding out process based on the title is a temporary design. Code will be enhanced to get the process using other options.
  59. return Process.GetProcesses().First<Process>(proc =>
  60. proc.ProcessName.Equals(ConfigHandler.GetValue(GetBrowserProcessName())) &&
  61. proc.MainWindowTitle.StartsWith(ApplicationTitleInBrowser));
  62. }
  63. catch (Exception)
  64. {
  65. return null;
  66. }
  67. }
  68. public virtual string ApplicationTitleInBrowser
  69. { get; set; }
  70. public abstract string GetBrowserPath();
  71. public abstract string GetBrowserProcessName();
  72. internal int ProcessId
  73. {
  74. get;
  75. set;
  76. }
  77. }
  78. }