Skip to content

Commit

Permalink
Merge pull request #54 from Frameworkium/extra-expected-conditions
Browse files Browse the repository at this point in the history
Updated ExtraExpectedConditions.java to include toString()
  • Loading branch information
robertgates55 authored Nov 14, 2016
2 parents 9990d37 + f69cb40 commit a17afb2
Showing 1 changed file with 68 additions and 38 deletions.
106 changes: 68 additions & 38 deletions src/main/java/com/frameworkium/core/ui/ExtraExpectedConditions.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import ru.yandex.qatools.htmlelements.element.TypifiedElement;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* Frameworkium extension of {@link ExpectedConditions}.
Expand All @@ -30,44 +32,41 @@ public class ExtraExpectedConditions {
* iff the element is visible, otherwise <strong>true</strong>.
*/
public static ExpectedCondition<Boolean> notPresentOrInvisible(
WebElement element) {
return driver -> {
try {
return !element.isDisplayed();
} catch (NoSuchElementException e) {
return true;
}
};
final WebElement element) {

return expectedCondition(driver -> {
try {
return !element.isDisplayed();
} catch (NoSuchElementException e) {
return true;
}
},
String.format(
"element '%s' to not be present or be invisible", element));
}

/**
* Overloaded {@link ExtraExpectedConditions#notPresentOrInvisible(WebElement)}
* for {@link List} of {@link WebElement}s.
*
* @param elements the lazy proxy for <code>List&lt;WebElement&gt;</code> to wait for
* @return an {@link ExpectedCondition} which returns <strong>false</strong>
* iff any element is visible, otherwise <strong>true</strong>.
* @return an {@link ExpectedCondition} which returns the <strong>list</strong>
* iff any element is visible, otherwise <strong>null</strong>.
* @see ExtraExpectedConditions#notPresentOrInvisible(WebElement)
*/
public static ExpectedCondition<List<? extends WebElement>> notPresentOrInvisible(
List<? extends WebElement> elements) {

return driver ->
elements.stream()
.noneMatch(WebElement::isDisplayed)
? elements
: null;
}

/**
* @return true iff jQuery is available and 0 ajax queries are active.
*/
public static ExpectedCondition<Boolean> jQueryAjaxDone() {
final List<? extends WebElement> elements) {

return driver ->
(Boolean) ((JavascriptExecutor) driver)
.executeScript(
"return !!window.jQuery && jQuery.active === 0;");
return expectedCondition(driver ->
elements.stream()
.noneMatch(WebElement::isDisplayed)
? elements
: null,
String.format(
"the following elements to not be present or be invisible: %s",
elements.stream()
.map(WebElement::toString)
.collect(Collectors.joining(", "))));
}

/**
Expand All @@ -80,10 +79,9 @@ public static ExpectedCondition<Boolean> jQueryAjaxDone() {
public static ExpectedCondition<List<? extends WebElement>> sizeGreaterThan(
List<? extends WebElement> list, int expectedSize) {

return driver ->
list.size() > expectedSize
? list
: null;
return expectedCondition(
driver -> list.size() > expectedSize ? list : null,
"list size of " + list.size() + " to be greater than " + expectedSize);
}

/**
Expand All @@ -96,10 +94,19 @@ public static ExpectedCondition<List<? extends WebElement>> sizeGreaterThan(
public static ExpectedCondition<List<? extends WebElement>> sizeLessThan(
List<? extends WebElement> list, int expectedSize) {

return driver ->
list.size() < expectedSize
? list
: null;
return expectedCondition(
driver -> list.size() < expectedSize ? list : null,
"list size of " + list.size() + " to be less than " + expectedSize);
}

/**
* @return true iff jQuery is available and 0 ajax queries are active.
*/
public static ExpectedCondition<Boolean> jQueryAjaxDone() {

return javascriptExpectedCondition(
"return !!window.jQuery && jQuery.active === 0;",
"jQuery AJAX queries to not be active");
}

/**
Expand All @@ -111,8 +118,31 @@ public static ExpectedCondition<List<? extends WebElement>> sizeLessThan(
*/
public static ExpectedCondition<Boolean> documentBodyReady() {

return driver ->
(Boolean) ((JavascriptExecutor) driver)
.executeScript("return document.readyState == 'complete';");
return javascriptExpectedCondition(
"return document.readyState == 'complete';",
"the document ready state to equal 'complete'");
}

private static ExpectedCondition<Boolean> javascriptExpectedCondition(
String query, String message) {
return expectedCondition(
driver -> (Boolean) ((JavascriptExecutor) driver).executeScript(query),
message);
}

private static <T> ExpectedCondition<T> expectedCondition(
Function<WebDriver, T> function, String string) {

return new ExpectedCondition<T>() {
@Override
public T apply(WebDriver driver) {
return function.apply(driver);
}

@Override
public String toString() {
return string;
}
};
}
}

0 comments on commit a17afb2

Please sign in to comment.