20
20
import static java .util .Collections .emptyList ;
21
21
import static org .assertj .core .api .Assertions .assertThat ;
22
22
import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
23
+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
23
24
import static org .mockito .Mockito .mock ;
24
25
import static org .mockito .Mockito .never ;
25
26
import static org .mockito .Mockito .verify ;
@@ -74,6 +75,7 @@ private WebElement mockSelectWebElement(String multiple) {
74
75
final WebElement element = mock (WebElement .class );
75
76
when (element .getTagName ()).thenReturn ("select" );
76
77
when (element .getDomAttribute ("multiple" )).thenReturn (multiple );
78
+ when (element .isEnabled ()).thenReturn (true );
77
79
return element ;
78
80
}
79
81
@@ -93,12 +95,14 @@ public void shouldReturnAllOptionsWhenAsked() {
93
95
94
96
private WebElement mockOption (String name , boolean isSelected ) {
95
97
final WebElement optionBad = mock (WebElement .class , name );
98
+ when (optionBad .isEnabled ()).thenReturn (true );
96
99
when (optionBad .isSelected ()).thenReturn (isSelected );
97
100
return optionBad ;
98
101
}
99
102
100
103
private WebElement mockOption (String name , boolean isSelected , int index ) {
101
104
WebElement option = mockOption (name , isSelected );
105
+ when (option .isEnabled ()).thenReturn (true );
102
106
when (option .getAttribute ("index" )).thenReturn (String .valueOf (index ));
103
107
return option ;
104
108
}
@@ -151,6 +155,40 @@ public void shouldAllowOptionsToBeSelectedByVisibleText() {
151
155
verify (firstOption ).click ();
152
156
}
153
157
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
+
154
192
@ Test
155
193
public void shouldAllowOptionsToBeSelectedByIndex () {
156
194
final WebElement firstOption = mockOption ("first" , true , 0 );
@@ -252,6 +290,7 @@ public void shouldFallBackToSlowLooksUpsWhenGetByVisibleTextFailsAndThereIsASpac
252
290
when (element .findElements (xpath1 )).thenReturn (emptyList ());
253
291
when (element .findElements (xpath2 )).thenReturn (Collections .singletonList (firstOption ));
254
292
when (firstOption .getText ()).thenReturn ("foo bar" );
293
+ when (firstOption .isEnabled ()).thenReturn (true );
255
294
256
295
Select select = new Select (element );
257
296
select .selectByVisibleText ("foo bar" );
0 commit comments