Skip to content

Commit

Permalink
Add new tests for some wait conditions
Browse files Browse the repository at this point in the history
Also add the ExpectedConditionsTest to the test suite.

Signed-off-by: Andreas Tolfsen <[email protected]>
  • Loading branch information
menonvarun authored and andreastt committed Oct 18, 2014
1 parent d54ca5e commit d0c863a
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 3 deletions.
2 changes: 2 additions & 0 deletions java/client/test/org/openqa/selenium/support/SmallTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.openqa.selenium.support.pagefactory.DefaultFieldDecoratorTest;
import org.openqa.selenium.support.pagefactory.internal.LocatingElementHandlerTest;
import org.openqa.selenium.support.pagefactory.internal.LocatingElementListHandlerTest;
import org.openqa.selenium.support.ui.ExpectedConditionsTest;
import org.openqa.selenium.support.ui.FluentWaitTest;
import org.openqa.selenium.support.ui.LoadableComponentTest;
import org.openqa.selenium.support.ui.SelectTest;
Expand All @@ -41,6 +42,7 @@
DefaultElementLocatorTest.class,
DefaultFieldDecoratorTest.class,
EventFiringWebDriverTest.class,
ExpectedConditionsTest.class,
FluentWaitTest.class,
LoadableComponentTest.class,
LocatingElementHandlerTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
package org.openqa.selenium.support.ui;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.openqa.selenium.support.ui.ExpectedConditions.elementSelectionStateToBe;
import static org.openqa.selenium.support.ui.ExpectedConditions.not;
import static org.openqa.selenium.support.ui.ExpectedConditions.textToBePresentInElementLocated;
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOf;
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfAllElements;
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfAllElementsLocatedBy;

import com.google.common.collect.Lists;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* Tests for {@link ExpectedConditions}.
*/
@RunWith(JUnit4.class)
public class ExpectedConditionsTest {

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

@Test
public void waitingForVisibilityOfAllElementsLocatedByReturnsListOfElements() {
List webElements = Lists.newArrayList(mockElement);
String testSelector = "testSelector";

when(mockDriver.findElements(By.cssSelector(testSelector))).thenReturn(webElements);
when(mockElement.isDisplayed()).thenReturn(true);

List returnedElements =
wait.until(visibilityOfAllElementsLocatedBy(By.cssSelector(testSelector)));
assertEquals(webElements, returnedElements);
}

@Test(expected = TimeoutException.class)
public void waitingForVisibilityOfAllElementsLocatedByThrowsTimeoutExceptionWhenElementNotDisplayed() {
List webElements = Lists.newArrayList(mockElement);
String testSelector = "testSelector";

when(mockDriver.findElements(By.cssSelector(testSelector))).thenReturn(webElements);
when(mockElement.isDisplayed()).thenReturn(false);

wait.until(visibilityOfAllElementsLocatedBy(By.cssSelector(testSelector)));
}

@Test(expected = StaleElementReferenceException.class)
public void waitingForVisibilityOfAllElementsLocatedByThrowsStaleExceptionWhenElementIsStale() {
List webElements = Lists.newArrayList(mockElement);
String testSelector = "testSelector";

when(mockDriver.findElements(By.cssSelector(testSelector))).thenReturn(webElements);
when(mockElement.isDisplayed()).thenThrow(new StaleElementReferenceException("Stale element"));

wait.until(visibilityOfAllElementsLocatedBy(By.cssSelector(testSelector)));
}

@Test(expected = TimeoutException.class)
public void waitingForVisibilityOfAllElementsLocatedByThrowsTimeoutExceptionWhenNoElementsFound() {
List webElements = Lists.newArrayList();
String testSelector = "testSelector";

when(mockDriver.findElements(By.cssSelector(testSelector))).thenReturn(webElements);

wait.until(visibilityOfAllElementsLocatedBy(By.cssSelector(testSelector)));
}

@Test
public void waitingForVisibilityOfAllElementsReturnsListOfElements() {
List webElements = Lists.newArrayList(mockElement);
when(mockElement.isDisplayed()).thenReturn(true);

List<WebElement> returnedElements = wait.until(visibilityOfAllElements(webElements));
assertEquals(webElements, returnedElements);
}

@Test(expected = TimeoutException.class)
public void waitingForVisibilityOfAllElementsThrowsTimeoutExceptionWhenElementNotDisplayed() {
List webElements = Lists.newArrayList(mockElement);
when(mockElement.isDisplayed()).thenReturn(false);

wait.until(visibilityOfAllElements(webElements));
}

@Test(expected = StaleElementReferenceException.class)
public void waitingForVisibilityOfAllElementsThrowsStaleElementReferenceExceptionWhenElementIsStale() {
List webElements = Lists.newArrayList(mockElement);

when(mockElement.isDisplayed()).thenThrow(new StaleElementReferenceException("Stale element"));

wait.until(visibilityOfAllElements(webElements));
}

@Test(expected = TimeoutException.class)
public void waitingForVisibilityOfAllElementsThrowsTimeoutExceptionWhenNoElementsFound() {
List webElements = Lists.newArrayList();

wait.until(visibilityOfAllElements(webElements));
}

@Test
public void waitingForVisibilityOfReturnsElement() {
when(mockElement.isDisplayed()).thenReturn(true);

WebElement returnedElement = wait.until(visibilityOf(mockElement));
assertEquals(mockElement, returnedElement);
}

@Test(expected = TimeoutException.class)
public void waitingForVisibilityOfThrowsTimeoutExceptionWhenElementNotDisplayed() {

when(mockElement.isDisplayed()).thenReturn(false);

wait.until(visibilityOf(mockElement));
}

@Test(expected = StaleElementReferenceException.class)
public void waitingForVisibilityOfThrowsStaleElementReferenceExceptionWhenElementIsStale() {

when(mockElement.isDisplayed()).thenThrow(new StaleElementReferenceException("Stale element"));

wait.until(visibilityOf(mockElement));
}

@Test
public void waitingForTextToBePresentInElementLocatedReturnsElement() {
String testSelector = "testSelector";
when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement);
when(mockElement.getText()).thenReturn("testText");

assertTrue(
wait.until(textToBePresentInElementLocated(By.cssSelector(testSelector), "testText")));
}

