Search in sources :

Example 6 with ElementNotFoundException

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

the class SwingElementLocator method getWindowFixture.

/**
     * Change window by specified name.
     * For internal use
     * @param driver Swing driver
     * @param windowTitle if null look for any visible window
     * @param isDialog
     * @return the {@link ContainerFinxture}
     */
public static WindowFixture<?> getWindowFixture(SwingDriverInternal driver, final String windowTitle, boolean isDialog) throws ElementNotFoundException {
    WindowFixture<?> windowFixture = driver.getWindowFixture();
    Robot robot = null;
    if (windowFixture != null) {
        // use the current robot instance
        robot = windowFixture.robot;
    } else {
        robot = BasicRobot.robotWithCurrentAwtHierarchy();
    }
    try {
        if (windowTitle != null) {
            if (isDialog) {
                windowFixture = WindowFinder.findDialog(new GenericTypeMatcher<Dialog>(Dialog.class) {

                    protected boolean isMatching(Dialog dialog) {
                        return windowTitle.equals(dialog.getTitle()) && dialog.isShowing();
                    }
                }).withTimeout(UiEngineConfigurator.getInstance().getElementStateChangeDelay()).using(robot);
            } else {
                windowFixture = WindowFinder.findFrame(new GenericTypeMatcher<Frame>(Frame.class) {

                    protected boolean isMatching(Frame frame) {
                        return windowTitle.equals(frame.getTitle()) && frame.isShowing();
                    }
                }).withTimeout(UiEngineConfigurator.getInstance().getElementStateChangeDelay()).using(robot);
            }
        } else {
            if (isDialog) {
                windowFixture = WindowFinder.findDialog(new GenericTypeMatcher<Dialog>(Dialog.class) {

                    protected boolean isMatching(Dialog dialog) {
                        return dialog.isShowing();
                    }
                }).withTimeout(UiEngineConfigurator.getInstance().getElementStateChangeDelay()).using(robot);
            } else {
                windowFixture = WindowFinder.findFrame(new GenericTypeMatcher<Frame>(Frame.class) {

                    protected boolean isMatching(Frame frame) {
                        if (log.isTraceEnabled()) {
                            log.trace("WindowFinder isMatching(): Title: " + frame.getTitle() + ", Frame + " + frame + ", Owner: " + frame.getOwner());
                        }
                        // owner == null - top frame. Two independent frames are both considered a top ones
                        return frame.isShowing() && frame.getOwner() == null;
                    }
                }).withTimeout(UiEngineConfigurator.getInstance().getElementStateChangeDelay()).using(robot);
            }
        }
        return windowFixture;
    } catch (WaitTimedOutError wtoe) {
        throw new ElementNotFoundException("Unable to find " + (isDialog ? "dialog" : "frame") + (windowTitle != null ? " with title '" + windowTitle + "'" : " without title specified (null passed)"), wtoe);
    }
}
Also used : Frame(java.awt.Frame) JInternalFrame(javax.swing.JInternalFrame) Dialog(java.awt.Dialog) ElementNotFoundException(com.axway.ats.uiengine.exceptions.ElementNotFoundException) WaitTimedOutError(org.fest.swing.exception.WaitTimedOutError) Robot(org.fest.swing.core.Robot) BasicRobot(org.fest.swing.core.BasicRobot) GenericTypeMatcher(org.fest.swing.core.GenericTypeMatcher)

Example 7 with ElementNotFoundException

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

the class HtmlNavigator method navigateToFrame.

@PublicAtsApi
public void navigateToFrame(WebDriver webDriver, UiElement element) {
    if (lastWebDriver != webDriver) {
        // this is a new WebDriver instance
        lastWebDriver = webDriver;
        lastFramesLocation = "";
    }
    String newFramesLocationProperty = element != null ? element.getElementProperty("frame") : null;
    try {
        if (newFramesLocationProperty == null) {
            // No frame selection. Go to top frame if not there yet
            if (!"".equals(lastFramesLocation)) {
                log.debug("Go to TOP frame");
                webDriver.switchTo().defaultContent();
                lastFramesLocation = "";
            }
        } else {
            lastFramesLocation = newFramesLocationProperty;
            log.debug("Go to frame: " + newFramesLocationProperty);
            String[] newFramesLocation = newFramesLocationProperty.split("\\->");
            webDriver.switchTo().defaultContent();
            for (String frame : newFramesLocation) {
                if (frame.startsWith("/") || frame.startsWith("(/")) {
                    WebElement frameElement = webDriver.findElement(By.xpath(frame.trim()));
                    webDriver.switchTo().frame(frameElement);
                } else {
                    webDriver.switchTo().frame(frame.trim());
                }
            }
        }
    } catch (NotFoundException nfe) {
        String msg = "Frame not found. Searched by: '" + (element != null ? element.getElementProperty("frame") : "") + "'";
        log.debug(msg);
        throw new ElementNotFoundException(msg, nfe);
    }
}
Also used : NotFoundException(org.openqa.selenium.NotFoundException) ElementNotFoundException(com.axway.ats.uiengine.exceptions.ElementNotFoundException) ElementNotFoundException(com.axway.ats.uiengine.exceptions.ElementNotFoundException) WebElement(org.openqa.selenium.WebElement) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 8 with ElementNotFoundException

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

