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

org.fluentlenium.core.wait.BaseWaitConditions Maven / Gradle / Ivy

package org.fluentlenium.core.wait;

import org.fluentlenium.core.FluentControl;
import org.fluentlenium.core.conditions.Conditions;
import org.fluentlenium.core.conditions.message.MessageProxy;

import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * Common class for all wait conditions instances.
 */
public class BaseWaitConditions {
    /**
     * Build the final message from default message.
     *
     * @return final message
     */
    protected Function messageCustomizer() {
        return Function.identity();
    }

    /**
     * Perform the wait.
     *
     * @param wait    fluent wait object.
     * @param present predicate to wait for.
     * @param message message to use.
     */
    protected void until(FluentWait wait, Predicate present, String message) {
        if (wait.hasMessageDefined()) {
            wait.untilPredicate(present);
        } else {
            message = messageCustomizer().apply(message);
            wait.withMessage(message).untilPredicate(present);
        }
    }

    /**
     * Perform the wait.
     *
     * @param wait            fluent wait object.
     * @param present         predicate to wait for.
     * @param messageSupplier default message to use.
     */
    protected void until(FluentWait wait, Predicate present, Supplier messageSupplier) {
        if (wait.hasMessageDefined()) {
            wait.untilPredicate(present);
        } else {
            Supplier customMessageSupplier = () -> messageCustomizer().apply(messageSupplier.get());
            wait.withMessage(customMessageSupplier).untilPredicate(present);
        }
    }

    /**
     * Perform the wait.
     *
     * @param wait              fluent wait object
     * @param condition         condition object to wait for
     * @param messageBuilder    message builder matching the condition object
     * @param conditionFunction condition fonction
     * @param                type of the condition.
     */
    protected > void until(FluentWait wait, T condition, T messageBuilder,
            Function conditionFunction) {
        Predicate predicate = input -> conditionFunction.apply(condition);
        Supplier messageSupplier = () -> {
            conditionFunction.apply(messageBuilder);
            return MessageProxy.message(messageBuilder);
        };

        until(wait, predicate, messageSupplier);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy