My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Bricks.Core;
using White.Core.Configuration;
using White.Core.Factory;
using White.Core.Sessions;
using White.Core.UIItems.Finders;
using White.Core.UIItems.WindowItems;

namespace White.Core
{
/// <summary>
/// Represents a process which contains windows.
/// </summary>
public class Application : IDisposable
{
private readonly Process process;
private readonly ApplicationSession applicationSession;
private readonly WindowFactory windowFactory;

protected Application() {}

private Application(Process process)
{
this.process = process;
applicationSession = new ApplicationSession();
applicationSession.Register(this);
windowFactory = WindowFactory.Desktop;
}

/// <summary>
/// Runs the process identified by the executable and creates Application object for this executable
/// </summary>
/// <param name="executable">location of the executable</param>
/// <returns></returns>
public static Application Launch(string executable)
{
var processStartInfo = new ProcessStartInfo(executable);
return Launch(processStartInfo);
}

/// <summary>
/// Lauches the process and creates and Application object for it
/// </summary>
/// <param name="processStartInfo"></param>
/// <returns></returns>
public static Application Launch(ProcessStartInfo processStartInfo)
{
return Attach(Process.Start(processStartInfo));
}

/// <summary>
/// Creates an Application object for existing process
/// </summary>
/// <param name="processId"></param>
/// <returns></returns>
public static Application Attach(int processId)
{
Process process = Process.GetProcessById(processId);
if (process == null) throw new WhiteException("Could not find process with id: " + processId);
return new Application(process);
}

/// <summary>
/// Attaches with existing process
/// </summary>
/// <param name="process"></param>
/// <returns></returns>
public static Application Attach(Process process)
{
return new Application(process);
}

/// <summary>
/// Attaches with existing process
/// </summary>
/// <param name="executable"></param>
/// <returns></returns>
public static Application Attach(string executable)
{
Process[] processes = Process.GetProcessesByName(executable);
if (processes.Length == 0) throw new WhiteException("Could not find process named: " + executable);
return Attach(processes[0]);
}

/// <summary>
/// Attaches to the process if it is running or launches a new process
/// </summary>
/// <param name="processStartInfo"></param>
/// <returns></returns>
public static Application AttachOrLaunch(ProcessStartInfo processStartInfo)
{
string processName = S.ReplaceLast(processStartInfo.FileName, ".exe", string.Empty);
Process[] processes = Process.GetProcessesByName(processName);
if (processes.Length == 0) return Launch(processStartInfo);
return Attach(processes[0]);
}

/// <summary>
/// Name of the process
/// </summary>
public virtual string Name
{
get { return process.ProcessName; }
}

/// <summary>
/// Internal to white
/// </summary>
public virtual ApplicationSession ApplicationSession
{
get { return applicationSession; }
}

/// <summary>
/// Get visible window
/// </summary>
/// <param name="title">Title text of window displayed on desktop</param>
/// <param name="option">Option which would be used to initialize the window.</param>
/// <returns></returns>
public virtual Window GetWindow(string title, InitializeOption option)
{
WindowSession windowSession = applicationSession.WindowSession(option);
return windowFactory.CreateWindow(title, process, option, windowSession);
}

/// <summary>
/// Get visible window
/// </summary>
/// <param name="title">Title text of window displayed on desktop</param>
/// <returns></returns>
public virtual Window GetWindow(string title)
{
return GetWindow(title, InitializeOption.NoCache);
}

/// <summary>
/// Find the first window which belongs to this application and satisfies the critera.
/// </summary>
/// <param name="searchCriteria"></param>
/// <param name="initializeOption">found window would be initialized with this option</param>
/// <returns></returns>
public virtual Window GetWindow(SearchCriteria searchCriteria, InitializeOption initializeOption)
{
WindowSession windowSession = applicationSession.WindowSession(initializeOption);
return windowFactory.CreateWindow(searchCriteria, process, initializeOption, windowSession);
}

/// <summary>
/// Kills the applications and waits till it is closed
/// </summary>
public virtual void Kill()
{
try
{
process.Kill();
process.WaitForExit();
}
catch {}
}

/// <summary>
/// All windows belonging to the application
/// </summary>
/// <returns></returns>
public virtual List<Window> GetWindows()
{
return windowFactory.DesktopWindows(process, new NoApplicationSession());
}

/// <summary>
/// Returns whether process has exited
/// </summary>
public virtual bool HasExited
{
get { return process.HasExited; }
}

/// <summary>
/// Two applications are equal if they have the same process
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public override bool Equals(object obj)
{
if (this == obj) return true;
var application = obj as Application;
if (application == null) return false;
return Equals(process, application.process);
}

public override int GetHashCode()
{
return process.GetHashCode();
}

public virtual void Dispose()
{
Kill();
}

public virtual Window FindSplash()
{
return windowFactory.SplashWindow(process);
}

/// <summary>
/// Waits till application is busy.
/// </summary>
public virtual void WaitWhileBusy()
{
process.WaitForInputIdle(CoreAppXmlConfiguration.Instance.BusyTimeout);
}

/// <summary>
/// Looks at all the windows visible for the application and finds one which matches the condition. The match is run against the title
/// of the windows
/// </summary>
/// <param name="match"></param>
/// <param name="initializeOption">option for the window which matches the condition</param>
public virtual Window Find(Predicate<string> match, InitializeOption initializeOption)
{
WindowSession windowSession = applicationSession.WindowSession(initializeOption);
return windowFactory.FindWindow(process, match, initializeOption, windowSession);
}

public virtual Process Process
{
get { return process; }
}

/// <summary>
/// Kills the application. Read Application.Kill.
/// It also saves the application test execution state. This saves the position of the window UIItems which would be loaded next time
/// automatically for improved performance. You would need to use InitializedOption.AndIdentifiedBy for specifying the identification of window.
/// </summary>
public virtual void KillAndSaveState()
{
Kill();
ApplicationSession.Save();
}
}
}

Change log

r9 by petmongrels on Nov 16, 2009   Diff
creating project
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 8751 bytes, 244 lines
Powered by Google Project Hosting