Skip to content

Commit 4d44b47

Browse files
committed
Finish migration of .NET By implementation
1 parent f0f6a24 commit 4d44b47

File tree

2 files changed

+36
-99
lines changed

2 files changed

+36
-99
lines changed

dotnet/src/webdriver/By.cs

+36-53
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ namespace OpenQA.Selenium
3636
[Serializable]
3737
public class By
3838
{
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+
3945
private string description = "OpenQA.Selenium.By";
4046
private string mechanism = string.Empty;
4147
private string criteria = string.Empty;
@@ -49,6 +55,23 @@ protected By()
4955
{
5056
}
5157

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+
5275
/// <summary>
5376
/// Initializes a new instance of the <see cref="By"/> class using the given functions to find elements.
5477
/// </summary>
@@ -152,13 +175,9 @@ public static By Id(string idToFind)
152175
throw new ArgumentNullException("idToFind", "Cannot find elements with a null id attribute.");
153176
}
154177

155-
By by = new By();
156-
by.description = "By.Id: " + idToFind;
157-
by.mechanism = "css selector";
158178
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;
162181
if (string.IsNullOrEmpty(selector))
163182
{
164183
// Finding multiple elements with an empty ID will return
@@ -167,10 +186,6 @@ public static By Id(string idToFind)
167186
// which means we need to short-circuit that behavior.
168187
by.findElementsMethod = (ISearchContext context) => new List<IWebElement>().AsReadOnly();
169188
}
170-
else
171-
{
172-
by.findElementsMethod = (ISearchContext context) => ((IFindsElement)context).FindElements(by.mechanism, by.criteria);
173-
}
174189

175190
return by;
176191
}
@@ -187,13 +202,8 @@ public static By LinkText(string linkTextToFind)
187202
throw new ArgumentNullException("linkTextToFind", "Cannot find elements when link text is null.");
188203
}
189204

190-
By by = new By();
205+
By by = new By(LinkTextMechanism, linkTextToFind);
191206
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);
197207
return by;
198208
}
199209

@@ -209,13 +219,9 @@ public static By Name(string nameToFind)
209219
throw new ArgumentNullException("nameToFind", "Cannot find elements when name text is null.");
210220
}
211221

212-
By by = new By();
222+
string selector = "*[name =\"" + By.EscapeCssSelector(nameToFind) + "\"]";
223+
By by = new By(CssSelectorMechanism, selector);
213224
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);
219225
return by;
220226
}
221227

@@ -234,13 +240,8 @@ public static By XPath(string xpathToFind)
234240
throw new ArgumentNullException("xpathToFind", "Cannot find elements when the XPath expression is null.");
235241
}
236242

237-
By by = new By();
243+
By by = new By(XPathSelectorMechanism, xpathToFind);
238244
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);
244245
return by;
245246
}
246247

@@ -259,11 +260,8 @@ public static By ClassName(string classNameToFind)
259260
throw new ArgumentNullException("classNameToFind", "Cannot find elements when the class name expression is null.");
260261
}
261262

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(" "))
267265
{
268266
// Finding elements by class name with whitespace is not allowed.
269267
// However, converting the single class name to a valid CSS selector
@@ -272,8 +270,8 @@ public static By ClassName(string classNameToFind)
272270
throw new InvalidSelectorException("Compound class names not allowed. Cannot have whitespace in class name. Use CSS selectors instead.");
273271
}
274272

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;
277275
return by;
278276
}
279277

@@ -289,13 +287,8 @@ public static By PartialLinkText(string partialLinkTextToFind)
289287
throw new ArgumentNullException("partialLinkTextToFind", "Cannot find elements when partial link text is null.");
290288
}
291289

292-
By by = new By();
290+
By by = new By(PartialLinkTextMechanism, partialLinkTextToFind);
293291
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);
299292
return by;
300293
}
301294

@@ -311,13 +304,8 @@ public static By TagName(string tagNameToFind)
311304
throw new ArgumentNullException("tagNameToFind", "Cannot find elements when name tag name is null.");
312305
}
313306

314-
By by = new By();
307+
By by = new By(TagNameMechanism, tagNameToFind);
315308
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);
321309
return by;
322310
}
323311

@@ -333,13 +321,8 @@ public static By CssSelector(string cssSelectorToFind)
333321
throw new ArgumentNullException("cssSelectorToFind", "Cannot find elements when name CSS selector is null.");
334322
}
335323

336-
By by = new By();
324+
By by = new By(CssSelectorMechanism, cssSelectorToFind);
337325
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);
343326
return by;
344327
}
345328

dotnet/test/common/ByTest.cs

-46
This file was deleted.

0 commit comments

Comments
 (0)