forked from florentbr/SeleniumBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaiter.cs
More file actions
290 lines (264 loc) · 10.2 KB
/
Waiter.cs
File metadata and controls
290 lines (264 loc) · 10.2 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using Selenium.Core;
using Selenium.Internal;
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
namespace Selenium {
/// <summary>
/// Waiting functions
/// </summary>
[ProgId("Selenium.Waiter")]
[Guid("0277FC34-FD1B-4616-BB19-7D30CBC3F6BB")]
[ComVisible(true), ClassInterface(ClassInterfaceType.None)]
[Description("Waiting functions to keep the visual basic editor from not responding")]
public class Waiter : ComInterfaces._Waiter {
private DateTime? _endtime;
private int _timeout = 5000;
private int _delayms = 60;
/// <summary>
/// Creates a new Waiter object
/// </summary>
public Waiter() {
}
/// <summary>
/// Waiter timeout in milliseconds. Default is 5000 milliseconds
/// </summary>
public int Timeout {
get { return (int)_timeout; }
set { _timeout = value; }
}
/// <summary>
/// Delay in millisecond before trying again. Default is 60 milliseconds
/// </summary>
public int RetryDelay {
get { return (int)_delayms; }
set { _delayms = value; }
}
/// <summary>
/// Waits the given time in milliseconds
/// </summary>
/// <param name="timems">Time to wait in milliseconde</param>
void ComInterfaces._Waiter.Wait(int timems) {
SysWaiter.Wait(timems);
}
/// <summary>
/// Returns a boolean to continue waiting and throws an exception if the timeout is reached
/// </summary>
/// <param name="result">Result</param>
/// <param name="timeout">Optional - Timeout in millisecond</param>
/// <param name="timeoutMessage">Optional - Message in case of timeout</param>
/// <returns>Returns false if the result is true, false otherwise.</returns>
/// <example>
///
/// <code lang="vbs">
/// Public Sub Script()
/// Dim driver As New FirefoxDriver, Waiter As New Waiter
/// driver.Get "http://www.mywebsite.com/"
/// While Waiter.Not(driver.Title = "MyTitle"): Wend
/// ...
/// End Sub
/// </code>
/// </example>
public bool Not(object result, int timeout = -1, string timeoutMessage = null) {
if (_endtime == null) {
_endtime = DateTime.UtcNow.AddMilliseconds(timeout == -1 ? _timeout : timeout);
} else {
if (DateTime.UtcNow > _endtime) {
_endtime = null;
if (timeoutMessage != null)
throw new Errors.TimeoutError(timeoutMessage);
throw new Errors.TimeoutError(timeout == -1 ? _timeout : timeout);
}
try {
SysWaiter.Wait(_delayms);
} catch {
_endtime = null;
throw;
}
}
if (result == null || false.Equals(result))
return true;
_endtime = null;
return false;
}
/// <summary>
/// Waits for the delegate function to return not null or true
/// </summary>
/// <typeparam name="T">Returned object or boolean</typeparam>
/// <param name="func">Delegate function</param>
/// <param name="timeout">Timeout in milliseconds</param>
/// <returns>Variant or boolean</returns>
public T Until<T>(Func<T> func, int timeout = -1) {
if (timeout == -1)
timeout = _timeout;
return Waiter.WaitUntil(func, timeout, _delayms);
}
/// <summary>
/// Waits for a procedure to set the result argument to true.
/// Procedure:
/// </summary>
/// <param name="procedure">Procedure</param>
/// <param name="argument">Argument</param>
/// <param name="timeout">Timeout in ms</param>
/// <param name="timeoutMessage">Timeout message</param>
/// <returns></returns>
/// <example>
/// VBA:
/// <code lang="vbs">
/// Public Sub WaitForTitle(driver, arg)
/// WaitForTitle = driver.Title = arg
/// End Sub
///
/// Public Sub Script()
/// Dim Waiter As New Waiter
/// Dim driver As New FirefoxDriver
/// driver.Get "http://www.google.com/"
/// Waiter.Until AddressOf WaitForTitle, driver, "Google"
/// ...
/// End Sub
/// </code>
///
/// VBScript:
/// <code lang="vbs">
/// Public Sub WaitForTitle(driver, arg)
/// WaitForTitle = driver.Title = arg
/// End Sub
///
/// Public Sub Script()
/// Set Waiter = CreateObject("Selenium.Waiter")
/// Set driver = CreateObject("Selenium.FirefoxDriver")
/// driver.Get "http://www.google.com/"
/// Waiter.Until GetRef("WaitForTitle"), 5000, driver, "Google"
/// ...
/// End Sub
/// </code>
/// </example>
object ComInterfaces._Waiter.Until(object procedure, object argument, int timeout, string timeoutMessage) {
if (timeout == -1)
timeout = _timeout;
return COMExt.WaitUntilProc(procedure, argument, null, timeout);
}
/// <summary>
/// Waits the given time in milliseconds
/// </summary>
/// <param name="timems">Time to wait in milliseconde</param>
public static void Wait(int timems) {
SysWaiter.Wait(timems);
}
/// <summary>
/// Waits for a delegate function to be executed without exception
/// </summary>
/// <param name="func">Delegate action</param>
/// <param name="timeoums">Timeout in millisecond</param>
/// <param name="sleepms">Delay between each attempt</param>
/// <returns>Delegate result</returns>
public static T WaitNoException<T>(Func<T> func, int timeoums, int sleepms) {
var endTime = DateTime.UtcNow.AddMilliseconds(timeoums);
while (true) {
try {
return func();
} catch {
if (DateTime.UtcNow > endTime)
throw;
}
SysWaiter.Wait(sleepms);
}
}
/// <summary>
/// Waits for a delegate action to be executed without exception
/// </summary>
/// <param name="action">Delegate action</param>
/// <param name="timeoums">Timeout in millisecond</param>
/// <param name="sleepms">Delay between each attempt</param>
public static void WaitNoException(Action action, int timeoums, int sleepms) {
var endTime = DateTime.UtcNow.AddMilliseconds(timeoums);
while (true) {
try {
action();
return;
} catch {
if (DateTime.UtcNow > endTime)
throw;
}
SysWaiter.Wait(sleepms);
}
}
/// <summary>
/// Waits for a delegate function to return true or not null
/// </summary>
/// <typeparam name="U"></typeparam>
/// <typeparam name="T"></typeparam>
/// <param name="func"></param>
/// <param name="args"></param>
/// <param name="timeoums"></param>
/// <param name="sleepms"></param>
/// <returns></returns>
public static T WaitUntil<U, T>(Func<U, T> func, U args, int timeoums, int sleepms) {
var endTime = DateTime.UtcNow.AddMilliseconds(timeoums);
while (true) {
T result = func(args);
if (IsNotNullOrFalse(result))
return result;
if (DateTime.UtcNow > endTime)
throw new Errors.TimeoutError(timeoums);
SysWaiter.Wait(sleepms);
}
}
/// <summary>
/// Waits for a delegate function to return true or not null
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="func"></param>
/// <param name="timeoums"></param>
/// <param name="sleepms"></param>
/// <returns></returns>
public static T WaitUntil<T>(Func<T> func, int timeoums, int sleepms) {
var endTime = DateTime.UtcNow.AddMilliseconds(timeoums);
while (true) {
T result = func();
if (IsNotNullOrFalse(result))
return result;
if (DateTime.UtcNow > endTime)
throw new Errors.TimeoutError(timeoums);
SysWaiter.Wait(sleepms);
}
}
/// <summary>
/// Waits for a delegate function to return true
/// </summary>
/// <param name="func">Delegate action</param>
/// <param name="timeoums">Timeout in millisecond</param>
/// <param name="sleepms">Delay between each attempt</param>
/// <returns>True if succeed, false otherwise</returns>
public static bool WaitTrue(Func<bool> func, int timeoums, int sleepms) {
var endTime = DateTime.UtcNow.AddMilliseconds(timeoums);
while (true) {
if (func())
return true;
if (DateTime.UtcNow > endTime)
return false;
SysWaiter.Wait(sleepms);
}
}
private static bool IsNotNullOrFalse<T>(T value) {
return !(value == null || value is bool && (bool)(object)value == false);
}
/// <summary>
/// Waits for a file to be ready.
/// </summary>
/// <param name="path">File path</param>
/// <param name="timeout">Timeout in milliseonds</param>
public void WaitForFile(string path, int timeout = -1) {
Waiter.WaitUntil(() => {
if (File.Exists(path)) {
try {
File.Move(path, path);
return true;
} catch (System.IO.IOException) { }
}
return false;
}, timeout, 100);
}
}
}