Skip to content

Commit

Permalink
Add conditions object on FluentWebElement as ExpectedConditions wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Toilal committed Jul 4, 2016
1 parent 89a25e7 commit 92ae5ff
Show file tree
Hide file tree
Showing 6 changed files with 383 additions and 92 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.fluentlenium.core.conditions;

import org.fluentlenium.adapter.FluentAdapter;
import org.fluentlenium.core.FluentThread;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

/**
* Fluent object to handle {@link org.openqa.selenium.support.ui.ExpectedConditions} on FluentWebElement in fluentlenium API.
*/
public class FluentConditions {
private WebElement element;

public FluentConditions(WebElement element) {
this.element = element;
}

/**
* Check that this element is visible and enabled such that you can click it.
*
* @return true if the element can be clicked, false otherwise.
*/
public boolean isClickable() {
FluentAdapter fluent = FluentThread.get();
return ExpectedConditions.elementToBeClickable(element).apply(fluent.getDriver()) != null;
}

/**
* Check that this element is no longer attached to the DOM.
*
* @return false is the element is still attached to the DOM, true otherwise.
*/
public boolean isStale() {
FluentAdapter fluent = FluentThread.get();
return ExpectedConditions.stalenessOf(element).apply(fluent.getDriver());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.fluentlenium.core.action.FluentDefaultActions;
import org.fluentlenium.core.action.MouseActions;
import org.fluentlenium.core.axes.Axes;
import org.fluentlenium.core.conditions.FluentConditions;
import org.fluentlenium.core.filter.Filter;
import org.fluentlenium.core.search.Search;
import org.fluentlenium.core.search.SearchActions;
Expand All @@ -23,11 +24,13 @@ public class FluentWebElement implements FluentDefaultActions<FluentWebElement>,
private final WebElement webElement;
private final Search search;
private final Axes axes;
private final FluentConditions conditions;

public FluentWebElement(WebElement webElement) {
this.webElement = webElement;
this.search = new Search(webElement);
this.axes = new Axes(webElement);
this.conditions = new FluentConditions(webElement);
}

/**
Expand All @@ -40,6 +43,15 @@ public FluentWebElement click() {
return this;
}

/**
* {@link org.openqa.selenium.support.ui.ExpectedConditions} wrapper on this element.
*
* @return object to perform {@link org.openqa.selenium.support.ui.ExpectedConditions} assertions.
*/
public FluentConditions conditions() {
return conditions;
}

/**
* XPath Axes accessor (parent, ancestors, preceding, following, ...).
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,16 @@

import static org.fluentlenium.core.wait.FluentWaitMessages.hasSizeMessage;
import static org.fluentlenium.core.wait.FluentWaitMessages.isDisplayedMessage;
import static org.fluentlenium.core.wait.FluentWaitMessages.isNotDisplayedMessage;
import static org.fluentlenium.core.wait.FluentWaitMessages.isEnabledMessage;
import static org.fluentlenium.core.wait.FluentWaitMessages.isNotEnabledMessage;
import static org.fluentlenium.core.wait.FluentWaitMessages.isPredicateVerifiedMessage;
import static org.fluentlenium.core.wait.FluentWaitMessages.isClickableMessage;
import static org.fluentlenium.core.wait.FluentWaitMessages.isNotClickableMessage;
import static org.fluentlenium.core.wait.FluentWaitMessages.isSelectedMessage;
import static org.fluentlenium.core.wait.FluentWaitMessages.isNotSelectedMessage;
import static org.fluentlenium.core.wait.FluentWaitMessages.isStaleMessage;
import static org.fluentlenium.core.wait.FluentWaitMessages.isNotStaleMessage;

/**
* Base Matcher for waiting on element list.
Expand All @@ -19,65 +28,167 @@ public AbstractWaitElementListMatcher(Search search, FluentWait wait, String sel
super(search, wait, selectionName);
}


/**
* Check that the elements are all enabled
* Check that given predicate is verified for all elements.
*
* @param predicate predicate function to verify for each element.
* @param defaultValue value to use when no element match.
*/
public void areEnabled() {
Predicate<Fluent> isEnabled = new com.google.common.base.Predicate<Fluent>() {
public void areVerified(Predicate<FluentWebElement> predicate, boolean defaultValue) {
Predicate<Fluent> allPredicate = buildAllPredicate(predicate, defaultValue);
until(wait, allPredicate, isPredicateVerifiedMessage(selectionName));
}

protected Predicate<Fluent> buildAllPredicate(final Predicate<FluentWebElement> predicate, final boolean defaultValue) {
Predicate<Fluent> untilPredicate = new com.google.common.base.Predicate<Fluent>() {
public boolean apply(Fluent fluent) {
FluentList<? extends FluentWebElement> fluentWebElements = find();
if (fluentWebElements.size() > 0) {
for (FluentWebElement fluentWebElement : fluentWebElements) {
if (!fluentWebElement.isEnabled()) {
if (!predicate.apply(fluentWebElement)) {
return false;
}
}
return true;
}
return false;
return defaultValue;
}
};
until(wait, isEnabled, isEnabledMessage(selectionName));
return untilPredicate;
}


/**
* Check that the elements are all enabled
*/
public void areEnabled() {
Predicate<Fluent> predicate = buildAllPredicate(new Predicate<FluentWebElement>() {
@Override
public boolean apply(FluentWebElement input) {
return input.isEnabled();
}
}, false);
until(wait, predicate, isEnabledMessage(selectionName));
}

/**
* Check that the elements are all not enabled
*/
public void areNotEnabled() {
Predicate<Fluent> predicate = buildAllPredicate(new Predicate<FluentWebElement>() {
@Override
public boolean apply(FluentWebElement input) {
return !input.isEnabled();
}
}, false);
until(wait, predicate, isNotEnabledMessage(selectionName));
}

/**
* Check that all the elements are all displayed
*/
public void areDisplayed() {
Predicate<Fluent> isVisible = new com.google.common.base.Predicate<Fluent>() {
public boolean apply(Fluent fluent) {
FluentList<? extends FluentWebElement> fluentWebElements = find();
if (fluentWebElements.size() > 0) {
for (FluentWebElement fluentWebElement : fluentWebElements) {
if (!fluentWebElement.isDisplayed()) {
return false;
}
}
return true;
}
return false;
Predicate<Fluent> predicate = buildAllPredicate(new Predicate<FluentWebElement>() {
@Override
public boolean apply(FluentWebElement input) {
return input.isDisplayed();
}
};
until(wait, isVisible, isDisplayedMessage(selectionName));
}, false);
until(wait, predicate, isDisplayedMessage(selectionName));
}

/**
* Check that all the elements are not displayed
*/
public void areNotDisplayed() {
Predicate<Fluent> isNotVisible = new com.google.common.base.Predicate<Fluent>() {
public boolean apply(Fluent fluent) {
FluentList<? extends FluentWebElement> fluentWebElements = find();
for (FluentWebElement fluentWebElement : fluentWebElements) {
if (fluentWebElement.isDisplayed()) {
return false;
}
}
return true;
Predicate<Fluent> predicate = buildAllPredicate(new Predicate<FluentWebElement>() {
@Override
public boolean apply(FluentWebElement input) {
return !input.isDisplayed();
}
};
until(wait, isNotVisible, isDisplayedMessage(selectionName));
}, true);
until(wait, predicate, isNotDisplayedMessage(selectionName));
}

/**
* Check that all the elements are clickable
*/
public void areClickable() {
Predicate<Fluent> predicate = buildAllPredicate(new Predicate<FluentWebElement>() {
@Override
public boolean apply(FluentWebElement input) {
return input.conditions().isClickable();
}
}, false);

until(wait, predicate, isClickableMessage(selectionName));
}

/**
* Check that all the elements are not clickable
*/
public void areNotClickable() {
Predicate<Fluent> predicate = buildAllPredicate(new Predicate<FluentWebElement>() {
@Override
public boolean apply(FluentWebElement input) {
return !input.conditions().isClickable();
}
}, false);

until(wait, predicate, isNotClickableMessage(selectionName));
}


/**
* Check that all the elements are selected
*/
public void areSelected() {
Predicate<Fluent> isSelected = buildAllPredicate(new Predicate<FluentWebElement>() {
@Override
public boolean apply(FluentWebElement input) {
return input.isSelected();
}
}, false);
until(wait, isSelected, isSelectedMessage(selectionName));
}

/**
* Check that all the elements are not selected
*/
public void areNotSelected() {
Predicate<Fluent> isNotSelected = buildAllPredicate(new Predicate<FluentWebElement>() {
@Override
public boolean apply(FluentWebElement input) {
return !input.isSelected();
}
}, false);
until(wait, isNotSelected, isNotSelectedMessage(selectionName));
}

/**
* Check that one or more element is stale
*/
public void areStale() {
Predicate<Fluent> isClickable = buildAllPredicate(new Predicate<FluentWebElement>() {
@Override
public boolean apply(FluentWebElement input) {
return input.conditions().isStale();
}
}, false);
until(wait, isClickable, isStaleMessage(selectionName));
}

/**
* Check that one or more element is not stale
*/
public void areNotStale() {
Predicate<Fluent> isClickable = buildAllPredicate(new Predicate<FluentWebElement>() {
@Override
public boolean apply(FluentWebElement input) {
return input.conditions().isStale();
}
}, false);
until(wait, isClickable, isNotStaleMessage(selectionName));
}

/**
Expand Down
Loading

0 comments on commit 92ae5ff

Please sign in to comment.