Search in sources :

Example 1 with ContainerFixture

use of org.fest.swing.fixture.ContainerFixture in project ats-framework by Axway.

the class SwingElementLocator method getContainerFixture.

/**
 * Change container by specified name or title.
 * For internal use
 * @param driver Swing driver
 * @param containerProperties property with name inside
 * @return the {@link ContainerFixture}
 */
public static ContainerFixture<?> getContainerFixture(SwingDriverInternal driver, UiElementProperties containerProperties) {
    String containerName = containerProperties.getProperty("name");
    final String containerTitle = containerProperties.getProperty("title");
    if (StringUtils.isNullOrEmpty(containerName) && StringUtils.isNullOrEmpty(containerTitle)) {
        throw new IllegalArgumentException("Illegal name/title (empty/null string) passed to search for container");
    }
    ContainerFixture<?> containerFixture = driver.getActiveContainerFixture();
    ContainerFixture<?> windowsFixture = driver.getWindowFixture();
    Robot robot;
    if (containerFixture != null) {
        // use the current robot instance
        robot = containerFixture.robot;
    } else {
        robot = BasicRobot.robotWithCurrentAwtHierarchy();
    }
    if (!StringUtils.isNullOrEmpty(containerName)) {
        try {
            Container cont = BasicComponentFinder.finderWithCurrentAwtHierarchy().findByName(windowsFixture.component(), containerName, Container.class);
            return new ContainerFixture<Container>(robot, cont) {
            };
        } catch (WaitTimedOutError wtoe) {
            throw new ElementNotFoundException("Unable to find container with name '" + containerName + "' under current window/dialog. If needed change current window first with swingEngineInstance.setActiveWindow()");
        }
    } else {
        try {
            Container cont = BasicComponentFinder.finderWithCurrentAwtHierarchy().find(windowsFixture.component(), new GenericTypeMatcher<Container>(Container.class, true) {

                @Override
                protected boolean isMatching(Container component) {
                    if (component instanceof Dialog) {
                        return ((Dialog) component).getTitle().equals(containerTitle);
                    } else if (component instanceof Frame) {
                        return ((Frame) component).getTitle().equals(containerTitle);
                    } else if (component instanceof JInternalFrame) {
                        return ((JInternalFrame) component).getTitle().equals(containerTitle);
                    }
                    return false;
                }
            });
            return new ContainerFixture<Container>(robot, cont) {
            };
        } catch (WaitTimedOutError wtoe) {
            throw new ElementNotFoundException("Unable to find container with title '" + containerName + "' under current window/dialog. If needed change current window first with swingEngineInstance.setActiveWindow()");
        }
    }
}
Also used : Frame(java.awt.Frame) JInternalFrame(javax.swing.JInternalFrame) ElementNotFoundException(com.axway.ats.uiengine.exceptions.ElementNotFoundException) WaitTimedOutError(org.fest.swing.exception.WaitTimedOutError) Container(java.awt.Container) Dialog(java.awt.Dialog) ContainerFixture(org.fest.swing.fixture.ContainerFixture) Robot(org.fest.swing.core.Robot) BasicRobot(org.fest.swing.core.BasicRobot) JInternalFrame(javax.swing.JInternalFrame)

Example 2 with ContainerFixture

use of org.fest.swing.fixture.ContainerFixture in project ats-framework by Axway.

the class SwingElementLocator method findFixture.

