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

com.redhat.darcy.ui.Elements Maven / Gradle / Ivy

Go to download

Framework for writing page objects to automate interaction with graphical user interfaces.

The newest version!
/*
 Copyright 2014 Red Hat, Inc. and/or its affiliates.

 This file is part of darcy-ui.

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see .
 */

package com.redhat.darcy.ui;

import com.redhat.darcy.ui.api.HasElementContext;
import com.redhat.darcy.ui.api.Locator;
import com.redhat.darcy.ui.api.WrapsElement;
import com.redhat.darcy.ui.api.elements.Button;
import com.redhat.darcy.ui.api.elements.Checkbox;
import com.redhat.darcy.ui.api.elements.DateInput;
import com.redhat.darcy.ui.api.elements.Element;
import com.redhat.darcy.ui.api.elements.FileSelect;
import com.redhat.darcy.ui.api.elements.Label;
import com.redhat.darcy.ui.api.elements.Link;
import com.redhat.darcy.ui.api.elements.MultiSelect;
import com.redhat.darcy.ui.api.elements.Radio;
import com.redhat.darcy.ui.api.elements.Select;
import com.redhat.darcy.ui.api.elements.Text;
import com.redhat.darcy.ui.api.elements.TextInput;
import com.redhat.darcy.ui.internal.ElementHandler;
import com.redhat.darcy.ui.internal.ElementListHandler;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.List;

/**
 * Static factories for the fundamental UI elements. Specifically, these return proxy instances of
 * those elements, so that they may be defined as instance fields in a View that may not yet have
 * a context with which to retrieve element references. This elements can be assigned a context
 * after instantiation.
 *
 * @see com.redhat.darcy.ui.AbstractView
 * @see com.redhat.darcy.ui.api.HasElementContext
 * @see com.redhat.darcy.ui.internal.ElementHandler
 *
 */
public abstract class Elements {
    /**
     * Looks up a automation-library-specific implementation for that element type, assuming an
     * implementation is registered for that class.
     */
    @SuppressWarnings("unchecked")
    public static  T element(Class type, Locator locator) {
        if (!type.isInterface()) {
            throw new IllegalArgumentException("Element type must be an interface, was: " + type);
        }

        InvocationHandler invocationHandler = new ElementHandler(type, locator);
        
        return (T) Proxy.newProxyInstance(Elements.class.getClassLoader(), 
                new Class[] { type, HasElementContext.class, WrapsElement.class},
                invocationHandler);
    }
    /**
     * Looks up a automation-library-specific implementation for that element type, assuming an
     * implementation is registered for that class.
     */
    @SuppressWarnings("unchecked")
    public static  List elements(Class type, Locator locator) {
        if (!type.isInterface()) {
            throw new IllegalArgumentException("Element type must be an interface, was: " + type);
        }

        InvocationHandler invocationHandler = new ElementListHandler(type, locator);
        
        return (List) Proxy.newProxyInstance(Elements.class.getClassLoader(), 
                new Class[] { List.class, HasElementContext.class },
                invocationHandler);
    }
    
    public static Element element(Locator locator) {
        return element(Element.class, locator);
    }
    
    public static List elements(Locator locator) {
        return elements(Element.class, locator);
    }
    
    public static TextInput textInput(Locator locator) {
        return element(TextInput.class, locator);
    }
    
    public static List textInputs(Locator locator) {
        return elements(TextInput.class, locator);
    }
    
    public static Button button(Locator locator) {
        return element(Button.class, locator);
    }
    
    public static List