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