public static ComponentFixture<? extends Component> findFixture(UiElement uiElement) {
    SwingDriverInternal driver = (SwingDriverInternal) uiElement.getUiDriver();
    ContainerFixture<?> containerFixture = (ContainerFixture<?>) driver.getActiveContainerFixture();
    Class<? extends Component> componentClass = componentsMap.get(uiElement.getClass());
    try {
        if (componentClass.equals(JButton.class)) {
            return (ComponentFixture<? extends Component>) new JButtonFixture(containerFixture.robot, (JButton) findElement(uiElement));
        } else if (componentClass.equals(JTextComponent.class)) {
            return (ComponentFixture<? extends Component>) new JTextComponentFixture(containerFixture.robot, (JTextComponent) findElement(uiElement));
        } else if (componentClass.equals(JMenuItem.class)) {
            if (uiElement.getElementProperty("path") != null) {
                return containerFixture.menuItemWithPath(uiElement.getElementProperty("path").split("[\\,\\/]+"));
            } else {
                return (ComponentFixture<? extends Component>) new JMenuItemFixture(containerFixture.robot, (JMenuItem) findElement(uiElement));
            }
        } else if (componentClass.equals(JPopupMenu.class)) {
            return (ComponentFixture<? extends Component>) new JPopupMenuFixture(containerFixture.robot, (JPopupMenu) findElement(uiElement));
        } else if (componentClass.equals(JTree.class)) {
            return (ComponentFixture<? extends Component>) new JTreeFixture(containerFixture.robot, (JTree) findElement(uiElement));
        } else if (componentClass.equals(JList.class)) {
            return (ComponentFixture<? extends Component>) new JListFixture(containerFixture.robot, (JList) findElement(uiElement));
        } else if (componentClass.equals(JCheckBox.class)) {
            return (ComponentFixture<? extends Component>) new JCheckBoxFixture(containerFixture.robot, (JCheckBox) findElement(uiElement));
        } else if (componentClass.equals(JToggleButton.class)) {
            return (ComponentFixture<? extends Component>) new JToggleButtonFixture(containerFixture.robot, (JToggleButton) findElement(uiElement));
        } else if (componentClass.equals(JComboBox.class)) {
            return (ComponentFixture<? extends Component>) new JComboBoxFixture(containerFixture.robot, (JComboBox) findElement(uiElement));
        } else if (componentClass.equals(JRadioButton.class)) {
            return (ComponentFixture<? extends Component>) new JRadioButtonFixture(containerFixture.robot, (JRadioButton) findElement(uiElement));
        } else if (componentClass.equals(JTable.class)) {
            return (ComponentFixture<? extends Component>) new JTableFixture(containerFixture.robot, (JTable) findElement(uiElement));
        } else if (componentClass.equals(JSpinner.class)) {
            return (ComponentFixture<? extends Component>) new JSpinnerFixture(containerFixture.robot, (JSpinner) findElement(uiElement));
        } else if (componentClass.equals(JTabbedPane.class)) {
            return (ComponentFixture<? extends Component>) new JTabbedPaneFixture(containerFixture.robot, (JTabbedPane) findElement(uiElement));
        } else if (componentClass.equals(JOptionPane.class)) {
            return (ComponentFixture<? extends Component>) containerFixture.optionPane();
        } else if (componentClass.equals(JLabel.class)) {
            return (ComponentFixture<? extends Component>) new JLabelFixture(containerFixture.robot, (JLabel) findElement(uiElement));
        } else if (componentClass.equals(Component.class)) {
            return new ComponentFixture<Component>(containerFixture.robot, findElement(uiElement)) {
            };
        } else if (componentClass.equals(JFileChooser.class)) {
            // TODO - might be searched by name too
            return containerFixture.fileChooser(Timeout.timeout(UiEngineConfigurator.getInstance().getElementStateChangeDelay()));
        } else {
            throw new ElementNotFoundException(uiElement.toString() + " not found. No such Fixture");
        }
    } catch (ComponentLookupException cle) {
        throw new ElementNotFoundException(uiElement.toString() + " not found.", cle);
    } catch (WaitTimedOutError exc) {
        // thrown for OptionPane search, wait for Window (BasicRobot.waitForWindow), AbstractJTableCellWriter, JTreeDriver.waitForChildrenToShowUp, each Pause wait
        throw new ElementNotFoundException(uiElement.toString() + " not found.", exc);
    }
}
Also used : JTreeFixture(org.fest.swing.fixture.JTreeFixture) JRadioButton(javax.swing.JRadioButton) SwingDriverInternal(com.axway.ats.uiengine.internal.driver.SwingDriverInternal) JTabbedPane(javax.swing.JTabbedPane) JButton(javax.swing.JButton) JTextComponent(javax.swing.text.JTextComponent) JSpinnerFixture(org.fest.swing.fixture.JSpinnerFixture) ElementNotFoundException(com.axway.ats.uiengine.exceptions.ElementNotFoundException) ComponentLookupException(org.fest.swing.exception.ComponentLookupException) WaitTimedOutError(org.fest.swing.exception.WaitTimedOutError) JLabelFixture(org.fest.swing.fixture.JLabelFixture) JTabbedPaneFixture(org.fest.swing.fixture.JTabbedPaneFixture) JButtonFixture(org.fest.swing.fixture.JButtonFixture) JToggleButtonFixture(org.fest.swing.fixture.JToggleButtonFixture) JComboBoxFixture(org.fest.swing.fixture.JComboBoxFixture) JTableFixture(org.fest.swing.fixture.JTableFixture) JToggleButton(javax.swing.JToggleButton) JTextComponentFixture(org.fest.swing.fixture.JTextComponentFixture) ComponentFixture(org.fest.swing.fixture.ComponentFixture) JMenuItemFixture(org.fest.swing.fixture.JMenuItemFixture) Component(java.awt.Component) JTextComponent(javax.swing.text.JTextComponent) JMenuItem(javax.swing.JMenuItem) JTextComponentFixture(org.fest.swing.fixture.JTextComponentFixture) JRadioButtonFixture(org.fest.swing.fixture.JRadioButtonFixture) JComboBox(javax.swing.JComboBox) JLabel(javax.swing.JLabel) JOptionPane(javax.swing.JOptionPane) JPopupMenu(javax.swing.JPopupMenu) JCheckBox(javax.swing.JCheckBox) JTree(javax.swing.JTree) JPopupMenuFixture(org.fest.swing.fixture.JPopupMenuFixture) JListFixture(org.fest.swing.fixture.JListFixture) ContainerFixture(org.fest.swing.fixture.ContainerFixture) JTable(javax.swing.JTable) JSpinner(javax.swing.JSpinner) JCheckBoxFixture(org.fest.swing.fixture.JCheckBoxFixture) JList(javax.swing.JList)

