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()");
}
}
}
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);
}
}
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);
}
Aggregations