Search in sources :

Example 6 with SeleniumOperationException

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

the class HiddenHtmlMultiSelectList method setValue.

/**
     * select a value
     *
     * @param value the value to select
     */
@Override
@PublicAtsApi
public void setValue(String value) {
    new HiddenHtmlElementState(this).waitToBecomeExisting();
    HtmlUnitWebElement selectElement = HiddenHtmlElementLocator.findElement(this);
    if (selectElement.getAttribute("multiple") == null) {
        throw new SeleniumOperationException("Not a multi-select. You may only add a selection to a select that supports multiple selections. (" + this.toString() + ")");
    }
    List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
    for (WebElement el : optionElements) {
        if (el.getText().equals(value)) {
            if (!el.isSelected()) {
                ((HtmlUnitWebElement) el).click();
                UiEngineUtilities.sleep();
            }
            return;
        }
    }
    throw new SeleniumOperationException("Option with label '" + value + "' not found. (" + this.toString() + ")");
}
Also used : HiddenHtmlElementState(com.axway.ats.uiengine.utilities.hiddenbrowser.HiddenHtmlElementState) HtmlUnitWebElement(org.openqa.selenium.htmlunit.HtmlUnitWebElement) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) WebElement(org.openqa.selenium.WebElement) HtmlUnitWebElement(org.openqa.selenium.htmlunit.HtmlUnitWebElement) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 7 with SeleniumOperationException

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

the class AbstractHtmlEngine method reloadFrames.

@PublicAtsApi
public void reloadFrames() {
    // real browsers reloads the frames automatically
    if (webDriver instanceof HtmlUnitDriver) {
        Field webClientField = null;
        boolean fieldAccessibleState = false;
        try {
            // Retrieve current WebClient instance (with the current page) from the Selenium WebDriver
            TargetLocator targetLocator = webDriver.switchTo();
            webClientField = targetLocator.getClass().getDeclaringClass().getDeclaredField("webClient");
            fieldAccessibleState = webClientField.isAccessible();
            webClientField.setAccessible(true);
            WebClient webClient = (WebClient) webClientField.get(targetLocator.defaultContent());
            HtmlPage page = (HtmlPage) webClient.getCurrentWindow().getEnclosedPage();
            for (final FrameWindow frameWindow : page.getFrames()) {
                final BaseFrameElement frame = frameWindow.getFrameElement();
                // use == and not equals(...) to identify initial content (versus URL set to "about:blank")
                if (frame.getEnclosedPage().getWebResponse().getWebRequest().getUrl() == WebClient.URL_ABOUT_BLANK) {
                    String src = frame.getSrcAttribute();
                    if (src != null && !src.isEmpty()) {
                        final URL url;
                        try {
                            url = ((HtmlPage) frame.getEnclosedPage()).getFullyQualifiedUrl(src);
                        } catch (final MalformedURLException e) {
                            String message = "Invalid src attribute of " + frame.getTagName() + ": url=[" + src + "]. Ignored.";
                            final IncorrectnessListener incorrectnessListener = webClient.getIncorrectnessListener();
                            incorrectnessListener.notify(message, this);
                            return;
                        }
                        if (isAlreadyLoadedByAncestor(url, ((HtmlPage) frame.getEnclosedPage()))) {
                            String message = "Recursive src attribute of " + frame.getTagName() + ": url=[" + src + "]. Ignored.";
                            final IncorrectnessListener incorrectnessListener = webClient.getIncorrectnessListener();
                            incorrectnessListener.notify(message, this);
                            log.info("Frame already loaded: " + frame.toString());
                            return;
                        }
                        try {
                            final WebRequest request = new WebRequest(url);
                            request.setAdditionalHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9, text/*;q=0.7, */*;q=0.5");
                            if (frameWindow.getName() == null || frameWindow.getName().isEmpty()) {
                                frameWindow.setName("frame_" + page.getFrames().indexOf(frameWindow));
                            }
                            webClient.loadWebResponseInto(webClient.loadWebResponse(request), frameWindow);
                            log.info("Frame loaded: " + frame.toString());
                        } catch (IOException e) {
                            log.error("Error when getting content for " + frame.getTagName() + " with src=" + url, e);
                        }
                    }
                } else {
                    log.info("Frame already loaded: " + frame.toString());
                }
            }
        } catch (Exception e) {
            throw new SeleniumOperationException("Error retrieving internal Selenium web client", e);
        } finally {
            if (webClientField != null) {
                webClientField.setAccessible(fieldAccessibleState);
            }
        }
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) BaseFrameElement(com.gargoylesoftware.htmlunit.html.BaseFrameElement) IOException(java.io.IOException) FrameWindow(com.gargoylesoftware.htmlunit.html.FrameWindow) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) WebClient(com.gargoylesoftware.htmlunit.WebClient) IncorrectnessListener(com.gargoylesoftware.htmlunit.IncorrectnessListener) URL(java.net.URL) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver) Field(java.lang.reflect.Field) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) TargetLocator(org.openqa.selenium.WebDriver.TargetLocator) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 8 with SeleniumOperationException

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

