Search in sources :

Example 1 with MoreThanOneSuchElementException

use of com.axway.ats.uiengine.exceptions.MoreThanOneSuchElementException in project ats-framework by Axway.

the class SwingElementLocator method findElement.

@SuppressWarnings("unchecked")
private static <T extends Component> T findElement(UiElement uiElement) {
    SwingDriverInternal driver = (SwingDriverInternal) uiElement.getUiDriver();
    ContainerFixture<? extends Container> containerFixture = driver.getActiveContainerFixture();
    Class<? extends Component> componentClass;
    String exactClassName = uiElement.getElementProperties().getProperty("class");
    if (exactClassName != null) {
        try {
            componentClass = (Class<? extends Component>) SwingElementLocator.class.getClassLoader().loadClass(exactClassName);
        } catch (ClassNotFoundException ex) {
            throw new ElementNotFoundException("Could not load UI Component class named " + exactClassName + ". Probably it is not in the classpath or the name is invalid. Cause message: " + ex.getMessage(), ex);
        }
    } else {
        componentClass = componentsMap.get(uiElement.getClass());
    }
    try {
        boolean requireVisible = true;
        if (uiElement.getElementProperty("visible") != null) {
            requireVisible = Boolean.parseBoolean(uiElement.getElementProperty("visible").trim());
        }
        // Finding components by their associated labels ( someJLabel.setLabelFor( someComponent ) )
        if (uiElement.getElementProperty("label") != null) {
            return (T) containerFixture.robot.finder().findByLabel(containerFixture.component(), uiElement.getElementProperty("label"), componentClass, requireVisible);
        }
        return (T) SwingElementFinder.find(containerFixture.robot, containerFixture.component(), buildLocator(componentClass, uiElement.getElementProperties(), requireVisible, uiElement.getPropertyNamesToUseForMatch()));
    } catch (ComponentLookupException cle) {
        if (cle.getMessage().startsWith("Found more than one ")) {
            throw new MoreThanOneSuchElementException(cle.getMessage() + "\n" + uiElement.toString(), cle);
        }
        Window win = driver.getWindowFixture().component();
        Container currentContainer = containerFixture.component();
        String winTitle;
        if (win instanceof Dialog) {
            winTitle = "'" + ((Dialog) win).getTitle() + "'";
        } else if (win instanceof Frame) {
            winTitle = "'" + ((Frame) win).getTitle() + "'";
        } else {
            winTitle = "N/A";
        }
        String containerMsg = null;
        if (win.equals(currentContainer)) {
            containerMsg = "[same as window]";
        }
        if (log.isDebugEnabled()) {
            // more verbose trace
            throw new ElementNotFoundException(uiElement.toString() + " not found.\n" + "Current container: " + (containerMsg != null ? containerMsg : currentContainer.toString()) + "\n" + "Current window: window title " + winTitle + "( details: " + win.toString() + ")", cle);
        } else {
            // /light message
            throw new ElementNotFoundException(uiElement.toString() + " not found.\n" + "Current container name: " + (containerMsg != null ? containerMsg : currentContainer.getName()) + "\n" + "Current window: window title " + winTitle, cle);
        }
    }
}
Also used : Window(java.awt.Window) Frame(java.awt.Frame) JInternalFrame(javax.swing.JInternalFrame) SwingDriverInternal(com.axway.ats.uiengine.internal.driver.SwingDriverInternal) ElementNotFoundException(com.axway.ats.uiengine.exceptions.ElementNotFoundException) ComponentLookupException(org.fest.swing.exception.ComponentLookupException) MoreThanOneSuchElementException(com.axway.ats.uiengine.exceptions.MoreThanOneSuchElementException) Container(java.awt.Container) Dialog(java.awt.Dialog)

Aggregations

ElementNotFoundException (com.axway.ats.uiengine.exceptions.ElementNotFoundException)1 MoreThanOneSuchElementException (com.axway.ats.uiengine.exceptions.MoreThanOneSuchElementException)1 SwingDriverInternal (com.axway.ats.uiengine.internal.driver.SwingDriverInternal)1 Container (java.awt.Container)1 Dialog (java.awt.Dialog)1 Frame (java.awt.Frame)1 Window (java.awt.Window)1 JInternalFrame (javax.swing.JInternalFrame)1 ComponentLookupException (org.fest.swing.exception.ComponentLookupException)1