Skip to content

Commit

Permalink
Addition of overload argument to the SelectElement.SelectByText method
Browse files Browse the repository at this point in the history
Issue raised in the issue log label as dotnet, issue number 3575. The
XML summary informs that the method provides partial match ability on
the options  by Text.  However this wasn't fully implemented so pushed
the ability via a boolean to perform such a partial match on an options
list. Fixes issue #3575.

Signed-off-by: Jim Evans <[email protected]>
  • Loading branch information
seanrand57 authored and jimevans committed Mar 21, 2018
1 parent 0d9bcec commit d0accdf
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
21 changes: 15 additions & 6 deletions dotnet/src/support/UI/SelectElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,25 +124,34 @@ public IList<IWebElement> AllSelectedOptions
/// <summary>
/// Select all options by the text displayed.
/// </summary>
/// <param name="text">The text of the option to be selected. If an exact match is not found,
/// this method will perform a substring match.</param>
/// <param name="text">The text of the option to be selected.</param>
/// <param name="partialMatch">Default value is false. If true a partial match on the Options list will be performed, otherwise exact match.</param>
/// <remarks>When given "Bar" this method would select an option like:
/// <para>
/// &lt;option value="foo"&gt;Bar&lt;/option&gt;
/// </para>
/// </remarks>
/// <exception cref="NoSuchElementException">Thrown if there is no element with the given text present.</exception>
public void SelectByText(string text)
public void SelectByText(string text, bool partialMatch = false)
{
if (text == null)
{
throw new ArgumentNullException("text", "text must not be null");
}

// try to find the option via XPATH ...
IList<IWebElement> options = this.element.FindElements(By.XPath(".//option[normalize-space(.) = " + EscapeQuotes(text) + "]"));

bool matched = false;
IList<IWebElement> options;

if (!partialMatch)
{
// try to find the option via XPATH ...
options = this.element.FindElements(By.XPath(".//option[normalize-space(.) = " + EscapeQuotes(text) + "]"));
}
else
{
options = this.element.FindElements(By.XPath(".//option[contains(normalize-space(.), " + EscapeQuotes(text) + ")]"));
}

foreach (IWebElement option in options)
{
SetSelected(option, true);
Expand Down
18 changes: 18 additions & 0 deletions dotnet/test/support/UI/SelectBrowserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,24 @@ public void ShouldAllowOptionsToBeSelectedByVisibleText()
Assert.AreEqual("select_2", firstSelected.Text);
}

[Test]
public void ShouldAllowOptionsToBeSelectedByPartialText()
{
IWebElement element = driver.FindElement(By.Name("select_empty_multiple"));
SelectElement elementWrapper = new SelectElement(element);
elementWrapper.SelectByText("4", true);
IWebElement firstSelected = elementWrapper.AllSelectedOptions[0];
Assert.AreEqual("select_4", firstSelected.Text);
}

[Test]
public void ShouldThrowExceptionOnSelectByTextExactMatchIfOptionDoesNotExist()
{
IWebElement element = driver.FindElement(By.Name("select_empty_multiple"));
SelectElement elementWrapper = new SelectElement(element);
Assert.Throws<NoSuchElementException>(() => elementWrapper.SelectByText("4"));
}

[Test]
public void ShouldNotAllowInvisibleOptionsToBeSelectedByVisibleText()
{
Expand Down

0 comments on commit d0accdf

Please sign in to comment.