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

com.chavaillaz.browser.engine.AutomatedBrowserFlow Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package com.chavaillaz.browser.engine;

import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.WebDriver;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

import static java.util.Optional.ofNullable;

@Slf4j
public class AutomatedBrowserFlow {

    private final B automatedBrowser;
    private BiConsumer defaultExceptionHandler;
    private C context;

    /**
     * Creates a new automated browser flow.
     *
     * @param automatedBrowser The automated browser instance to use
     */
    public AutomatedBrowserFlow(B automatedBrowser) {
        this.automatedBrowser = automatedBrowser;
    }

    /**
     * Creates a new automated browser flow.
     * This will use an instance of {@link AutomatedBrowser} behind.
     *
     * @param driver The browser driver to use
     */
    public AutomatedBrowserFlow(WebDriver driver) {
        this.automatedBrowser = (B) new AutomatedBrowser(driver);
    }

    /**
     * Sets the context instance used to store data and state between browsing steps.
     *
     * @param context The context instance to set
     * @return The current flow instance
     */
    public AutomatedBrowserFlow withContext(C context) {
        this.context = context;
        return this;
    }

    /**
     * Sets the default exception handler when executing flow steps.
     *
     * @param exceptionHandler The exception handler to set
     * @return The current flow instance
     */
    public AutomatedBrowserFlow withDefaultExceptionHandler(BiConsumer exceptionHandler) {
        this.defaultExceptionHandler = exceptionHandler;
        return this;
    }

    /**
     * Sets the default exception handler when executing flow steps.
     *
     * @param exceptionHandler The exception handler to set
     * @return The current flow instance
     */
    public AutomatedBrowserFlow withDefaultExceptionHandler(Consumer exceptionHandler) {
        this.defaultExceptionHandler = (exception, unusedContext) -> exceptionHandler.accept(exception);
        return this;
    }

    /**
     * Executes a flow step with a specific exception handler.
     *
     * @param step             The step to execute
     * @param exceptionHandler The exception handler overriding the default one
     * @return The current flow instance
     */
    public AutomatedBrowserFlow withStep(BiConsumer step, BiConsumer exceptionHandler) {
        try {
            step.accept(automatedBrowser, context);
        } catch (Exception e) {
            handleException(exceptionHandler, e);
        }
        return this;
    }

    /**
     * Executes a flow step.
     *
     * @param step The step to execute
     * @return The current flow instance
     */
    public AutomatedBrowserFlow withStep(BiConsumer step) {
        return withStep(step, defaultExceptionHandler);
    }

    /**
     * Executes a flow step with a specific exception handler.
     *
     * @param step             The step to execute
     * @param exceptionHandler The exception handler overriding the default one
     * @return The current flow instance
     */
    public AutomatedBrowserFlow withStep(Consumer step, BiConsumer exceptionHandler) {
        try {
            step.accept(automatedBrowser);
        } catch (Exception e) {
            handleException(exceptionHandler, e);
        }
        return this;
    }

    /**
     * Executes a flow step.
     *
     * @param step The step to execute
     * @return The current flow instance
     */
    public AutomatedBrowserFlow withStep(Consumer step) {
        return withStep(step, defaultExceptionHandler);
    }

    /**
     * Executes a flow step with a specific exception handler.
     *
     * @param step             The step to execute
     * @param exceptionHandler The exception handler overriding the default one
     * @return The current flow instance
     */
    public AutomatedBrowserFlow withStep(Runnable step, BiConsumer exceptionHandler) {
        try {
            step.run();
        } catch (Exception e) {
            handleException(exceptionHandler, e);
        }
        return this;
    }

    /**
     * Executes a flow step.
     *
     * @param step The step to execute
     * @return The current flow instance
     */
    public AutomatedBrowserFlow withStep(Runnable step) {
        return withStep(step, defaultExceptionHandler);
    }

    /**
     * Handles an exception during a browsing step.
     *
     * @param exceptionHandler The exception handler to use, may be {@code null}
     * @param exception        The exception to handle
     */
    protected void handleException(BiConsumer exceptionHandler, Exception exception) {
        ofNullable(exceptionHandler).ifPresentOrElse(
                handler -> handler.accept(exception, context),
                () -> log.error("Unhandled exception in step", exception));
    }

}