Permalink
Browse files

add new wait features : areEnabled and isNotPresent

  • Loading branch information...
1 parent d5bd796 commit 1e17349ba4280c29dd78e2732004f845b8877d4a @MathildeLemee MathildeLemee committed Aug 31, 2012
View
@@ -426,7 +426,7 @@ await().atMost(5, TimeUnit.SECONDS).until(".small").hasSize(3);
```
The default wait is 500 ms.
-Instead of hasSize, you can also use `hasText("myTextValue")`, `hasId("myId")`, `hasName("myName")`, `containsText("myName")`.
+Instead of hasSize, you can also use `hasText("myTextValue")`, `isPresent()`, `isNotPresent()`, `hasId("myId")`, `hasName("myName")`, `containsText("myName")`,`areDisplayed()`, `areEnabled()`.
The `isPresent()` assertion is going to check if there is at most one element on the page corresponding to the filter.
If you need to be more precise, you can also use filters in the search:
@@ -442,7 +442,7 @@ You can also use matchers:
await().atMost(5, TimeUnit.SECONDS).until(".small").withText().startsWith("start").isPresent();
```
-Just use `startsWith`, `notStartsWith`, `endsWith`, `notEndsWith`, `contains`, `notContains`, `equalTo`,`areDisplayed`.
+Just use `startsWith`, `notStartsWith`, `endsWith`, `notEndsWith`, `contains`, `notContains`, `equalTo`.
If you need to filter on a custom attribute name, this syntax will help :
@@ -38,7 +38,8 @@ public FluentWait(WebDriver driver, Search search) {
return this;
}
- public FluentWait<T> ignoreAll(java.util.Collection<java.lang.Class<? extends java.lang.RuntimeException>> types) {
+
+ public FluentWait<T> ignoreAll(java.util.Collection<java.lang.Class<? extends Throwable>> types) {
wait.ignoreAll(types);
return this;
@@ -200,7 +200,26 @@ public boolean apply(@Nullable WebDriver webDriver) {
return FluentThread.get();
}
+ /**
+ * Check that the element is not present
+ */
+ public Fluent isNotPresent() {
+ Predicate isNotPresent = new com.google.common.base.Predicate<WebDriver>() {
+ public boolean apply(@Nullable WebDriver webDriver) {
+ int size;
+ if (filters.size() > 0) {
+ size = search.find(selector, (Filter[]) filters.toArray(new Filter[filters.size()])).size();
+ } else {
+ size = search.find(selector).size();
+ }
+ return size == 0;
+ }
+ };
+
+ until(wait, isNotPresent, filters, isNotPresentMessage(selector));
+ return FluentThread.get();
+ }
/**
* Check that the elements are all displayed
@@ -238,6 +257,42 @@ public boolean apply(@Nullable WebDriver webDriver) {
return FluentThread.get();
}
+ /**
+ * Check that the elements are all enabled
+ *
+ * @return
+ */
+ public Fluent areEnabled() {
+ Predicate isVisible = new com.google.common.base.Predicate<WebDriver>() {
+ public boolean apply(@Nullable WebDriver webDriver) {
+ if (filters.size() > 0) {
+ FluentList<FluentWebElement> fluentWebElements = search.find(selector, (Filter[]) filters.toArray(new Filter[filters.size()]));
+ if (fluentWebElements.size() > 0) {
+ for (FluentWebElement fluentWebElement : fluentWebElements) {
+ if (!fluentWebElement.isEnabled()) {
+ return false;
+ }
+ }
+ return true;
+ }
+ } else {
+ FluentList<FluentWebElement> fluentWebElements = search.find(selector);
+ if (fluentWebElements.size() > 0) {
+ for (FluentWebElement fluentWebElement : fluentWebElements) {
+ if (!fluentWebElement.isEnabled()) {
+ return false;
+ }
+ }
+ return true;
+ }
+ }
+ return false;
+ }
+ };
+ until(wait, isVisible, filters, isDisplayedMessage(selector));
+ return FluentThread.get();
+ }
+
/**
* Create a filter builder for the attribute
*
@@ -11,6 +11,7 @@
private static final String SELECTOR = "Selector ";
private static final String PAGE = "Page ";
private static final String IS_NOT_PRESENT = " is not present.";
+ private static final String IS_PRESENT = " is present.";
private static final String HAS_NOT_THE_NAME = " has not the name ";
private static final String HAS_NOT_THE_SIZE = " has not the size";
private static final String POINT = ".";
@@ -43,6 +44,10 @@ static final String isPresentMessage(String selector) {
return SELECTOR + selector + IS_NOT_PRESENT;
}
+ static final String isNotPresentMessage(String selector) {
+ return SELECTOR + selector + IS_PRESENT;
+ }
+
static final String isDisplayedMessage(String selector) {
return SELECTOR + selector + IS_NOT_DISPLAY;
}
@@ -44,5 +44,6 @@
<div id="unvisible" style="display:none;"></div>
+<input type="checkbox" disabled="true" id="disabled" />
</body>
</html>
@@ -25,6 +25,7 @@
public void checkFillAction() {
goTo(DEFAULT_URL);
$("input").text("zzz");
+
assertThat($("input").getValues()).contains("zzz");
}
@@ -108,12 +108,20 @@ public void checkAwaitContainsId() {
@Test
public void checkAwaitContainsTextWithTextMatcher() {
-
await().atMost(1, NANOSECONDS).until(".small").withText().contains("Small 1").isPresent();
}
@Test
+ public void when_a_element_is_not_present_then_isNotEnabled_return_true() {
+ await().atMost(1, NANOSECONDS).until(".small").withText().contains("notPresent").isNotPresent();
+ }
+
+ @Test(expected = TimeoutException.class)
+ public void when_a_element_is_present_then_isNotEnabled_throw_an_exception() {
+ await().atMost(1, NANOSECONDS).until(".small").withText().contains("Small 1").isNotPresent();
+ }
+ @Test
public void checkAwaitStartWithRegex() {
await().atMost(1, NANOSECONDS).until(".small").with("id").startsWith(regex("id")).hasSize(2);
}
@@ -217,6 +225,19 @@ public void when_element_is_not_displayed_then_isDisplayed_throws_exception() {
}
@Test
+ public void when_element_is_present_then_areEnabled_return_true() {
+ goTo(JAVASCRIPT_URL);
+ await().atMost(1, NANOSECONDS).until("#default").areEnabled();
+ }
+
+ @Test(expected = TimeoutException.class)
+ public void when_element_is_not_displayed_then_areEnabled_throws_exception() {
+ goTo(JAVASCRIPT_URL);
+ await().atMost(1, NANOSECONDS).until("#disabled").areEnabled();
+ }
+
+
+ @Test
public void when_element_is_not_displayed_then_isPresent_return_true() {
goTo(JAVASCRIPT_URL);
await().atMost(1, NANOSECONDS).until("#unvisible").isPresent();
@@ -44,5 +44,7 @@
<div id="unvisible" style="display:none;"></div>
+<input type="checkbox" disabled="true" id="disabled" />
+
</body>
</html>

0 comments on commit 1e17349

Please sign in to comment.