Search in sources :

Example 1 with ElementNotFoundException

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

the class HiddenHtmlElementLocator method findElement.

public static HtmlUnitWebElement findElement(UiElement uiElement, String xpathSuffix, boolean verbose) {
    HiddenBrowserDriver browserDriver = (HiddenBrowserDriver) uiElement.getUiDriver();
    WebDriver webDriver = (WebDriver) browserDriver.getInternalObject(InternalObjectsEnum.WebDriver.name());
    HtmlNavigator.getInstance().navigateToFrame(webDriver, uiElement);
    String xpath = uiElement.getElementProperties().getInternalProperty(HtmlElementLocatorBuilder.PROPERTY_ELEMENT_LOCATOR);
    String css = uiElement.getElementProperty("_css");
    if (xpathSuffix != null) {
        xpath += xpathSuffix;
    }
    List<WebElement> elements = null;
    if (!StringUtils.isNullOrEmpty(css)) {
        elements = webDriver.findElements(By.cssSelector(css));
    } else {
        elements = webDriver.findElements(By.xpath(xpath));
    }
    if (elements.size() == 0) {
        throw new ElementNotFoundException(uiElement.toString() + " not found.");
    } else if (elements.size() > 1) {
        if (verbose) {
            log.warn("More than one HTML elements were found having properties " + uiElement.toString() + ".Only the first HTML element will be used.");
        }
    }
    HtmlUnitWebElement element = (HtmlUnitWebElement) elements.get(0);
    if (verbose) {
        log.info("Found element: " + element.toString());
    }
    return element;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) HiddenBrowserDriver(com.axway.ats.uiengine.HiddenBrowserDriver) HtmlUnitWebElement(org.openqa.selenium.htmlunit.HtmlUnitWebElement) ElementNotFoundException(com.axway.ats.uiengine.exceptions.ElementNotFoundException) WebElement(org.openqa.selenium.WebElement) HtmlUnitWebElement(org.openqa.selenium.htmlunit.HtmlUnitWebElement)

Example 2 with ElementNotFoundException

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

the class RealHtmlElementLocator method findElement.

public static WebElement findElement(UiElement uiElement, String xpathSuffix, boolean verbose) {
    AbstractRealBrowserDriver browserDriver = (AbstractRealBrowserDriver) uiElement.getUiDriver();
    WebDriver webDriver = (WebDriver) browserDriver.getInternalObject(InternalObjectsEnum.WebDriver.name());
    HtmlNavigator.getInstance().navigateToFrame(webDriver, uiElement);
    String xpath = uiElement.getElementProperties().getInternalProperty(HtmlElementLocatorBuilder.PROPERTY_ELEMENT_LOCATOR);
    String css = uiElement.getElementProperty("_css");
    if (xpathSuffix != null) {
        xpath += xpathSuffix;
    }
    List<WebElement> elements = null;
    if (!StringUtils.isNullOrEmpty(css)) {
        elements = webDriver.findElements(By.cssSelector(css));
    } else {
        elements = webDriver.findElements(By.xpath(xpath));
    }
    if (elements.size() == 0) {
        throw new ElementNotFoundException(uiElement.toString() + " not found.");
    } else if (elements.size() > 1) {
        if (verbose) {
            log.warn("More than one HTML elements were found having properties " + uiElement.toString() + ".Only the first HTML element will be used.");
        }
    }
    WebElement element = (WebElement) elements.get(0);
    if (verbose) {
        log.debug("Found element: " + element.toString());
    }
    return element;
}
Also used : WebDriver(org.openqa.selenium.WebDriver) AbstractRealBrowserDriver(com.axway.ats.uiengine.AbstractRealBrowserDriver) ElementNotFoundException(com.axway.ats.uiengine.exceptions.ElementNotFoundException) WebElement(org.openqa.selenium.WebElement)

Example 3 with ElementNotFoundException

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

the class SwingElementLocator method getContainerFixture.

/**
     * Change container by specified name or title.
     * For internal use
     * @param driver
     * @param containerProperties property with name inside
     * @return the {@link ContainerFinxture}
     */
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 = null;
    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 4 with ElementNotFoundException

use of com.axway.ats.uiengine.exceptions.ElementNotFoundException 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 = null;
    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)

Example 5 with ElementNotFoundException

use of com.axway.ats.uiengine.exceptions.ElementNotFoundException 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)

Aggregations

ElementNotFoundException (com.axway.ats.uiengine.exceptions.ElementNotFoundException)8 Dialog (java.awt.Dialog)3 Frame (java.awt.Frame)3 JInternalFrame (javax.swing.JInternalFrame)3 WaitTimedOutError (org.fest.swing.exception.WaitTimedOutError)3 SwingDriverInternal (com.axway.ats.uiengine.internal.driver.SwingDriverInternal)2 Container (java.awt.Container)2 BasicRobot (org.fest.swing.core.BasicRobot)2 ComponentLookupException (org.fest.swing.exception.ComponentLookupException)2 WebDriver (org.openqa.selenium.WebDriver)2 WebElement (org.openqa.selenium.WebElement)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 AbstractRealBrowserDriver (com.axway.ats.uiengine.AbstractRealBrowserDriver)1 HiddenBrowserDriver (com.axway.ats.uiengine.HiddenBrowserDriver)1 HtmlElement (com.axway.ats.uiengine.elements.html.HtmlElement)1 MoreThanOneSuchElementException (com.axway.ats.uiengine.exceptions.MoreThanOneSuchElementException)1 SeleniumOperationException (com.axway.ats.uiengine.exceptions.SeleniumOperationException)1 Page (com.gargoylesoftware.htmlunit.Page)1 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)1 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)1