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);
}
}
}
Aggregations