Skip to content

Commit fa85eff

Browse files
committed
[dotnot] change default behavior for Select class with disabled elements
1 parent e2bbb54 commit fa85eff

3 files changed

Lines changed: 48 additions & 1 deletion

File tree

dotnet/src/support/UI/SelectElement.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public SelectElement(IWebElement element)
4444
throw new ArgumentNullException(nameof(element), "element cannot be null");
4545
}
4646

47+
if (!element.Enabled)
48+
{
49+
throw new InvalidOperationException("Select element is disabled and may not be used.");
50+
}
51+
4752
string tagName = element.TagName;
4853

4954
if (string.IsNullOrEmpty(tagName) || string.Compare(tagName, "select", StringComparison.OrdinalIgnoreCase) != 0)
@@ -446,6 +451,10 @@ private static string GetLongestSubstringWithoutSpace(string s)
446451

447452
private static void SetSelected(IWebElement option, bool select)
448453
{
454+
if (select && !option.Enabled) {
455+
throw new InvalidOperationException("You may not select a disabled option");
456+
}
457+
449458
bool isSelected = option.Selected;
450459
if ((!isSelected && select) || (isSelected && !select))
451460
{

dotnet/test/support/UI/SelectBrowserTests.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ public void ShouldThrowAnExceptionIfTheElementIsNotASelectElement()
3434
Assert.Throws<UnexpectedTagNameException>(() => { SelectElement elementWrapper = new SelectElement(element); });
3535
}
3636

37+
[Test]
38+
public void ShouldThrowAnExceptionIfTheElementIsNotEnabled()
39+
{
40+
IWebElement element = driver.FindElement(By.Name("no-select"));
41+
Assert.Throws<InvalidOperationException>(() => { SelectElement elementWrapper = new SelectElement(element); });
42+
}
43+
3744
[Test]
3845
public void ShouldIndicateThatASelectCanSupportMultipleOptions()
3946
{
@@ -141,7 +148,7 @@ public void ShouldReturnFirstSelectedOption()
141148

142149
// [Test]
143150
// [ExpectedException(typeof(NoSuchElementException))]
144-
// The .NET bindings do not have a "FirstSelectedOption" property,
151+
// The .NET bindings do not have a "FirstSelectedOption" property,
145152
// and no one has asked for it to this point. Given that, this test
146153
// is not a valid test.
147154
public void ShouldThrowANoSuchElementExceptionIfNothingIsSelected()
@@ -196,6 +203,14 @@ public void ShouldThrowExceptionOnSelectByVisibleTextIfOptionDoesNotExist()
196203
Assert.Throws<NoSuchElementException>(() => elementWrapper.SelectByText("not there"));
197204
}
198205

206+
[Test]
207+
public void ShouldThrowExceptionOnSelectByVisibleTextIfOptionDisabled()
208+
{
209+
IWebElement element = driver.FindElement(By.Name("single_disabled"));
210+
SelectElement elementWrapper = new SelectElement(element);
211+
Assert.Throws<NoSuchElementException>(() => elementWrapper.SelectByText("Disabled"));
212+
}
213+
199214
[Test]
200215
public void ShouldAllowOptionsToBeSelectedByIndex()
201216
{
@@ -214,6 +229,14 @@ public void ShouldThrowExceptionOnSelectByIndexIfOptionDoesNotExist()
214229
Assert.Throws<NoSuchElementException>(() => elementWrapper.SelectByIndex(10));
215230
}
216231

232+
[Test]
233+
public void ShouldThrowExceptionOnSelectByIndexIfOptionDisabled()
234+
{
235+
IWebElement element = driver.FindElement(By.Name("single_disabled"));
236+
SelectElement elementWrapper = new SelectElement(element);
237+
Assert.Throws<NoSuchElementException>(() => elementWrapper.SelectByIndex(1));
238+
}
239+
217240
[Test]
218241
public void ShouldAllowOptionsToBeSelectedByReturnedValue()
219242
{
@@ -232,6 +255,14 @@ public void ShouldThrowExceptionOnSelectByReturnedValueIfOptionDoesNotExist()
232255
Assert.Throws<NoSuchElementException>(() => elementWrapper.SelectByValue("not there"));
233256
}
234257

258+
[Test]
259+
public void ShouldThrowExceptionOnSelectByReturnedValueIfOptionDisabled()
260+
{
261+
IWebElement element = driver.FindElement(By.Name("single_disabled"));
262+
SelectElement elementWrapper = new SelectElement(element);
263+
Assert.Throws<NoSuchElementException>(() => elementWrapper.SelectByValue("disabled"));
264+
}
265+
235266
[Test]
236267
public void ShouldAllowUserToDeselectAllWhenSelectSupportsMultipleSelections()
237268
{

dotnet/test/support/UI/SelectTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ public void ThrowUnexpectedTagNameExceptionWhenNotSelectTag()
2525
Assert.Throws<UnexpectedTagNameException>(() => new SelectElement(webElement.Object));
2626
}
2727

28+
[Test]
29+
public void ThrowUnexpectedTagNameExceptionWhenNotEnabled()
30+
{
31+
webElement.SetupGet<bool>(_ => _.Enabled).Returns(false);
32+
Assert.Throws<InvalidOperationException>(() => new SelectElement(webElement.Object));
33+
}
34+
2835
[Test]
2936
public void CanCreateNewInstanceOfSelectWithNormalSelectElement()
3037
{

0 commit comments

Comments
 (0)