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