2020import static java .util .Collections .emptyList ;
2121import static org .assertj .core .api .Assertions .assertThat ;
2222import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
23+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
2324import static org .mockito .Mockito .mock ;
2425import static org .mockito .Mockito .never ;
2526import static org .mockito .Mockito .verify ;
@@ -74,6 +75,7 @@ private WebElement mockSelectWebElement(String multiple) {
7475 final WebElement element = mock (WebElement .class );
7576 when (element .getTagName ()).thenReturn ("select" );
7677 when (element .getDomAttribute ("multiple" )).thenReturn (multiple );
78+ when (element .isEnabled ()).thenReturn (true );
7779 return element ;
7880 }
7981
@@ -93,12 +95,14 @@ public void shouldReturnAllOptionsWhenAsked() {
9395
9496 private WebElement mockOption (String name , boolean isSelected ) {
9597 final WebElement optionBad = mock (WebElement .class , name );
98+ when (optionBad .isEnabled ()).thenReturn (true );
9699 when (optionBad .isSelected ()).thenReturn (isSelected );
97100 return optionBad ;
98101 }
99102
100103 private WebElement mockOption (String name , boolean isSelected , int index ) {
101104 WebElement option = mockOption (name , isSelected );
105+ when (option .isEnabled ()).thenReturn (true );
102106 when (option .getAttribute ("index" )).thenReturn (String .valueOf (index ));
103107 return option ;
104108 }
@@ -151,6 +155,40 @@ public void shouldAllowOptionsToBeSelectedByVisibleText() {
151155 verify (firstOption ).click ();
152156 }
153157
158+ @ Test
159+ public void shouldNotAllowDisabledOptionsToBeSelected () {
160+ final WebElement firstOption = mockOption ("first" , false );
161+ when (firstOption .isEnabled ()).thenReturn (false );
162+
163+ final WebElement element = mockSelectWebElement ("multiple" );
164+ when (element .findElements (By .xpath (".//option[normalize-space(.) = \" fish\" ]" )))
165+ .thenReturn (Collections .singletonList (firstOption ));
166+
167+ Select select = new Select (element );
168+ assertThatThrownBy (() -> select .selectByVisibleText ("fish" ))
169+ .isInstanceOf (UnsupportedOperationException .class )
170+ .hasMessage ("You may not select a disabled option" );
171+
172+ verify (firstOption , never ()).click ();
173+ }
174+
175+ @ Test
176+ public void shouldNotAllowOptionsToBeSelectedInDisabledSelect () {
177+ final WebElement firstOption = mockOption ("first" , false );
178+
179+ final WebElement element = mockSelectWebElement ("multiple" );
180+ when (element .isEnabled ()).thenReturn (false );
181+ when (element .findElements (By .xpath (".//option[normalize-space(.) = \" fish\" ]" )))
182+ .thenReturn (Collections .singletonList (firstOption ));
183+
184+ Select select = new Select (element );
185+ assertThatThrownBy (() -> select .selectByVisibleText ("fish" ))
186+ .isInstanceOf (UnsupportedOperationException .class )
187+ .hasMessage ("You may not select an option in disabled select" );
188+
189+ verify (firstOption , never ()).click ();
190+ }
191+
154192 @ Test
155193 public void shouldAllowOptionsToBeSelectedByIndex () {
156194 final WebElement firstOption = mockOption ("first" , true , 0 );
@@ -252,6 +290,7 @@ public void shouldFallBackToSlowLooksUpsWhenGetByVisibleTextFailsAndThereIsASpac
252290 when (element .findElements (xpath1 )).thenReturn (emptyList ());
253291 when (element .findElements (xpath2 )).thenReturn (Collections .singletonList (firstOption ));
254292 when (firstOption .getText ()).thenReturn ("foo bar" );
293+ when (firstOption .isEnabled ()).thenReturn (true );
255294
256295 Select select = new Select (element );
257296 select .selectByVisibleText ("foo bar" );
0 commit comments