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

info.novatec.testit.webtester.internal.ActionTemplate Maven / Gradle / Ivy

package info.novatec.testit.webtester.internal;

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

import lombok.experimental.UtilityClass;

import info.novatec.testit.webtester.browser.Browser;
import info.novatec.testit.webtester.css.DefaultStyleChanger;
import info.novatec.testit.webtester.events.Event;
import info.novatec.testit.webtester.events.EventSystem;
import info.novatec.testit.webtester.markings.Marker;
import info.novatec.testit.webtester.pagefragments.PageFragment;

@UtilityClass
public class ActionTemplate {

    public static  PageFragmentAction pageFragment(T subject) {
        return new PageFragmentAction<>(subject);
    }

    public static  PageFragmentsAction pageFragments(T subject1, T subject2) {
        return new PageFragmentsAction<>(subject1, subject2);
    }

    public static  BrowserAction browser(T subject) {
        return new BrowserAction<>(subject);
    }

    public static class Action> {

        private final T subject;
        private final Browser browser;

        public Action(T subject, Browser browser) {
            this.subject = subject;
            this.browser = browser;
        }

        @SuppressWarnings({ "unchecked", "PMD.AvoidCatchingGenericException" })
        public A execute(Consumer consumer) {
            try {
                consumer.accept(subject);
            } catch (RuntimeException e) {
                browser.events().fireExceptionEvent(e);
                throw e;
            }
            return ( A ) this;
        }

        protected T getSubject() {
            return subject;
        }

        protected EventSystem getEventSystem() {
            return browser.events();
        }

    }

    public static class BiAction> {

        private final T subject1;
        private final T subject2;
        private final Browser browser;

        public BiAction(T subject1, T subject2, Browser browser) {
            this.subject1 = subject1;
            this.subject2 = subject2;
            this.browser = browser;
        }

        @SuppressWarnings({ "unchecked", "PMD.AvoidCatchingGenericException" })
        public A execute(BiConsumer consumer) {
            try {
                consumer.accept(subject1, subject2);
            } catch (RuntimeException e) {
                browser.events().fireExceptionEvent(e);
                throw e;
            }
            return ( A ) this;
        }

        protected T getSubject1() {
            return subject1;
        }

        protected T getSubject2() {
            return subject2;
        }

        protected EventSystem getEventSystem() {
            return browser.events();
        }

    }

    public static class PageFragmentAction extends Action> {

        private Marker marker;

        public PageFragmentAction(T subject) {
            super(subject, subject.browser());
            marker = new Marker(new DefaultStyleChanger());
        }

        public PageFragmentAction fireEvent(Function function) {
            EventSystem eventSystem = getEventSystem();
            if (eventSystem.isEnabled()) {
                eventSystem.fireEvent(function.apply(getSubject()));
            }
            return this;
        }

        public PageFragmentAction markAsUsed() {
            marker.markAsUsed(getSubject());
            return this;
        }

        public PageFragmentAction markAsRead() {
            marker.markAsRead(getSubject());
            return this;
        }

    }

    public static class PageFragmentsAction extends BiAction> {

        private Marker marker;

        public PageFragmentsAction(T subject1, T subject2) {
            super(subject1, subject2, subject1.browser());
            marker = new Marker(new DefaultStyleChanger());
        }

        public PageFragmentsAction fireEvent(BiFunction function) {
            EventSystem eventSystem = getEventSystem();
            if (eventSystem.isEnabled()) {
                eventSystem.fireEvent(function.apply(getSubject1(), getSubject2()));
            }
            return this;
        }

        public PageFragmentsAction markAsUsed() {
            marker.markAsUsed(getSubject1());
            marker.markAsUsed(getSubject2());
            return this;
        }

        public PageFragmentsAction markAsRead() {
            marker.markAsRead(getSubject1());
            marker.markAsRead(getSubject2());
            return this;
        }

    }

    public static class BrowserAction extends Action> {

        public BrowserAction(T subject) {
            super(subject, subject);
        }

        public BrowserAction fireEvent(Function function) {
            EventSystem eventSystem = getEventSystem();
            if (eventSystem.isEnabled()) {
                eventSystem.fireEvent(function.apply(getSubject()));
            }
            return this;
        }

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy