Skip to content

Commit d0c863a

Browse files
menonvarunandreastt
authored andcommitted
Add new tests for some wait conditions
Also add the ExpectedConditionsTest to the test suite. Signed-off-by: Andreas Tolfsen <[email protected]>
1 parent d54ca5e commit d0c863a

File tree

2 files changed

+189
-3
lines changed

2 files changed

+189
-3
lines changed

Diff for: java/client/test/org/openqa/selenium/support/SmallTests.java

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.openqa.selenium.support.pagefactory.DefaultFieldDecoratorTest;
2424
import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandlerTest;
2525
import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandlerTest;
26+
import org.openqa.selenium.support.ui.ExpectedConditionsTest;
2627
import org.openqa.selenium.support.ui.FluentWaitTest;
2728
import org.openqa.selenium.support.ui.LoadableComponentTest;
2829
import org.openqa.selenium.support.ui.SelectTest;
@@ -41,6 +42,7 @@
4142
DefaultElementLocatorTest.class,
4243
DefaultFieldDecoratorTest.class,
4344
EventFiringWebDriverTest.class,
45+
ExpectedConditionsTest.class,
4446
FluentWaitTest.class,
4547
LoadableComponentTest.class,
4648
LocatingElementHandlerTest.class,

Diff for: java/client/test/org/openqa/selenium/support/ui/ExpectedConditionsTest.java

+187-3
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,39 @@
11
package org.openqa.selenium.support.ui;
22

3+
import static org.junit.Assert.assertEquals;
34
import static org.junit.Assert.assertSame;
45
import static org.junit.Assert.assertTrue;
56
import static org.junit.Assert.fail;
67
import static org.mockito.Mockito.times;
78
import static org.mockito.Mockito.verify;
89
import static org.mockito.Mockito.verifyZeroInteractions;
910
import static org.mockito.Mockito.when;
11+
import static org.openqa.selenium.support.ui.ExpectedConditions.elementSelectionStateToBe;
1012
import static org.openqa.selenium.support.ui.ExpectedConditions.not;
13+
import static org.openqa.selenium.support.ui.ExpectedConditions.textToBePresentInElementLocated;
1114
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;
1219

1320
import org.junit.Before;
1421
import org.junit.Test;
15-
import org.junit.runner.RunWith;
16-
import org.junit.runners.JUnit4;
1722
import org.mockito.Mock;
1823
import org.mockito.MockitoAnnotations;
24+
import org.openqa.selenium.By;
25+
import org.openqa.selenium.NoSuchElementException;
26+
import org.openqa.selenium.StaleElementReferenceException;
1927
import org.openqa.selenium.TimeoutException;
2028
import org.openqa.selenium.WebDriver;
2129
import org.openqa.selenium.WebElement;
2230

31+
import java.util.List;
2332
import java.util.concurrent.TimeUnit;
2433

2534
/**
2635
* Tests for {@link ExpectedConditions}.
2736
*/
28-
@RunWith(JUnit4.class)
2937
public class ExpectedConditionsTest {
3038

3139
@Mock private WebDriver mockDriver;
@@ -174,5 +182,181 @@ public void doubleNegatives_conditionThatReturnsNullTimesOut() throws Interrupte
174182
verify(mockSleeper, times(1)).sleep(new Duration(250, TimeUnit.MILLISECONDS));
175183
}
176184

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+
177361
interface GenericCondition extends ExpectedCondition<Object> {}
178362
}

0 commit comments

Comments
 (0)