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

org.popper.fw.jemmy.elements.impl.AbstractJemmyElement Maven / Gradle / Ivy

/*
 * Copyright [2013] [Michael Bulla, [email protected]]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.popper.fw.jemmy.elements.impl;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.netbeans.jemmy.ComponentChooser;
import org.netbeans.jemmy.TimeoutExpiredException;
import org.netbeans.jemmy.operators.ComponentOperator;
import org.netbeans.jemmy.operators.ContainerOperator;
import org.popper.fw.jemmy.JemmyContext;
import org.popper.fw.jemmy.JemmyPageObjectHelper.SearchContextProvider;
import org.popper.fw.jemmy.elements.IJemmyElement;
import org.popper.fw.jemmy.elements.IJemmyLabel;

/**
 * Abstract Superclass for all Elements in an application that can be interacted with. Interaction may mean
 * readonly actions (see {@link IJemmyLabel}) or read-write action (see {@link AbstractJemmyInput}).
 *
 * This class is responsible to handle location of elements and handle errormessages (with help of the
 * getElement-method).
 *
 * When implementing a new Jemmy Element, it should be a subclass of {@link AbstractJemmyElement}.
 *
 * @author Michael
 *
 */
public abstract class AbstractJemmyElement implements IJemmyElement {

    protected final JemmyElementReference reference;

    protected final Class operatorType;

    protected AbstractJemmyElement(JemmyElementReference reference, Class operatorType) {
        this.reference = reference;
        this.operatorType = operatorType;
    }

    protected JemmyContext getContext() {
        return reference.getContext();
    }

    @Override
    public T getOperator() {
        try {
            Constructor constructor = operatorType.getConstructor(ContainerOperator.class, ComponentChooser.class);
            ContainerOperator parent = reference.getParent().getExtension(SearchContextProvider.class)
                    .getSearchContext();

            return constructor.newInstance(parent, reference.getBy());
        } catch (InvocationTargetException ite) {
            Throwable cause = ite.getTargetException();
            if (cause instanceof RuntimeException) {
                throw (RuntimeException) cause;
            }
            throw new RuntimeException(cause);
        } catch (RuntimeException re) {
            throw re;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public T getOperatorFast() {
        int oldTimeout = reference.getContext().getRelevantTimeouts();
        try {
            reference.getContext().setRelevantTimeouts(0);
            return getOperator();
        } finally {
            reference.getContext().setRelevantTimeouts(oldTimeout);
        }
    }

    /**
     * Convenience-method
     * @param timeInMillis time to sleep in Milliseconds
     */
    protected void sleep(long timeInMillis) {
        try {
            Thread.sleep(timeInMillis);
        } catch (InterruptedException e) {
            // nothing to do
        }
    }

    /*
     * (non-Javadoc)
     * @see org.popper.fw.element.IElement#isDisplayed()
     */
    @Override
    public boolean isDisplayed() {
        try {
            ComponentOperator op = getOperatorFast();
            return op.isVisible();
        } catch (TimeoutExpiredException e) {
            e.printStackTrace();
            return false;
        }
    }

    /*
     * (non-Javadoc)
     * @see org.popper.fw.element.IElement#isSelected()
     */
    @Override
    public boolean isSelected() {
        throw new IllegalStateException("method not implemented");
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy