Search in sources :

Example 16 with SeleniumOperationException

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

the class RealHtmlMultiSelectList method unsetValue.

/**
     * unselect a value
     *
     * @param value the value to unselect
     */
@Override
@PublicAtsApi
public void unsetValue(String value) {
    new RealHtmlElementState(this).waitToBecomeExisting();
    WebElement element = RealHtmlElementLocator.findElement(this);
    Select select = new Select(element);
    // select.deselectByVisibleText( value ); // this method doesn't throw an exception if the option doesn't exist
    for (WebElement option : select.getOptions()) {
        if (option.getText().equals(value)) {
            if (option.isSelected()) {
                option.click();
                UiEngineUtilities.sleep();
            }
            return;
        }
    }
    throw new SeleniumOperationException("Option with label '" + value + "' not found. (" + this.toString() + ")");
}
Also used : RealHtmlElementState(com.axway.ats.uiengine.utilities.realbrowser.html.RealHtmlElementState) Select(org.openqa.selenium.support.ui.Select) WebElement(org.openqa.selenium.WebElement) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 17 with SeleniumOperationException

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

the class RealHtmlMultiSelectList method setValue.

/**
     * select a value
     *
     * @param value the value to select
     */
@Override
@PublicAtsApi
public void setValue(String value) {
    new RealHtmlElementState(this).waitToBecomeExisting();
    try {
        WebElement element = RealHtmlElementLocator.findElement(this);
        Select select = new Select(element);
        select.selectByVisibleText(value);
    } catch (NoSuchElementException nsee) {
        throw new SeleniumOperationException("Option with label '" + value + "' not found. (" + this.toString() + ")");
    }
    UiEngineUtilities.sleep();
}
Also used : RealHtmlElementState(com.axway.ats.uiengine.utilities.realbrowser.html.RealHtmlElementState) Select(org.openqa.selenium.support.ui.Select) WebElement(org.openqa.selenium.WebElement) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) NoSuchElementException(org.openqa.selenium.NoSuchElementException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 18 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 19 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)

Example 20 with SeleniumOperationException

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

the class HiddenHtmlMultiSelectList method getValues.

/**
     * @return the selected value
     */
@Override
@PublicAtsApi
public String[] getValues() {
    new HiddenHtmlElementState(this).waitToBecomeExisting();
    HtmlUnitWebElement selectElement = HiddenHtmlElementLocator.findElement(this);
    List<String> values = new ArrayList<String>();
    List<WebElement> optionElements = selectElement.findElements(By.tagName("option"));
    for (WebElement element : optionElements) {
        if (element.isSelected()) {
            values.add(element.getText());
        }
    }
    if (values.isEmpty()) {
        throw new SeleniumOperationException("There is no selected 'option' in " + this.toString());
    }
    return values.toArray(new String[0]);
}
Also used : HiddenHtmlElementState(com.axway.ats.uiengine.utilities.hiddenbrowser.HiddenHtmlElementState) HtmlUnitWebElement(org.openqa.selenium.htmlunit.HtmlUnitWebElement) ArrayList(java.util.ArrayList) WebElement(org.openqa.selenium.WebElement) HtmlUnitWebElement(org.openqa.selenium.htmlunit.HtmlUnitWebElement) SeleniumOperationException(com.axway.ats.uiengine.exceptions.SeleniumOperationException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

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