Example 3 with ContainerFixture

use of org.fest.swing.fixture.ContainerFixture in project ats-framework by Axway.

the class SwingElementLocator method useComponentInspector.

/**
 * Log the clicked element (its type, properties and index in the component hierarchy from the current container)
 *
 * @param driver {@link SwingDriverInternal} instance
 */
public static void useComponentInspector(final SwingDriverInternal driver) {
    // long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK + AWTEvent.MOUSE_EVENT_MASK;
    long eventMask = AWTEvent.MOUSE_EVENT_MASK;
    Toolkit.getDefaultToolkit().addAWTEventListener(new AWTEventListener() {

        public void eventDispatched(AWTEvent e) {
            if (e.getID() == MouseEvent.MOUSE_PRESSED && e.getSource() instanceof Component) {
                Component component = (Component) e.getSource();
                Class<?> swingClass = findSwingClass(component.getClass());
                if (swingClass == null) {
                    swingClass = component.getClass();
                    log.warn("Can't find swing class of type \"" + swingClass.getName() + "\"");
                }
                String logEntry = "\t[INSPECTOR] " + getProperties(component).replaceFirst("\\[", " [") + " index=" + calculateIndex(component, swingClass);
                if (!component.getClass().getName().equals(swingClass.getName())) {
                    logEntry += "\t(extends " + swingClass.getName() + ")";
                }
                log.info(logEntry);
            }
        }

        private String getProperties(Component c) {
            String properties = Formatting.inEdtFormat(c);
            if (c instanceof JButton) {
                String tooltip = ((JButton) c).getToolTipText();
                if (!StringUtils.isNullOrEmpty(tooltip)) {
                    int lastBrIndex = properties.lastIndexOf(']');
                    if (lastBrIndex > 0) {
                        properties = properties.substring(0, lastBrIndex) + ", tooltip='" + tooltip + "'" + properties.substring(lastBrIndex);
                    } else {
                        return c.getClass().getName() + " [tooltip='" + tooltip + "']";
                    }
                }
            }
            return properties;
        }

        private Class<?> findSwingClass(Class<?> clazz) {
            if (clazz == null) {
                return null;
            }
            if (clazz.getName().startsWith("javax.swing")) {
                return clazz;
            }
            return findSwingClass(clazz.getSuperclass());
        }

        @SuppressWarnings("unchecked")
        private int calculateIndex(final Component component, Class<?> swingClass) {
            ContainerFixture<?> containerFixture = driver.getActiveContainerFixture();
            Robot robot;
            if (containerFixture != null) {
                // use the current robot instance
                robot = containerFixture.robot;
            } else {
                robot = BasicRobot.robotWithCurrentAwtHierarchy();
            }
            List<Component> found = SwingElementFinder.find(robot.hierarchy(), ((containerFixture != null) ? containerFixture.component() : null), new GenericTypeMatcher<Component>((Class<Component>) swingClass, true) {

                @Override
                protected boolean isMatching(Component c) {
                    return true;
                }
            });
            return found.indexOf(component);
        }
    }, eventMask);
}
Also used : AWTEventListener(java.awt.event.AWTEventListener) JButton(javax.swing.JButton) GenericTypeMatcher(org.fest.swing.core.GenericTypeMatcher) ContainerFixture(org.fest.swing.fixture.ContainerFixture) AWTEvent(java.awt.AWTEvent) List(java.util.List) JList(javax.swing.JList) Component(java.awt.Component) JTextComponent(javax.swing.text.JTextComponent) Robot(org.fest.swing.core.Robot) BasicRobot(org.fest.swing.core.BasicRobot)

Aggregations

ContainerFixture (org.fest.swing.fixture.ContainerFixture)3 ElementNotFoundException (com.axway.ats.uiengine.exceptions.ElementNotFoundException)2 Component (java.awt.Component)2 JButton (javax.swing.JButton)2 JList (javax.swing.JList)2 JTextComponent (javax.swing.text.JTextComponent)2 BasicRobot (org.fest.swing.core.BasicRobot)2 Robot (org.fest.swing.core.Robot)2 WaitTimedOutError (org.fest.swing.exception.WaitTimedOutError)2 SwingDriverInternal (com.axway.ats.uiengine.internal.driver.SwingDriverInternal)1 AWTEvent (java.awt.AWTEvent)1 Container (java.awt.Container)1 Dialog (java.awt.Dialog)1 Frame (java.awt.Frame)1 AWTEventListener (java.awt.event.AWTEventListener)1 List (java.util.List)1 JCheckBox (javax.swing.JCheckBox)1 JComboBox (javax.swing.JComboBox)1 JInternalFrame (javax.swing.JInternalFrame)1 JLabel (javax.swing.JLabel)1