use of com.gargoylesoftware.htmlunit.ScriptException in project yacy_grid_loader by yacy.
the class HtmlUnitLoader method initClient.
public static void initClient(String userAgent) {
userAgentDefault = userAgent;
if (webClient != null) {
webClient.getCache().clear();
webClient.close();
}
webClient = new WebClient(getBrowser(userAgent));
WebClientOptions options = webClient.getOptions();
options.setJavaScriptEnabled(true);
options.setCssEnabled(false);
options.setPopupBlockerEnabled(true);
options.setRedirectEnabled(true);
options.setThrowExceptionOnScriptError(false);
options.setDownloadImages(false);
options.setGeolocationEnabled(false);
options.setPrintContentOnFailingStatusCode(false);
// this might be a bit large, is regulated with throttling and client cache clear in short memory status
webClient.getCache().setMaxSize(10000);
webClient.setIncorrectnessListener(new IncorrectnessListener() {
@Override
public void notify(String arg0, Object arg1) {
}
});
webClient.setCssErrorHandler(new ErrorHandler() {
@Override
public void warning(CSSParseException exception) throws CSSException {
}
@Override
public void fatalError(CSSParseException exception) throws CSSException {
}
@Override
public void error(CSSParseException exception) throws CSSException {
}
});
webClient.setJavaScriptErrorListener(new JavaScriptErrorListener() {
@Override
public void timeoutError(HtmlPage arg0, long arg1, long arg2) {
}
@Override
public void scriptException(HtmlPage arg0, ScriptException arg1) {
}
@Override
public void malformedScriptURL(HtmlPage arg0, String arg1, MalformedURLException arg2) {
}
@Override
public void loadScriptError(HtmlPage arg0, URL arg1, Exception arg2) {
}
});
webClient.setHTMLParserListener(new HTMLParserListener() {
@Override
public void error(String message, URL url, String html, int line, int column, String key) {
}
@Override
public void warning(String message, URL url, String html, int line, int column, String key) {
}
});
}
use of com.gargoylesoftware.htmlunit.ScriptException 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 {
// Allow invoking click on non-visible elements. Prevents HtmlUnit check for currently visible element
// TODO: remove this possibility by removing UiEngineConfigurator#workWithInvisibleElements()
// 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 {
// Restore accessibility modifier
if (getElementForOperationMethod != null) {
getElementForOperationMethod.setAccessible(getElementForOperationMethodAccessible);
}
if (moveOutIfNeededMethod != null) {
moveOutIfNeededMethod.setAccessible(moveOutIfNeededMethodAccessible);
}
if (updateActiveElementMethod != null) {
updateActiveElementMethod.setAccessible(updateActiveElementMethodAccessible);
}
}
}
use of com.gargoylesoftware.htmlunit.ScriptException in project syncany by syncany.
the class OAuthTokenWebListenerTest method testMissingField.
@Test
public void testMissingField() throws Exception {
OAuthTokenWebListener twl = OAuthTokenWebListener.forMode(OAuthMode.SERVER).build();
URI baseUri = twl.start();
Future<OAuthTokenFinish> submittedToken = twl.getToken();
final URI requestUri = URI.create(baseUri.toString() + "?" + OAuthTokenExtractors.RFC_STATE_FIELD + "=1234");
new Thread(new Runnable() {
public void run() {
try {
TimeUnit.SECONDS.sleep(2);
System.out.println("Requesting: " + requestUri);
HtmlPage page = WEB_CLIENT.getPage(requestUri.toURL());
assertEquals(400, page.getWebResponse().getStatusCode());
} catch (ScriptException e) {
// ignore "Connection is not open"
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}).start();
assertNull(submittedToken.get());
}
use of com.gargoylesoftware.htmlunit.ScriptException in project syncany by syncany.
the class OAuthTokenWebListenerTest method testTokenAsQuery.
@Test
public void testTokenAsQuery() throws Exception {
OAuthTokenWebListener twl = OAuthTokenWebListener.forMode(OAuthMode.SERVER).build();
URI baseUri = twl.start();
Future<OAuthTokenFinish> submittedToken = twl.getToken();
final URI requestUri = URI.create(baseUri.toString() + "?" + OAuthTokenExtractors.RFC_CODE_FIELD + "=" + REFERENCE_TOKEN + "&" + OAuthTokenExtractors.RFC_STATE_FIELD + "=1234");
new Thread(new Runnable() {
public void run() {
try {
TimeUnit.SECONDS.sleep(2);
System.out.println("Requesting: " + requestUri);
HtmlPage page = WEB_CLIENT.getPage(requestUri.toURL());
assertEquals(200, page.getWebResponse().getStatusCode());
} catch (ScriptException e) {
// ignore "Connection is not open"
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}).start();
assertEquals(REFERENCE_TOKEN, submittedToken.get().getToken());
}
use of com.gargoylesoftware.htmlunit.ScriptException in project syncany by syncany.
the class OAuthTokenWebListenerTest method testTokenAsHash.
@Test
public void testTokenAsHash() throws Exception {
OAuthTokenWebListener twl = OAuthTokenWebListener.forMode(OAuthMode.BROWSER).build();
URI baseUri = twl.start();
Future<OAuthTokenFinish> submittedToken = twl.getToken();
final URI requestUri = URI.create(baseUri.toString() + "?a=b#" + OAuthTokenExtractors.RFC_ACCESS_TOKEN_FIELD + "=" + REFERENCE_TOKEN + "&" + OAuthTokenExtractors.RFC_STATE_FIELD + "=1234");
new Thread(new Runnable() {
public void run() {
try {
TimeUnit.SECONDS.sleep(2);
System.out.println("Requesting: " + requestUri);
HtmlPage page = WEB_CLIENT.getPage(requestUri.toURL());
assertEquals(200, page.getWebResponse().getStatusCode());
} catch (ScriptException e) {
// ignore "Connection is not open"
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}).start();
assertEquals(REFERENCE_TOKEN, submittedToken.get().getToken());
}
Aggregations