forked from florentbr/SeleniumBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssert.cs
More file actions
248 lines (215 loc) · 8.98 KB
/
Assert.cs
File metadata and controls
248 lines (215 loc) · 8.98 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
using Selenium.ComInterfaces;
using Selenium.Core;
using Selenium.Internal;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace Selenium {
/// <summary>
/// Testing functions. Throws an exception if the condition is not met
/// </summary>
/// <example>
///
/// The following example asserts the page title.
/// <code lang="vbs">
/// Set Assert = CreateObject("Selenium.Assert")
/// Set driver = CreateObject("Selenium.FirefoxDriver")
/// driver.get "http://www.google.com"
/// Assert.Equals "Google", driver.Title
/// driver.stop
/// </code>
///
/// <code lang="vbs">
/// Private Assert As New Selenium.Assert
///
/// Public Sub TestCase()
/// Dim driver As New Selenium.FirefoxDriver
/// driver.get "http://www.google.com"
/// Assert.Equals "Google", driver.Title
/// driver.stop
/// End Sub
/// </code>
///
/// </example>
[ProgId("Selenium.Assert")]
[Description("Testing functions. Throws an exception if the condition is not met")]
[Guid("0277FC34-FD1B-4616-BB19-6AAF7EDD33D6")]
[ComVisible(true), ClassInterface(ClassInterfaceType.None)]
public class Assert : _Assert {
#region Static API
/// <summary>
/// Raise an error if the value is true.
/// </summary>
/// <param name="input"></param>
/// <param name="failmessage"></param>
public static void True(bool input, string failmessage = null) {
if (input != true)
throw new AssertFailure(failmessage, "expected={0}\nwas={1}", true, input);
}
/// <summary>
/// Raise an error if the value is false.
/// </summary>
/// <param name="input"></param>
/// <param name="failmessage"></param>
public static void False(bool input, string failmessage = null) {
if (input != false)
throw new AssertFailure(failmessage, "expected={0}\nwas={1}", false, input);
}
/// <summary>
/// Test that two objects are equal and raise an exception if the result is false
/// </summary>
/// <param name="expected">expected object. Can be a string, number, array...</param>
/// <param name="input">current object. Can be a string, number, array...</param>
/// <param name="failmessage"></param>
public static void Equals(Object expected, Object input, string failmessage = null) {
if (!ObjExt.AreEqual(expected, input))
throw new AssertFailure(failmessage, "expected={0}\nwas={1}", expected, input);
}
/// <summary>
/// Test that two objects are not equal and raise an exception if the result is false
/// </summary>
/// <param name="notexpected">expected object. Can be a string, number, array...</param>
/// <param name="input">current object. Can be a string, number, array...</param>
/// <param name="failmessage"></param>
public static void NotEquals(Object notexpected, Object input, string failmessage = null) {
if (ObjExt.AreEqual(notexpected, input))
throw new AssertFailure(failmessage, "expected!={0}\nwas={1}", notexpected, input);
}
/// <summary>
/// Raise an error if the text matches the pattern.
/// </summary>
/// <param name="pattern"></param>
/// <param name="input"></param>
/// <param name="failmessage"></param>
public static void Matches(string pattern, string input, string failmessage = null) {
if (!Regex.IsMatch(input, pattern))
throw new AssertFailure(failmessage, "pattern={0}\nwas={1}", pattern, input);
}
/// <summary>
/// Raise an error if the text does not match the pattern.
/// </summary>
/// <param name="pattern"></param>
/// <param name="input"></param>
/// <param name="failmessage"></param>
public static void NotMatches(string pattern, string input, string failmessage = null) {
if (Regex.IsMatch(input, pattern))
throw new AssertFailure(failmessage, "pattern!={0}\nwas={1}", pattern, input);
}
/// <summary>
/// Raise an error if the text does not contain the value.
/// </summary>
/// <param name="input"></param>
/// <param name="value"></param>
/// <param name="failmessage"></param>
public static void Contains(string value, string input, string failmessage = null) {
if (!input.Contains(value))
throw new AssertFailure(failmessage, "contains={0}\nwas={1}", value, input);
}
/// <summary>
/// Raise an error.
/// </summary>
/// <param name="message"></param>
public static void Fail(string message = null) {
throw new AssertFailure(message);
}
#endregion
#region COM Interface
/// <summary>
/// Create an assert object.
/// </summary>
public Assert() {
UnhandledException.Initialize();
}
/// <summary>
/// Raise an error if the value is true.
/// </summary>
/// <param name="input"></param>
/// <param name="failmessage"></param>
void _Assert.True(bool input, string failmessage) {
True(input, failmessage);
}
/// <summary>
/// Raise an error if the value is false.
/// </summary>
/// <param name="input"></param>
/// <param name="failmessage"></param>
void _Assert.False(bool input, string failmessage) {
False(input, failmessage);
}
/// <summary>
/// Test that two objects are equal and raise an exception if the result is false
/// </summary>
/// <param name="expected">expected object. Can be a string, number, array...</param>
/// <param name="input">current object. Can be a string, number, array...</param>
/// <param name="failmessage"></param>
void _Assert.Equals(Object expected, Object input, string failmessage) {
Equals(expected, input, failmessage);
}
/// <summary>
/// Test that two objects are not equal and raise an exception if the result is false
/// </summary>
/// <param name="notexpected">expected object. Can be a string, number, array...</param>
/// <param name="input">current object. Can be a string, number, array...</param>
/// <param name="failmessage"></param>
void _Assert.NotEquals(Object notexpected, Object input, string failmessage) {
NotEquals(notexpected, input, failmessage);
}
/// <summary>
/// Raise an error if the text matches the pattern.
/// </summary>
/// <param name="pattern"></param>
/// <param name="input"></param>
/// <param name="failmessage"></param>
void _Assert.Matches(string pattern, string input, string failmessage) {
Matches(pattern, input, failmessage);
}
/// <summary>
/// Raise an error if the text does not match the pattern.
/// </summary>
/// <param name="pattern"></param>
/// <param name="input"></param>
/// <param name="failmessage"></param>
void _Assert.NotMatches(string pattern, string input, string failmessage) {
NotMatches(pattern, input, failmessage);
}
/// <summary>
/// Raise an error if the text does not contain the value.
/// </summary>
/// <param name="input"></param>
/// <param name="value"></param>
/// <param name="failmessage"></param>
void _Assert.Contains(string value, string input, string failmessage) {
Contains(value, input, failmessage);
}
/// <summary>
/// Raise an error.
/// </summary>
/// <param name="message"></param>
void _Assert.Fail(string message) {
Fail(message);
}
#endregion
class AssertFailure : SeleniumException {
const int HRESULT = -2146828288;
string _message;
public AssertFailure(string message, string template, object arg1, object arg2) {
base.HResult = HRESULT;
_message = message ?? string.Format(template
, ObjExt.ToStrings(arg1), ObjExt.ToStrings(arg2));
}
public AssertFailure(string message) {
base.HResult = HRESULT;
_message = message ?? string.Empty;
}
public override string Message {
get {
var frame = new StackTrace(this, true).GetFrame(0);
var method = frame.GetMethod().Name;
return string.Format("Assert.{0} failed!\n{1}", method, _message).TrimEnd();
}
}
}
}
}