the class HiddenHtmlElement method clickAndDownloadFile.

/**
     * Click the element and download file
     */
protected void clickAndDownloadFile() {
    WebWindow currentWindow = null;
    Field currentWindowField = null;
    boolean fieldAccessibleState = false;
    try {
        currentWindowField = htmlUnitDriver.getClass().getDeclaredField("currentWindow");
        fieldAccessibleState = currentWindowField.isAccessible();
        currentWindowField.setAccessible(true);
        currentWindow = (WebWindow) currentWindowField.get(htmlUnitDriver);
    } catch (Exception e) {
        throw new SeleniumOperationException("Error retrieving internal Selenium web client", e);
    } finally {
        if (currentWindowField != null) {
            currentWindowField.setAccessible(fieldAccessibleState);
        }
    }
    String elementXPath = properties.getProperty("xpath");
    // find element and download the file
    HtmlPage page = (HtmlPage) currentWindow.getEnclosedPage();
    List<?> foundElementsList = page.getByXPath(elementXPath);
    if (foundElementsList != null && !foundElementsList.isEmpty()) {
        InputStream in = null;
        FileOutputStream fos = null;
        try {
            com.gargoylesoftware.htmlunit.html.HtmlElement element = (com.gargoylesoftware.htmlunit.html.HtmlElement) foundElementsList.get(0);
            // Use generic Page. Exact page type returned depends on the MIME type set in response header
            Page result = element.click();
            String fileName = null;
            String contentDisposition = result.getWebResponse().getResponseHeaderValue("Content-Disposition");
            if (contentDisposition != null) {
                Matcher m = contentDispositionPattern.matcher(contentDisposition);
                if (m.matches()) {
                    fileName = m.group(1);
                    log.debug("Download file name extracted from the 'Content-Disposition' header is " + fileName);
                }
            }
            if (fileName == null) {
                String url = result.getWebResponse().getWebRequest().getUrl().getFile().trim();
                Matcher m = urlFileNamePattern.matcher(url);
                if (m.matches()) {
                    fileName = m.group(1);
                    log.debug("Download file name extracted from the request URL is " + fileName);
                } else {
                    fileName = String.valueOf(new Date().getTime()) + ".bin";
                    log.debug("Downloaded file name constructed the current timestamp is " + fileName);
                }
            }
            in = result.getWebResponse().getContentAsStream();
            String fileAbsPath = UiEngineConfigurator.getInstance().getBrowserDownloadDir() + fileName;
            fos = new FileOutputStream(new File(fileAbsPath), false);
            byte[] buff = new byte[BUFFER_LENGTH];
            int len;
            while ((len = in.read(buff)) != -1) {
                fos.write(buff, 0, len);
            }
            fos.flush();
            log.info("Downloaded file: " + fileAbsPath);
        } catch (IOException e) {
            throw new SeleniumOperationException("Error downloading file", e);
        } finally {
            IoUtils.closeStream(fos);
            IoUtils.closeStream(in);
        }
    } else {
        throw new ElementNotFoundException("Can't find element by XPath: " + elementXPath);
    }
}
Also used : HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Matcher(java.util.regex.Matcher) InputStream(java.io.InputStream) HtmlElement(com.axway.ats.uiengine.elements.html.HtmlElement) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) Page(com.gargoylesoftware.htmlunit.Page) ElementNotFoundException(com.axway.ats.uiengine.exceptions.ElementNotFoundException) IOException(java.io.IOException) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) ElementNotFoundException(com.axway.ats.uiengine.exceptions.ElementNotFoundException) IOException(java.io.IOException) Date(java.util.Date) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Field(java.lang.reflect.Field) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

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