the class RealHtmlTable method getColumnCount.

/**
     * @return how many columns this table has
     */
@PublicAtsApi
public int getColumnCount() {
    new RealHtmlElementState(this).waitToBecomeExisting();
    String css = this.getElementProperty("_css");
    try {
        if (!StringUtils.isNullOrEmpty(css)) {
            StringBuilder sb = new StringBuilder(css);
            sb.append(" tr:nth-child(1) td");
            int count = webDriver.findElements(By.cssSelector(sb.toString())).size();
            sb = new StringBuilder(css);
            sb.append(" tr:nth-child(1) th");
            count += webDriver.findElements(By.cssSelector(sb.toString())).size();
            return count;
        } else {
            // get elements matching the following xpath
            return this.webDriver.findElements(By.xpath("(" + properties.getInternalProperty(HtmlElementLocatorBuilder.PROPERTY_ELEMENT_LOCATOR) + "//tr[th or td])[1]/*")).size();
        }
    } catch (Exception e) {
        throw new SeleniumOperationException(this, "getColumnsCount", e);
    }
}
Also used : RealHtmlElementState(com.axway.ats.uiengine.utilities.realbrowser.html.RealHtmlElementState) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) VerifyNotEqualityException(com.axway.ats.uiengine.exceptions.VerifyNotEqualityException) VerifyEqualityException(com.axway.ats.uiengine.exceptions.VerifyEqualityException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 9 with SeleniumOperationException

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

the class HtmlFileBrowse method setFileInputValue.

/**
    *
    * @param webDriver {@link WebDriver} instance
    * @param value the file input value to set
    */
protected void setFileInputValue(WebDriver webDriver, String value) {
    String locator = this.getElementProperties().getInternalProperty(HtmlElementLocatorBuilder.PROPERTY_ELEMENT_LOCATOR);
    String css = this.getElementProperty("_css");
    WebElement element = null;
    if (!StringUtils.isNullOrEmpty(css)) {
        element = webDriver.findElement(By.cssSelector(css));
    } else {
        element = webDriver.findElement(By.xpath(locator));
    }
    try {
        element.sendKeys(value);
    } catch (ElementNotVisibleException enve) {
        if (!UiEngineConfigurator.getInstance().isWorkWithInvisibleElements()) {
            throw enve;
        }
        // try to make the element visible overriding some CSS properties
        // but keep in mind that it can be still invisible using another CSS and/or JavaScript techniques
        String styleAttrValue = element.getAttribute("style");
        JavascriptExecutor jsExec = (JavascriptExecutor) webDriver;
        try {
            jsExec.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "display:'block'; visibility:'visible'; top:'auto'; left:'auto'; z-index:999;" + "height:'auto'; width:'auto';");
            element.sendKeys(value);
        } finally {
            jsExec.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, styleAttrValue);
        }
    } catch (InvalidElementStateException e) {
        throw new SeleniumOperationException(e.getMessage(), e);
    }
}
Also used : JavascriptExecutor(org.openqa.selenium.JavascriptExecutor) WebElement(org.openqa.selenium.WebElement) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) InvalidElementStateException(org.openqa.selenium.InvalidElementStateException) ElementNotVisibleException(org.openqa.selenium.ElementNotVisibleException)

Example 10 with SeleniumOperationException

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

SeleniumOperationException (com.axway.ats.uiengine.exceptions.SeleniumOperationException)22 PublicAtsApi (com.axway.ats.common.PublicAtsApi)16 WebElement (org.openqa.selenium.WebElement)13 HiddenHtmlElementState (com.axway.ats.uiengine.utilities.hiddenbrowser.HiddenHtmlElementState)7 RealHtmlElementState (com.axway.ats.uiengine.utilities.realbrowser.html.RealHtmlElementState)7 HtmlUnitWebElement (org.openqa.selenium.htmlunit.HtmlUnitWebElement)6 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Select (org.openqa.selenium.support.ui.Select)4 ElementNotFoundException (com.axway.ats.uiengine.exceptions.ElementNotFoundException)3 Field (java.lang.reflect.Field)3 URL (java.net.URL)3 VerificationException (com.axway.ats.uiengine.exceptions.VerificationException)2 VerifyEqualityException (com.axway.ats.uiengine.exceptions.VerifyEqualityException)2 VerifyNotEqualityException (com.axway.ats.uiengine.exceptions.VerifyNotEqualityException)2 IncorrectnessListener (com.gargoylesoftware.htmlunit.IncorrectnessListener)2 Page (com.gargoylesoftware.htmlunit.Page)2 WebClient (com.gargoylesoftware.htmlunit.WebClient)2 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)2 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)2