@Test
public void waitingForTextToBePresentInElementLocatedReturnsElementWhenTextContainsSaidText() {
String testSelector = "testSelector";
when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement);
when(mockElement.getText()).thenReturn("testText");

assertTrue(wait.until(textToBePresentInElementLocated(By.cssSelector(testSelector), "test")));
}

@Test(expected = TimeoutException.class)
public void waitingForTextToBePresentInElementLocatedThrowsTimeoutExceptionWhenTextNotPresent() {
String testSelector = "testSelector";
when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement);
when(mockElement.getText()).thenReturn("testText");

wait.until(textToBePresentInElementLocated(By.cssSelector(testSelector), "failText"));
}

@Test(expected = TimeoutException.class)
public void waitingForTextToBePresentInElementLocatedThrowsTimeoutExceptionWhenElementIsStale() {
String testSelector = "testSelector";
when(mockDriver.findElement(By.cssSelector(testSelector))).thenReturn(mockElement);
when(mockElement.getText()).thenThrow(new StaleElementReferenceException("Stale element"));

wait.until(textToBePresentInElementLocated(By.cssSelector(testSelector), "testText"));
}

@Test(expected = NoSuchElementException.class)
public void waitingTextToBePresentInElementLocatedThrowsTimeoutExceptionWhenNoElementFound() {
String testSelector = "testSelector";
when(mockDriver.findElement(By.cssSelector(testSelector))).thenThrow(
new NoSuchElementException("Element not found"));

wait.until(textToBePresentInElementLocated(By.cssSelector(testSelector), "testText"));
}

@Test
public void waitingElementSelectionStateToBeTrueReturnsTrue() {
when(mockElement.isSelected()).thenReturn(true);

assertTrue(wait.until(elementSelectionStateToBe(mockElement, true)));
}

@Test
public void waitingElementSelectionStateToBeFalseReturnsTrue() {
when(mockElement.isSelected()).thenReturn(false);

assertTrue(wait.until(elementSelectionStateToBe(mockElement, false)));
}

@Test(expected = TimeoutException.class)
public void waitingElementSelectionStateToBeThrowsTimeoutExceptionWhenStateDontMatch() {
when(mockElement.isSelected()).thenReturn(true);

assertTrue(wait.until(elementSelectionStateToBe(mockElement, false)));
}

@Test(expected = StaleElementReferenceException.class)
public void waitingElementSelectionStateToBeThrowsStaleExceptionWhenElementIsStale() {
when(mockElement.isSelected()).thenThrow(new StaleElementReferenceException("Stale element"));

assertTrue(wait.until(elementSelectionStateToBe(mockElement, false)));
}

interface GenericCondition extends ExpectedCondition<Object> {}
}

0 comments on commit d0c863a

Please sign in to comment.