Search in sources :

Example 1 with HtmlUnitKeyboard

use of org.openqa.selenium.htmlunit.HtmlUnitKeyboard in project ats-framework by Axway.

the class HiddenHtmlElementUtils method mouseClick.

@PublicAtsApi
public static void mouseClick(HtmlUnitWebElement webElement) {
    Method getElementForOperationMethod = null;
    boolean getElementForOperationMethodAccessible = false;
    Method moveOutIfNeededMethod = null;
    boolean moveOutIfNeededMethodAccessible = false;
    Method updateActiveElementMethod = null;
    boolean updateActiveElementMethodAccessible = false;
    try {
        // change access modifiers of some methods
        getElementForOperationMethod = HtmlUnitMouse.class.getDeclaredMethod("getElementForOperation", Coordinates.class);
        getElementForOperationMethodAccessible = getElementForOperationMethod.isAccessible();
        getElementForOperationMethod.setAccessible(true);
        moveOutIfNeededMethod = HtmlUnitMouse.class.getDeclaredMethod("moveOutIfNeeded", DomElement.class);
        moveOutIfNeededMethodAccessible = moveOutIfNeededMethod.isAccessible();
        moveOutIfNeededMethod.setAccessible(true);
        updateActiveElementMethod = HtmlUnitMouse.class.getDeclaredMethod("updateActiveElement", DomElement.class);
        updateActiveElementMethodAccessible = updateActiveElementMethod.isAccessible();
        updateActiveElementMethod.setAccessible(true);
        // get the target element
        HtmlUnitDriver htmlUnitDriver = (HtmlUnitDriver) webElement.getWrappedDriver();
        HtmlUnitMouse mouse = (HtmlUnitMouse) htmlUnitDriver.getMouse();
        HtmlElement element = (HtmlElement) getElementForOperationMethod.invoke(mouse, webElement.getCoordinates());
        moveOutIfNeededMethod.invoke(mouse, element);
        if (htmlUnitDriver.isJavascriptEnabled()) {
            if (!(element instanceof HtmlInput)) {
                element.focus();
            }
            element.mouseOver();
            element.mouseMove();
        }
        HtmlUnitKeyboard keyboard = (HtmlUnitKeyboard) htmlUnitDriver.getKeyboard();
        element.click(keyboard.isShiftPressed(), keyboard.isCtrlPressed(), keyboard.isAltPressed());
        updateActiveElementMethod.invoke(mouse, element);
    } catch (IOException ioe) {
        throw new WebDriverException(ioe);
    } catch (ScriptException e) {
        // we need only our exception if such exists
        Throwable uiEngineException = e.getCause();
        while (uiEngineException != null && !uiEngineException.getClass().getName().toLowerCase().contains("com.axway.ats")) {
            uiEngineException = uiEngineException.getCause();
        }
        if (uiEngineException != null) {
            throw (RuntimeException) uiEngineException;
        }
        // Log the exception with level WARN, because in the main Selenium implementation
        // (HtmlUnitMouse.click(coordinates)) the exception is even skipped
        log.warn("Script error while clicking web element. " + webElement.toString(), e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (getElementForOperationMethod != null) {
            getElementForOperationMethod.setAccessible(getElementForOperationMethodAccessible);
        }
        if (moveOutIfNeededMethod != null) {
            moveOutIfNeededMethod.setAccessible(moveOutIfNeededMethodAccessible);
        }
        if (updateActiveElementMethod != null) {
            updateActiveElementMethod.setAccessible(updateActiveElementMethodAccessible);
        }
    }
}
Also used : HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) Coordinates(org.openqa.selenium.interactions.internal.Coordinates) Method(java.lang.reflect.Method) IOException(java.io.IOException) HtmlInput(com.gargoylesoftware.htmlunit.html.HtmlInput) WebDriverException(org.openqa.selenium.WebDriverException) IOException(java.io.IOException) ScriptException(com.gargoylesoftware.htmlunit.ScriptException) HtmlUnitDriver(org.openqa.selenium.htmlunit.HtmlUnitDriver) ScriptException(com.gargoylesoftware.htmlunit.ScriptException) DomElement(com.gargoylesoftware.htmlunit.html.DomElement) HtmlUnitMouse(org.openqa.selenium.htmlunit.HtmlUnitMouse) HtmlUnitKeyboard(org.openqa.selenium.htmlunit.HtmlUnitKeyboard) WebDriverException(org.openqa.selenium.WebDriverException) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Aggregations

PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 ScriptException (com.gargoylesoftware.htmlunit.ScriptException)1 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)1 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)1 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)1 IOException (java.io.IOException)1 Method (java.lang.reflect.Method)1 WebDriverException (org.openqa.selenium.WebDriverException)1 HtmlUnitDriver (org.openqa.selenium.htmlunit.HtmlUnitDriver)1 HtmlUnitKeyboard (org.openqa.selenium.htmlunit.HtmlUnitKeyboard)1 HtmlUnitMouse (org.openqa.selenium.htmlunit.HtmlUnitMouse)1 Coordinates (org.openqa.selenium.interactions.internal.Coordinates)1