Skip to content

Commit

Permalink
Finish migration of .NET By implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jimevans committed Nov 30, 2020
1 parent f0f6a24 commit 4d44b47
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 99 deletions.
89 changes: 36 additions & 53 deletions dotnet/src/webdriver/By.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ namespace OpenQA.Selenium
[Serializable]
public class By
{
private static readonly string CssSelectorMechanism = "css selector";
private static readonly string XPathSelectorMechanism = "xpath";
private static readonly string TagNameMechanism = "tag name";
private static readonly string LinkTextMechanism = "link text";
private static readonly string PartialLinkTextMechanism = "partial link text";

private string description = "OpenQA.Selenium.By";
private string mechanism = string.Empty;
private string criteria = string.Empty;
Expand All @@ -49,6 +55,23 @@ protected By()
{
}

/// <summary>
/// Intializes a new instance of the <see cref="By"/> class using the specified mechanism and critieria for finding elements.
/// </summary>
/// <param name="mechanism">The mechanism to use in finding elements.</param>
/// <param name="criteria">The criteria to use in finding elements.</param>
/// <remarks>
/// Customizing nothing else, instances using this constructor will attempt to find elemsnts
/// using the <see cref="IFindsElement.FindElement(string, string)"/> method, taking string arguments.
/// </remarks>
protected By(string mechanism, string criteria)
{
this.mechanism = mechanism;
this.criteria = criteria;
this.findElementMethod = (ISearchContext context) => ((IFindsElement)context).FindElement(this.mechanism, this.criteria);
this.findElementsMethod = (ISearchContext context) => ((IFindsElement)context).FindElements(this.mechanism, this.criteria);
}

/// <summary>
/// Initializes a new instance of the <see cref="By"/> class using the given functions to find elements.
/// </summary>
Expand Down Expand Up @@ -152,13 +175,9 @@ public static By Id(string idToFind)
throw new ArgumentNullException("idToFind", "Cannot find elements with a null id attribute.");
}

By by = new By();
by.description = "By.Id: " + idToFind;
by.mechanism = "css selector";
string selector = By.EscapeCssSelector(idToFind);
by.criteria = "#" + selector;

by.findElementMethod = (ISearchContext context) => ((IFindsElement)context).FindElement(by.mechanism, by.criteria);
By by = new By(CssSelectorMechanism, "#" + selector);
by.description = "By.Id: " + idToFind;
if (string.IsNullOrEmpty(selector))
{
// Finding multiple elements with an empty ID will return
Expand All @@ -167,10 +186,6 @@ public static By Id(string idToFind)
// which means we need to short-circuit that behavior.
by.findElementsMethod = (ISearchContext context) => new List<IWebElement>().AsReadOnly();
}
else
{
by.findElementsMethod = (ISearchContext context) => ((IFindsElement)context).FindElements(by.mechanism, by.criteria);
}

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

By by = new By();
By by = new By(LinkTextMechanism, linkTextToFind);
by.description = "By.LinkText: " + linkTextToFind;
by.mechanism = "link text";
by.criteria = linkTextToFind;

by.findElementMethod = (ISearchContext context) => ((IFindsElement)context).FindElement(by.mechanism, by.criteria);
by.findElementsMethod = (ISearchContext context) => ((IFindsElement)context).FindElements(by.mechanism, by.criteria);
return by;
}

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

By by = new By();
string selector = "*[name =\"" + By.EscapeCssSelector(nameToFind) + "\"]";
By by = new By(CssSelectorMechanism, selector);
by.description = "By.Name: " + nameToFind;
by.mechanism = "css selector";
by.criteria = "*[name =\"" + By.EscapeCssSelector(nameToFind) + "\"]";

by.findElementMethod = (ISearchContext context) => ((IFindsElement)context).FindElement(by.mechanism, by.criteria);
by.findElementsMethod = (ISearchContext context) => ((IFindsElement)context).FindElements(by.mechanism, by.criteria);
return by;
}

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

By by = new By();
By by = new By(XPathSelectorMechanism, xpathToFind);
by.description = "By.XPath: " + xpathToFind;
by.mechanism = "xpath";
by.criteria = xpathToFind;

by.findElementMethod = (ISearchContext context) => ((IFindsElement)context).FindElement(by.mechanism, by.criteria);
by.findElementsMethod = (ISearchContext context) => ((IFindsElement)context).FindElements(by.mechanism, by.criteria);
return by;
}

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

By by = new By();
by.description = "By.ClassName[Contains]: " + classNameToFind;
by.mechanism = "css selector";
by.criteria = "." + By.EscapeCssSelector(classNameToFind);
if (by.criteria.Contains(" "))
string selector = "." + By.EscapeCssSelector(classNameToFind);
if (selector.Contains(" "))
{
// Finding elements by class name with whitespace is not allowed.
// However, converting the single class name to a valid CSS selector
Expand All @@ -272,8 +270,8 @@ public static By ClassName(string classNameToFind)
throw new InvalidSelectorException("Compound class names not allowed. Cannot have whitespace in class name. Use CSS selectors instead.");
}

by.findElementMethod = (ISearchContext context) => ((IFindsElement)context).FindElement(by.mechanism, by.criteria);
by.findElementsMethod = (ISearchContext context) => ((IFindsElement)context).FindElements(by.mechanism, by.criteria);
By by = new By(CssSelectorMechanism, selector);
by.description = "By.ClassName[Contains]: " + classNameToFind;
return by;
}

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

By by = new By();
By by = new By(PartialLinkTextMechanism, partialLinkTextToFind);
by.description = "By.PartialLinkText: " + partialLinkTextToFind;
by.mechanism = "partial link text";
by.criteria = partialLinkTextToFind;

by.findElementMethod = (ISearchContext context) => ((IFindsElement)context).FindElement(by.mechanism, by.criteria);
by.findElementsMethod = (ISearchContext context) => ((IFindsElement)context).FindElements(by.mechanism, by.criteria);
return by;
}

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

By by = new By();
By by = new By(TagNameMechanism, tagNameToFind);
by.description = "By.TagName: " + tagNameToFind;
by.mechanism = "css selector";
by.criteria = tagNameToFind;

by.findElementMethod = (ISearchContext context) => ((IFindsElement)context).FindElement(by.mechanism, by.criteria);
by.findElementsMethod = (ISearchContext context) => ((IFindsElement)context).FindElements(by.mechanism, by.criteria);
return by;
}

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

By by = new By();
By by = new By(CssSelectorMechanism, cssSelectorToFind);
by.description = "By.CssSelector: " + cssSelectorToFind;
by.mechanism = "css selector";
by.criteria = cssSelectorToFind;

by.findElementMethod = (ISearchContext context) => ((IFindsElement)context).FindElement(by.mechanism, by.criteria);
by.findElementsMethod = (ISearchContext context) => ((IFindsElement)context).FindElements(by.mechanism, by.criteria);
return by;
}

Expand Down
46 changes: 0 additions & 46 deletions dotnet/test/common/ByTest.cs

This file was deleted.

0 comments on commit 4d44b47

Please sign in to comment.