|
1 | 1 | package org.openqa.selenium.support.ui;
|
2 | 2 |
|
| 3 | +import static org.junit.Assert.assertEquals; |
3 | 4 | import static org.junit.Assert.assertSame;
|
4 | 5 | import static org.junit.Assert.assertTrue;
|
5 | 6 | import static org.junit.Assert.fail;
|
6 | 7 | import static org.mockito.Mockito.times;
|
7 | 8 | import static org.mockito.Mockito.verify;
|
8 | 9 | import static org.mockito.Mockito.verifyZeroInteractions;
|
9 | 10 | import static org.mockito.Mockito.when;
|
| 11 | +import static org.openqa.selenium.support.ui.ExpectedConditions.elementSelectionStateToBe; |
10 | 12 | import static org.openqa.selenium.support.ui.ExpectedConditions.not;
|
| 13 | +import static org.openqa.selenium.support.ui.ExpectedConditions.textToBePresentInElementLocated; |
11 | 14 | import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOf;
|
| 15 | +import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfAllElements; |
| 16 | +import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfAllElementsLocatedBy; |
| 17 | + |
| 18 | +import com.google.common.collect.Lists; |
12 | 19 |
|
13 | 20 | import org.junit.Before;
|
14 | 21 | import org.junit.Test;
|
15 |
| -import org.junit.runner.RunWith; |
16 |
| -import org.junit.runners.JUnit4; |
17 | 22 | import org.mockito.Mock;
|
18 | 23 | import org.mockito.MockitoAnnotations;
|
| 24 | +import org.openqa.selenium.By; |
| 25 | +import org.openqa.selenium.NoSuchElementException; |
| 26 | +import org.openqa.selenium.StaleElementReferenceException; |
19 | 27 | import org.openqa.selenium.TimeoutException;
|
20 | 28 | import org.openqa.selenium.WebDriver;
|
21 | 29 | import org.openqa.selenium.WebElement;
|
22 | 30 |
|
| 31 | +import java.util.List; |
23 | 32 | import java.util.concurrent.TimeUnit;
|
24 | 33 |
|
25 | 34 | /**
|
26 | 35 | * Tests for {@link ExpectedConditions}.
|
27 | 36 | */
|
28 |
| -@RunWith(JUnit4.class) |
29 | 37 | public class ExpectedConditionsTest {
|
30 | 38 |
|
31 | 39 | @Mock private WebDriver mockDriver;
|
@@ -174,5 +182,181 @@ public void doubleNegatives_conditionThatReturnsNullTimesOut() throws Interrupte
|
174 | 182 | verify(mockSleeper, times(1)).sleep(new Duration(250, TimeUnit.MILLISECONDS));
|
175 | 183 | }
|
176 | 184 |
|
| 185 | + @Test |
| 186 | + public void waitingForVisibilityOfAllElementsLocatedByReturnsListOfElements() { |
| 187 | + List webElements = Lists.newArrayList(mockElement); |
| 188 | + String testSelector = "testSelector"; |
| 189 | + |
| 190 | + when(mockDriver.findElements(By.cssSelector(testSelector))).thenReturn(webElements); |
| 191 | + when(mockElement.isDisplayed()).thenReturn(true); |
| 192 | + |
| 193 | + List returnedElements = |
| 194 | + wait.until(visibilityOfAllElementsLocatedBy(By.cssSelector(testSelector))); |
| 195 | + assertEquals(webElements, returnedElements); |
| 196 | + } |
| 197 | + |
| 198 | + @Test(expected = TimeoutException.class) |
| 199 | + public void waitingForVisibilityOfAllElementsLocatedByThrowsTimeoutExceptionWhenElementNotDisplayed() { |
| 200 | + List webElements = Lists.newArrayList(mockElement); |
| 201 | + String testSelector = "testSelector"; |
| 202 | + |
| 203 | + when(mockDriver.findElements(By.cssSelector(testSelector))).thenReturn(webElements); |
| 204 | + when(mockElement.isDisplayed()).thenReturn(false); |
| 205 | + |
| 206 | + wait.until(visibilityOfAllElementsLocatedBy(By.cssSelector(testSelector))); |
| 207 | + } |
| 208 | + |
| 209 | + @Test(expected = StaleElementReferenceException.class) |
| 210 | + public void waitingForVisibilityOfAllElementsLocatedByThrowsStaleExceptionWhenElementIsStale() { |
| 211 | + List webElements = Lists.newArrayList(mockElement); |
| 212 | + String testSelector = "testSelector"; |
| 213 | + |
| 214 | + when(mockDriver.findElements(By.cssSelector(testSelector))).thenReturn(webElements); |
| 215 | + when(mockElement.isDisplayed()).thenThrow(new StaleElementReferenceException("Stale element")); |
| 216 | + |
| 217 | + wait.until(visibilityOfAllElementsLocatedBy(By.cssSelector(testSelector))); |
| 218 | + } |
| 219 | + |
| 220 | + @Test(expected = TimeoutException.class) |
| 221 | + public void waitingForVisibilityOfAllElementsLocatedByThrowsTimeoutExceptionWhenNoElementsFound() { |
| 222 | + List webElements = Lists.newArrayList(); |
| 223 | + String testSelector = "testSelector"; |
| 224 | + |
| 225 | + when(mockDriver.findElements(By.cssSelector(testSelector))).thenReturn(webElements); |
| 226 | + |
| 227 | + wait.until(visibilityOfAllElementsLocatedBy(By.cssSelector(testSelector))); |
| 228 | + } |
| 229 | + |
| 230 | + @Test |
| 231 | + public void waitingForVisibilityOfAllElementsReturnsListOfElements() { |
| 232 | + List webElements = Lists.newArrayList(mockElement); |
| 233 | + when(mockElement.isDisplayed()).thenReturn(true); |
| 234 | + |
| 235 | + List<WebElement> returnedElements = wait.until(visibilityOfAllElements(webElements)); |
| 236 | + assertEquals(webElements, returnedElements); |
| 237 | + } |
| 238 | + |
| 239 | + @Test(expected = TimeoutException.class) |
| 240 | + public void waitingForVisibilityOfAllElementsThrowsTimeoutExceptionWhenElementNotDisplayed() { |
| 241 | + List webElements = Lists.newArrayList(mockElement); |
| 242 | + when(mockElement.isDisplayed()).thenReturn(false); |
| 243 | + |
| 244 | + wait.until(visibilityOfAllElements(webElements)); |
| 245 | + } |
| 246 | + |
| 247 | + @Test(expected = StaleElementReferenceException.class) |
| 248 | + public void waitingForVisibilityOfAllElementsThrowsStaleElementReferenceExceptionWhenElementIsStale() { |
| 249 | + List webElements = Lists.newArrayList(mockElement); |
| 250 | + |
| 251 | + when(mockElement.isDisplayed()).thenThrow(new StaleElementReferenceException("Stale element")); |
| 252 | + |
| 253 | + wait.until(visibilityOfAllElements(webElements)); |
| 254 | + } |
| 255 | + |
| 256 | + @Test(expected = TimeoutException.class) |
| 257 | + public void waitingForVisibilityOfAllElementsThrowsTimeoutExceptionWhenNoElementsFound() { |
| 258 | + List webElements = Lists.newArrayList(); |
| 259 | + |
| 260 | + wait.until(visibilityOfAllElements(webElements)); |
| 261 | + } |
| 262 | + |
| 263 | + @Test |
| 264 | + public void waitingForVisibilityOfReturnsElement() { |
| 265 | + when(mockElement.isDisplayed()).thenReturn(true); |
| 266 | + |
| 267 | + WebElement returnedElement = wait.until(visibilityOf(mockElement)); |
| 268 | + assertEquals(mockElement, returnedElement); |
| 269 | + } |
| 270 | + |
| 271 | + @Test(expected = TimeoutException.class) |
| 272 | + public void waitingForVisibilityOfThrowsTimeoutExceptionWhenElementNotDisplayed() { |
| 273 | + |
| 274 | + when(mockElement.isDisplayed()).thenReturn(false); |
| 275 | + |
| 276 | + wait.until(visibilityOf(mockElement)); |
| 277 | + } |
| 278 | + |
| 279 | + @Test(expected = StaleElementReferenceException.class) |
| 280 | + public void waitingForVisibilityOfThrowsStaleElementReferenceExceptionWhenElementIsStale() { |
| 281 | + |
| 282 | + when(mockElement.isDisplayed()).thenThrow(new StaleElementReferenceException("Stale element")); |
| 283 | + |
| 284 | + wait.until(visibilityOf(mockElement)); |
| 285 | + } |
| 286 | + |
| 287 | + @Test |
| 288 | + public void waitingForTextToBePresentInElementLocatedReturnsElement() { |
| 289 | + String testSelector = "testSelector"; |
| 290 | + when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement); |
| 291 | + when(mockElement.getText()).thenReturn("testText"); |
| 292 | + |
| 293 | + assertTrue( |
| 294 | + wait.until(textToBePresentInElementLocated(By.cssSelector(testSelector), "testText"))); |
| 295 | + } |
| 296 | + |
| 297 | + @Test |
| 298 | + public void waitingForTextToBePresentInElementLocatedReturnsElementWhenTextContainsSaidText() { |
| 299 | + String testSelector = "testSelector"; |
| 300 | + when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement); |
| 301 | + when(mockElement.getText()).thenReturn("testText"); |
| 302 | + |
| 303 | + assertTrue(wait.until(textToBePresentInElementLocated(By.cssSelector(testSelector), "test"))); |
| 304 | + } |
| 305 | + |
| 306 | + @Test(expected = TimeoutException.class) |
| 307 | + public void waitingForTextToBePresentInElementLocatedThrowsTimeoutExceptionWhenTextNotPresent() { |
| 308 | + String testSelector = "testSelector"; |
| 309 | + when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement); |
| 310 | + when(mockElement.getText()).thenReturn("testText"); |
| 311 | + |
| 312 | + wait.until(textToBePresentInElementLocated(By.cssSelector(testSelector), "failText")); |
| 313 | + } |
| 314 | + |
| 315 | + @Test(expected = TimeoutException.class) |
| 316 | + public void waitingForTextToBePresentInElementLocatedThrowsTimeoutExceptionWhenElementIsStale() { |
| 317 | + String testSelector = "testSelector"; |
| 318 | + when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement); |
| 319 | + when(mockElement.getText()).thenThrow(new StaleElementReferenceException("Stale element")); |
| 320 | + |
| 321 | + wait.until(textToBePresentInElementLocated(By.cssSelector(testSelector), "testText")); |
| 322 | + } |
| 323 | + |
| 324 | + @Test(expected = NoSuchElementException.class) |
| 325 | + public void waitingTextToBePresentInElementLocatedThrowsTimeoutExceptionWhenNoElementFound() { |
| 326 | + String testSelector = "testSelector"; |
| 327 | + when(mockDriver.findElement(By.cssSelector(testSelector))).thenThrow( |
| 328 | + new NoSuchElementException("Element not found")); |
| 329 | + |
| 330 | + wait.until(textToBePresentInElementLocated(By.cssSelector(testSelector), "testText")); |
| 331 | + } |
| 332 | + |
| 333 | + @Test |
| 334 | + public void waitingElementSelectionStateToBeTrueReturnsTrue() { |
| 335 | + when(mockElement.isSelected()).thenReturn(true); |
| 336 | + |
| 337 | + assertTrue(wait.until(elementSelectionStateToBe(mockElement, true))); |
| 338 | + } |
| 339 | + |
| 340 | + @Test |
| 341 | + public void waitingElementSelectionStateToBeFalseReturnsTrue() { |
| 342 | + when(mockElement.isSelected()).thenReturn(false); |
| 343 | + |
| 344 | + assertTrue(wait.until(elementSelectionStateToBe(mockElement, false))); |
| 345 | + } |
| 346 | + |
| 347 | + @Test(expected = TimeoutException.class) |
| 348 | + public void waitingElementSelectionStateToBeThrowsTimeoutExceptionWhenStateDontMatch() { |
| 349 | + when(mockElement.isSelected()).thenReturn(true); |
| 350 | + |
| 351 | + assertTrue(wait.until(elementSelectionStateToBe(mockElement, false))); |
| 352 | + } |
| 353 | + |
| 354 | + @Test(expected = StaleElementReferenceException.class) |
| 355 | + public void waitingElementSelectionStateToBeThrowsStaleExceptionWhenElementIsStale() { |
| 356 | + when(mockElement.isSelected()).thenThrow(new StaleElementReferenceException("Stale element")); |
| 357 | + |
| 358 | + assertTrue(wait.until(elementSelectionStateToBe(mockElement, false))); |
| 359 | + } |
| 360 | + |
177 | 361 | interface GenericCondition extends ExpectedCondition<Object> {}
|
178 | 362 | }
|
0 commit comments