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