forked from florentbr/SeleniumBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVerify.cs
More file actions
225 lines (197 loc) · 8.35 KB
/
Verify.cs
File metadata and controls
225 lines (197 loc) · 8.35 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
using Selenium.ComInterfaces;
using Selenium.Core;
using Selenium.Internal;
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace Selenium {
/// <summary>
/// Testing functions. Return the résult of the verification
/// </summary>
/// <example>
///
/// The following example asserts the page title.
/// <code lang="vbs">
/// Set driver = CreateObject("Selenium.WebDriver")
/// Set Verify = CreateObject("Selenium.Verify")
/// driver.start "firefox", "http://www.google.com"
/// driver.get "/"
/// wscript.echo Verify.Equals("Google", driver.Title)
/// driver.stop
/// </code>
///
/// <code lang="vbs">
/// Private Verify As New Selenium.Verify
///
/// Public Sub TestCase()
/// Dim driver As New FirefoxDriver
/// driver.Get "http://www.google.com"
/// Range("A1") = Verify.Equals("Google", driver.Title)
/// driver.Quit
/// End Sub
/// </code>
///
/// </example>
///
[ProgId("Selenium.Verify")]
[Guid("0277FC34-FD1B-4616-BB19-B0C8C528C673")]
[ComVisible(true), ClassInterface(ClassInterfaceType.None)]
[Description("Testing functions. Return the résult of the verification")]
public class Verify : _Verify {
const string MSG_OK = "OK";
const string MSG_KO = "NOK";
private static string format(string message, string template, object arg1, object arg2) {
if (message == null)
message = string.Format(template, ObjExt.ToStrings(arg1), ObjExt.ToStrings(arg2));
return (MSG_KO + ' ' + message).TrimEnd();
}
#region Static API
/// <summary>
/// Tests that an input is true.
/// </summary>
/// <param name="input"></param>
/// <param name="failmessage"></param>
/// <returns></returns>
public static string True(bool input, string failmessage = null) {
return input ? MSG_OK : format(failmessage, "expected={0} was={1}", true, input);
}
/// <summary>
/// Tests that an input is false.
/// </summary>
/// <param name="input"></param>
/// <param name="failmessage"></param>
/// <returns></returns>
public static string False(bool input, string failmessage = null) {
return !input ? MSG_OK : format(failmessage, "expected={0} was={1}", false, input);
}
/// <summary>
/// Tests that two objects are equal.
/// </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">Message to return if the verification fails...</param>
public static string Equals(object expected, object input, string failmessage = null) {
return ObjExt.AreEqual(expected, input) ? MSG_OK
: format(failmessage, "expected={0} was={1}", expected, input);
}
/// <summary>
/// Tests that two objects are not equal.
/// </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">Message to return if the verification fails...</param>
public static string NotEquals(object expected, object input, string failmessage = null) {
return !ObjExt.AreEqual(expected, input) ? MSG_OK
: format(failmessage, "expected!={0} was={1}", expected, input);
}
/// <summary>
/// Tests that an input matches a regular expression pattern.
/// </summary>
/// <param name="pattern"></param>
/// <param name="input"></param>
/// <param name="failmessage"></param>
/// <returns></returns>
public static string Matches(string pattern, string input, string failmessage = null) {
return Regex.IsMatch(input, pattern) ? MSG_OK
: format(failmessage, "pattern={0} was={1}", input, pattern);
}
/// <summary>
/// Tests that an input does not matche a regular expression pattern.
/// </summary>
/// <param name="input"></param>
/// <param name="pattern"></param>
/// <param name="failmessage"></param>
/// <returns></returns>
public static string NotMatches(string pattern, string input, string failmessage = null) {
return !Regex.IsMatch(input, pattern) ? MSG_OK
: format(failmessage, "pattern!={0} was={1}", pattern, input);
}
/// <summary>
/// Tests that an input does not contain a value.
/// </summary>
/// <param name="input"></param>
/// <param name="value"></param>
/// <param name="failmessage"></param>
/// <returns></returns>
public static string Contains(string value, string input, string failmessage = null) {
return input.Contains(value) ? MSG_OK
: format(failmessage, "contains={0} was={1}", value, input);
}
#endregion
#region COM Interface
/// <summary>
/// Create a Verify object.
/// </summary>
public Verify() {
UnhandledException.Initialize();
}
/// <summary>
/// Tests that an input is true.
/// </summary>
/// <param name="input"></param>
/// <param name="failmessage"></param>
/// <returns></returns>
string _Verify.True(bool input, string failmessage) {
return True(input, failmessage);
}
/// <summary>
/// Tests that an input is false.
/// </summary>
/// <param name="input"></param>
/// <param name="failmessage"></param>
/// <returns></returns>
string _Verify.False(bool input, string failmessage) {
return False(input, failmessage);
}
/// <summary>
/// Tests that two objects are equal.
/// </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">Message to return if the verification fails...</param>
string _Verify.Equals(object expected, object input, string failmessage) {
return Equals(expected, input, failmessage);
}
/// <summary>
/// Tests that two objects are not equal.
/// </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">Message to return if the verification fails...</param>
string _Verify.NotEquals(object expected, object input, string failmessage) {
return NotEquals(expected, input, failmessage);
}
/// <summary>
/// Tests that an input matches a regular expression pattern.
/// </summary>
/// <param name="pattern"></param>
/// <param name="input"></param>
/// <param name="failmessage"></param>
/// <returns></returns>
string _Verify.Matches(string pattern, string input, string failmessage) {
return Matches(pattern, input, failmessage);
}
/// <summary>
/// Tests that an input does not matche a regular expression pattern.
/// </summary>
/// <param name="input"></param>
/// <param name="pattern"></param>
/// <param name="failmessage">Optional</param>
/// <returns></returns>
string _Verify.NotMatches(string pattern, string input, string failmessage) {
return NotMatches(pattern, input, failmessage);
}
/// <summary>
/// Tests that an input does not contain a value.
/// </summary>
/// <param name="input"></param>
/// <param name="value"></param>
/// <param name="failmessage">Optional</param>
/// <returns></returns>
string _Verify.Contains(string value, string input, string failmessage) {
return Contains(value, input, failmessage);
}
#endregion
}
}