All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.fluentlenium.core.conditions.AtLeastOneElementConditions Maven / Gradle / Ivy

package org.fluentlenium.core.conditions;

import java.util.List;
import java.util.function.Predicate;

import org.fluentlenium.core.FluentDriver;
import org.fluentlenium.core.domain.FluentWebElement;

/**
 * Conditions for list of elements, matching when at least one element of the list matches.
 */
public class AtLeastOneElementConditions extends AbstractFluentListConditions {
    /**
     * Creates a new condition.
     *
     * @param elements underlying elements
     */
    public AtLeastOneElementConditions(List elements) {
        super(elements);
    }

    @Override
    public AtLeastOneElementConditions not() {
        AtLeastOneElementConditions negatedConditions = new AtLeastOneElementConditions(getElements());
        negatedConditions.setNegation(!isNegation());
        return negatedConditions;
    }

    @Override
    public boolean verify(Predicate predicate, boolean defaultValue) {
        if (isNegation()) {
            predicate = predicate.negate();
            defaultValue = !defaultValue;
        }
        return buildAtLeastOnePredicate(predicate, defaultValue).test(null);
    }

    /**
     * Build predicate for this condition.
     *
     * @param predicate    predicate
     * @param defaultValue default value if elements list is empty.
     * @return predicate
     */
    protected Predicate buildAtLeastOnePredicate(Predicate predicate, boolean defaultValue) {
        Predicate untilPredicate = fluent -> {
            if (!getElements().isEmpty()) {
                for (FluentWebElement element : getElements()) {
                    if (predicate.test(element)) {
                        return true;
                    }
                }
                return false;
            }
            return defaultValue;
        };
        return untilPredicate;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy