Skip to content

Commit

Permalink
Updating .NET bindings to enable legacy behavior for spec-compliant d…
Browse files Browse the repository at this point in the history
…rivers
  • Loading branch information
jimevans committed Jan 27, 2016
1 parent f2c0cf5 commit 4532dc6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
36 changes: 33 additions & 3 deletions dotnet/src/webdriver/Remote/RemoteWebDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,17 @@ public ReadOnlyCollection<IWebElement> FindElementsById(string id)
{
if (this.IsSpecificationCompliant)
{
return this.FindElements("css selector", "#" + EscapeCssSelector(id));
string selector = EscapeCssSelector(id);
if (selector == string.Empty)
{
// Finding multiple elements with an empty ID will return
// an empty list. However, finding by a CSS selector of '#'
// throws an exception, even in the multiple elements case,
// which means we need to short-circuit that behavior.
return new List<IWebElement>().AsReadOnly();
}

return this.FindElements("css selector", "#" + selector);
}

return this.FindElements("id", id);
Expand All @@ -594,7 +604,17 @@ public IWebElement FindElementByClassName(string className)
// return this.FindElement("css selector", "." + className);
if (this.IsSpecificationCompliant)
{
return this.FindElement("css selector", "." + EscapeCssSelector(className));
string selector = EscapeCssSelector(className);
if (selector.Contains(" "))
{
// Finding elements by class name with whitespace is not allowed.
// However, converting the single class name to a valid CSS selector
// by prepending a '.' may result in a still-valid, but incorrect
// selector. Thus, we short-ciruit that behavior here.
throw new InvalidSelectorException("Compound class names not allowed. Cannot have whitespace in class name. Use CSS selectors instead.");
}

return this.FindElement("css selector", "." + selector);
}

return this.FindElement("class name", className);
Expand All @@ -621,7 +641,17 @@ public ReadOnlyCollection<IWebElement> FindElementsByClassName(string className)
// return this.FindElements("css selector", "." + className);
if (this.IsSpecificationCompliant)
{
return this.FindElements("css selector", "." + EscapeCssSelector(className));
string selector = EscapeCssSelector(className);
if (selector.Contains(" "))
{
// Finding elements by class name with whitespace is not allowed.
// However, converting the single class name to a valid CSS selector
// by prepending a '.' may result in a still-valid, but incorrect
// selector. Thus, we short-ciruit that behavior here.
throw new InvalidSelectorException("Compound class names not allowed. Cannot have whitespace in class name. Use CSS selectors instead.");
}

return this.FindElements("css selector", "." + selector);
}

return this.FindElements("class name", className);
Expand Down
2 changes: 1 addition & 1 deletion dotnet/test/common/ElementFindingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void ShouldNotBeAbleToLocateByIdMultipleElementsThatDoNotExist()
public void FindingASingleElementByEmptyIdShouldThrow()
{
driver.Url = formsPage;
Assert.Throws<NoSuchElementException>(() => driver.FindElement(By.Id("")));
Assert.Throws(Is.InstanceOf<NoSuchElementException>(), () => driver.FindElement(By.Id("")));
}

[Test]
Expand Down

0 comments on commit 4532dc6

Please sign in to comment.