Search in sources :

Example 1 with ScriptException

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) {
        }
    });
}
Also used : HTMLParserListener(com.gargoylesoftware.htmlunit.html.HTMLParserListener) ErrorHandler(org.w3c.css.sac.ErrorHandler) WebClientOptions(com.gargoylesoftware.htmlunit.WebClientOptions) MalformedURLException(java.net.MalformedURLException) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebClient(com.gargoylesoftware.htmlunit.WebClient) IncorrectnessListener(com.gargoylesoftware.htmlunit.IncorrectnessListener) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) CSSException(org.w3c.css.sac.CSSException) CSSParseException(org.w3c.css.sac.CSSParseException) IOException(java.io.IOException) ScriptException(com.gargoylesoftware.htmlunit.ScriptException) JavaScriptErrorListener(com.gargoylesoftware.htmlunit.javascript.JavaScriptErrorListener) ScriptException(com.gargoylesoftware.htmlunit.ScriptException) CSSParseException(org.w3c.css.sac.CSSParseException) CSSException(org.w3c.css.sac.CSSException)

Example 2 with ScriptException

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);
        }
    }
}
Also used : HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) Coordinates(org.openqa.selenium.interactions.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)

Example 3 with ScriptException

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());
}
Also used : ScriptException(com.gargoylesoftware.htmlunit.ScriptException) OAuthTokenFinish(org.syncany.plugins.transfer.oauth.OAuthTokenFinish) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) OAuthTokenWebListener(org.syncany.plugins.transfer.oauth.OAuthTokenWebListener) URI(java.net.URI) StorageException(org.syncany.plugins.transfer.StorageException) ScriptException(com.gargoylesoftware.htmlunit.ScriptException) Test(org.junit.Test)

Example 4 with ScriptException

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());
}
Also used : ScriptException(com.gargoylesoftware.htmlunit.ScriptException) OAuthTokenFinish(org.syncany.plugins.transfer.oauth.OAuthTokenFinish) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) OAuthTokenWebListener(org.syncany.plugins.transfer.oauth.OAuthTokenWebListener) URI(java.net.URI) StorageException(org.syncany.plugins.transfer.StorageException) ScriptException(com.gargoylesoftware.htmlunit.ScriptException) Test(org.junit.Test)

Example 5 with ScriptException

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());
}
Also used : ScriptException(com.gargoylesoftware.htmlunit.ScriptException) OAuthTokenFinish(org.syncany.plugins.transfer.oauth.OAuthTokenFinish) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) OAuthTokenWebListener(org.syncany.plugins.transfer.oauth.OAuthTokenWebListener) URI(java.net.URI) StorageException(org.syncany.plugins.transfer.StorageException) ScriptException(com.gargoylesoftware.htmlunit.ScriptException) Test(org.junit.Test)

Aggregations

ScriptException (com.gargoylesoftware.htmlunit.ScriptException)5 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)4 URI (java.net.URI)3 Test (org.junit.Test)3 StorageException (org.syncany.plugins.transfer.StorageException)3 OAuthTokenFinish (org.syncany.plugins.transfer.oauth.OAuthTokenFinish)3 OAuthTokenWebListener (org.syncany.plugins.transfer.oauth.OAuthTokenWebListener)3 IOException (java.io.IOException)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 IncorrectnessListener (com.gargoylesoftware.htmlunit.IncorrectnessListener)1 WebClient (com.gargoylesoftware.htmlunit.WebClient)1 WebClientOptions (com.gargoylesoftware.htmlunit.WebClientOptions)1 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)1 HTMLParserListener (com.gargoylesoftware.htmlunit.html.HTMLParserListener)1 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)1 HtmlInput (com.gargoylesoftware.htmlunit.html.HtmlInput)1 JavaScriptErrorListener (com.gargoylesoftware.htmlunit.javascript.JavaScriptErrorListener)1 Method (java.lang.reflect.Method)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1