@@ -36,6 +36,12 @@ namespace OpenQA.Selenium
36
36
[ Serializable ]
37
37
public class By
38
38
{
39
+ private static readonly string CssSelectorMechanism = "css selector" ;
40
+ private static readonly string XPathSelectorMechanism = "xpath" ;
41
+ private static readonly string TagNameMechanism = "tag name" ;
42
+ private static readonly string LinkTextMechanism = "link text" ;
43
+ private static readonly string PartialLinkTextMechanism = "partial link text" ;
44
+
39
45
private string description = "OpenQA.Selenium.By" ;
40
46
private string mechanism = string . Empty ;
41
47
private string criteria = string . Empty ;
@@ -49,6 +55,23 @@ protected By()
49
55
{
50
56
}
51
57
58
+ /// <summary>
59
+ /// Intializes a new instance of the <see cref="By"/> class using the specified mechanism and critieria for finding elements.
60
+ /// </summary>
61
+ /// <param name="mechanism">The mechanism to use in finding elements.</param>
62
+ /// <param name="criteria">The criteria to use in finding elements.</param>
63
+ /// <remarks>
64
+ /// Customizing nothing else, instances using this constructor will attempt to find elemsnts
65
+ /// using the <see cref="IFindsElement.FindElement(string, string)"/> method, taking string arguments.
66
+ /// </remarks>
67
+ protected By ( string mechanism , string criteria )
68
+ {
69
+ this . mechanism = mechanism ;
70
+ this . criteria = criteria ;
71
+ this . findElementMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElement ( this . mechanism , this . criteria ) ;
72
+ this . findElementsMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElements ( this . mechanism , this . criteria ) ;
73
+ }
74
+
52
75
/// <summary>
53
76
/// Initializes a new instance of the <see cref="By"/> class using the given functions to find elements.
54
77
/// </summary>
@@ -152,13 +175,9 @@ public static By Id(string idToFind)
152
175
throw new ArgumentNullException ( "idToFind" , "Cannot find elements with a null id attribute." ) ;
153
176
}
154
177
155
- By by = new By ( ) ;
156
- by . description = "By.Id: " + idToFind ;
157
- by . mechanism = "css selector" ;
158
178
string selector = By . EscapeCssSelector ( idToFind ) ;
159
- by . criteria = "#" + selector ;
160
-
161
- by . findElementMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElement ( by . mechanism , by . criteria ) ;
179
+ By by = new By ( CssSelectorMechanism , "#" + selector ) ;
180
+ by . description = "By.Id: " + idToFind ;
162
181
if ( string . IsNullOrEmpty ( selector ) )
163
182
{
164
183
// Finding multiple elements with an empty ID will return
@@ -167,10 +186,6 @@ public static By Id(string idToFind)
167
186
// which means we need to short-circuit that behavior.
168
187
by . findElementsMethod = ( ISearchContext context ) => new List < IWebElement > ( ) . AsReadOnly ( ) ;
169
188
}
170
- else
171
- {
172
- by . findElementsMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElements ( by . mechanism , by . criteria ) ;
173
- }
174
189
175
190
return by ;
176
191
}
@@ -187,13 +202,8 @@ public static By LinkText(string linkTextToFind)
187
202
throw new ArgumentNullException ( "linkTextToFind" , "Cannot find elements when link text is null." ) ;
188
203
}
189
204
190
- By by = new By ( ) ;
205
+ By by = new By ( LinkTextMechanism , linkTextToFind ) ;
191
206
by . description = "By.LinkText: " + linkTextToFind ;
192
- by . mechanism = "link text" ;
193
- by . criteria = linkTextToFind ;
194
-
195
- by . findElementMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElement ( by . mechanism , by . criteria ) ;
196
- by . findElementsMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElements ( by . mechanism , by . criteria ) ;
197
207
return by ;
198
208
}
199
209
@@ -209,13 +219,9 @@ public static By Name(string nameToFind)
209
219
throw new ArgumentNullException ( "nameToFind" , "Cannot find elements when name text is null." ) ;
210
220
}
211
221
212
- By by = new By ( ) ;
222
+ string selector = "*[name =\" " + By . EscapeCssSelector ( nameToFind ) + "\" ]" ;
223
+ By by = new By ( CssSelectorMechanism , selector ) ;
213
224
by . description = "By.Name: " + nameToFind ;
214
- by . mechanism = "css selector" ;
215
- by . criteria = "*[name =\" " + By . EscapeCssSelector ( nameToFind ) + "\" ]" ;
216
-
217
- by . findElementMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElement ( by . mechanism , by . criteria ) ;
218
- by . findElementsMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElements ( by . mechanism , by . criteria ) ;
219
225
return by ;
220
226
}
221
227
@@ -234,13 +240,8 @@ public static By XPath(string xpathToFind)
234
240
throw new ArgumentNullException ( "xpathToFind" , "Cannot find elements when the XPath expression is null." ) ;
235
241
}
236
242
237
- By by = new By ( ) ;
243
+ By by = new By ( XPathSelectorMechanism , xpathToFind ) ;
238
244
by . description = "By.XPath: " + xpathToFind ;
239
- by . mechanism = "xpath" ;
240
- by . criteria = xpathToFind ;
241
-
242
- by . findElementMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElement ( by . mechanism , by . criteria ) ;
243
- by . findElementsMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElements ( by . mechanism , by . criteria ) ;
244
245
return by ;
245
246
}
246
247
@@ -259,11 +260,8 @@ public static By ClassName(string classNameToFind)
259
260
throw new ArgumentNullException ( "classNameToFind" , "Cannot find elements when the class name expression is null." ) ;
260
261
}
261
262
262
- By by = new By ( ) ;
263
- by . description = "By.ClassName[Contains]: " + classNameToFind ;
264
- by . mechanism = "css selector" ;
265
- by . criteria = "." + By . EscapeCssSelector ( classNameToFind ) ;
266
- if ( by . criteria . Contains ( " " ) )
263
+ string selector = "." + By . EscapeCssSelector ( classNameToFind ) ;
264
+ if ( selector . Contains ( " " ) )
267
265
{
268
266
// Finding elements by class name with whitespace is not allowed.
269
267
// However, converting the single class name to a valid CSS selector
@@ -272,8 +270,8 @@ public static By ClassName(string classNameToFind)
272
270
throw new InvalidSelectorException ( "Compound class names not allowed. Cannot have whitespace in class name. Use CSS selectors instead." ) ;
273
271
}
274
272
275
- by . findElementMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElement ( by . mechanism , by . criteria ) ;
276
- by . findElementsMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElements ( by . mechanism , by . criteria ) ;
273
+ By by = new By ( CssSelectorMechanism , selector ) ;
274
+ by . description = "By.ClassName[Contains]: " + classNameToFind ;
277
275
return by ;
278
276
}
279
277
@@ -289,13 +287,8 @@ public static By PartialLinkText(string partialLinkTextToFind)
289
287
throw new ArgumentNullException ( "partialLinkTextToFind" , "Cannot find elements when partial link text is null." ) ;
290
288
}
291
289
292
- By by = new By ( ) ;
290
+ By by = new By ( PartialLinkTextMechanism , partialLinkTextToFind ) ;
293
291
by . description = "By.PartialLinkText: " + partialLinkTextToFind ;
294
- by . mechanism = "partial link text" ;
295
- by . criteria = partialLinkTextToFind ;
296
-
297
- by . findElementMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElement ( by . mechanism , by . criteria ) ;
298
- by . findElementsMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElements ( by . mechanism , by . criteria ) ;
299
292
return by ;
300
293
}
301
294
@@ -311,13 +304,8 @@ public static By TagName(string tagNameToFind)
311
304
throw new ArgumentNullException ( "tagNameToFind" , "Cannot find elements when name tag name is null." ) ;
312
305
}
313
306
314
- By by = new By ( ) ;
307
+ By by = new By ( TagNameMechanism , tagNameToFind ) ;
315
308
by . description = "By.TagName: " + tagNameToFind ;
316
- by . mechanism = "css selector" ;
317
- by . criteria = tagNameToFind ;
318
-
319
- by . findElementMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElement ( by . mechanism , by . criteria ) ;
320
- by . findElementsMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElements ( by . mechanism , by . criteria ) ;
321
309
return by ;
322
310
}
323
311
@@ -333,13 +321,8 @@ public static By CssSelector(string cssSelectorToFind)
333
321
throw new ArgumentNullException ( "cssSelectorToFind" , "Cannot find elements when name CSS selector is null." ) ;
334
322
}
335
323
336
- By by = new By ( ) ;
324
+ By by = new By ( CssSelectorMechanism , cssSelectorToFind ) ;
337
325
by . description = "By.CssSelector: " + cssSelectorToFind ;
338
- by . mechanism = "css selector" ;
339
- by . criteria = cssSelectorToFind ;
340
-
341
- by . findElementMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElement ( by . mechanism , by . criteria ) ;
342
- by . findElementsMethod = ( ISearchContext context ) => ( ( IFindsElement ) context ) . FindElements ( by . mechanism , by . criteria ) ;
343
326
return by ;
344
327
}
345
328
0 commit comments