Search in sources :

Example 11 with JavaScriptEngine

use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.

the class WebClient method readObject.

/**
 * When we deserialize, re-initializie transient fields.
 * @param in the object input stream
 * @throws IOException if an error occurs
 * @throws ClassNotFoundException if an error occurs
 */
private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
    in.defaultReadObject();
    webConnection_ = new HttpWebConnection(this);
    scriptEngine_ = new JavaScriptEngine(this);
    jobManagers_ = Collections.synchronizedList(new ArrayList<>());
    loadQueue_ = new ArrayList<>();
    if (getBrowserVersion().hasFeature(JS_XML_SUPPORT_VIA_ACTIVEXOBJECT)) {
        initMSXMLActiveX();
    }
}
Also used : ArrayList(java.util.ArrayList) AbstractJavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.AbstractJavaScriptEngine) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Example 12 with JavaScriptEngine

use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.

the class MutationObserver method attributeReplaced.

/**
 * {@inheritDoc}
 */
@Override
public void attributeReplaced(final HtmlAttributeChangeEvent event) {
    final HtmlElement target = event.getHtmlElement();
    if (subtree_ || target == node_.getDomNodeOrDie()) {
        final String attributeName = event.getName();
        if (attributeFilter_ == null || attributeFilter_.contains(attributeName)) {
            final MutationRecord mutationRecord = new MutationRecord();
            final Scriptable scope = getParentScope();
            mutationRecord.setParentScope(scope);
            mutationRecord.setPrototype(getPrototype(mutationRecord.getClass()));
            mutationRecord.setAttributeName(attributeName);
            mutationRecord.setType("attributes");
            mutationRecord.setTarget(target.getScriptableObject());
            if (attributeOldValue_) {
                mutationRecord.setOldValue(event.getValue());
            }
            final Window window = getWindow();
            final HtmlPage owningPage = (HtmlPage) window.getDocument().getPage();
            final JavaScriptEngine jsEngine = (JavaScriptEngine) window.getWebWindow().getWebClient().getJavaScriptEngine();
            jsEngine.addPostponedAction(new PostponedAction(owningPage, "MutationObserver.attributeReplaced") {

                @Override
                public void execute() throws Exception {
                    final NativeArray array = new NativeArray(new Object[] { mutationRecord });
                    ScriptRuntime.setBuiltinProtoAndParent(array, scope, TopLevel.Builtins.Array);
                    jsEngine.callFunction(owningPage, function_, scope, MutationObserver.this, new Object[] { array });
                }
            });
        }
    }
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) NativeArray(net.sourceforge.htmlunit.corejs.javascript.NativeArray) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) PostponedAction(com.gargoylesoftware.htmlunit.javascript.PostponedAction) NativeObject(net.sourceforge.htmlunit.corejs.javascript.NativeObject) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Example 13 with JavaScriptEngine

use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.

the class WebDriverTestCase method releaseResources.

/**
 * Release resources but DON'T close the browser if we are running with a real browser.
 * Note that HtmlUnitDriver is not cached by default, but that can be configured by {@link #isWebClientCached()}.
 */
@After
@Override
public void releaseResources() {
    final List<String> unhandledAlerts = new ArrayList<>();
    if (webDriver_ != null) {
        UnhandledAlertException ex = null;
        do {
            ex = null;
            try {
                // getTitle will do an implicit check for open alerts
                webDriver_.getTitle();
            } catch (final NoSuchWindowException e) {
            // ignore
            } catch (final UnhandledAlertException e) {
                ex = e;
                unhandledAlerts.add(e.getMessage());
            }
        } while (ex != null);
    }
    super.releaseResources();
    if (!isWebClientCached()) {
        boolean rhino = false;
        if (webDriver_ != null) {
            try {
                rhino = getWebWindowOf(webDriver_).getWebClient().getJavaScriptEngine() instanceof JavaScriptEngine;
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
            webDriver_.quit();
            webDriver_ = null;
        }
        if (rhino) {
            assertTrue("There are still JS threads running after the test", getJavaScriptThreads().isEmpty());
        }
    }
    if (useRealBrowser()) {
        synchronized (WEB_DRIVERS_REAL_BROWSERS) {
            final WebDriver driver = WEB_DRIVERS_REAL_BROWSERS.get(getBrowserVersion());
            if (driver != null) {
                try {
                    final String currentWindow = driver.getWindowHandle();
                    final Set<String> handles = driver.getWindowHandles();
                    // close all windows except the current one
                    handles.remove(currentWindow);
                    if (handles.size() > 0) {
                        for (final String handle : handles) {
                            try {
                                driver.switchTo().window(handle);
                                driver.close();
                            } catch (final NoSuchWindowException e) {
                                LOG.error("Error switching to browser window; quit browser.", e);
                                WEB_DRIVERS_REAL_BROWSERS.remove(getBrowserVersion());
                                WEB_DRIVERS_REAL_BROWSERS_USAGE_COUNT.remove(getBrowserVersion());
                                driver.quit();
                                return;
                            }
                        }
                        // we have to force WebDriver to treat the remaining window
                        // as the one we like to work with from now on
                        // looks like a web driver issue to me (version 2.47.2)
                        driver.switchTo().window(currentWindow);
                    }
                    driver.manage().deleteAllCookies();
                    // in the remaining window, load a blank page
                    driver.get("about:blank");
                } catch (final WebDriverException e) {
                    shutDownRealBrowsers();
                }
            }
        }
    }
    assertTrue("There are still unhandled alerts: " + String.join("; ", unhandledAlerts), unhandledAlerts.isEmpty());
}
Also used : WebDriver(org.openqa.selenium.WebDriver) UnhandledAlertException(org.openqa.selenium.UnhandledAlertException) ArrayList(java.util.ArrayList) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) ServletException(javax.servlet.ServletException) URISyntaxException(java.net.URISyntaxException) NoSuchSessionException(org.openqa.selenium.NoSuchSessionException) UnreachableBrowserException(org.openqa.selenium.remote.UnreachableBrowserException) WebDriverException(org.openqa.selenium.WebDriverException) UnhandledAlertException(org.openqa.selenium.UnhandledAlertException) NoAlertPresentException(org.openqa.selenium.NoAlertPresentException) IOException(java.io.IOException) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) WebDriverException(org.openqa.selenium.WebDriverException) After(org.junit.After)

Example 14 with JavaScriptEngine

use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.

the class HtmlUnitRegExpProxy2Test method needCustomFix.

/**
 * Tests if custom patch is still needed.
 */
@Test
public void needCustomFix() {
    final WebClient client = getWebClient();
    final ContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
    final Context ctx = cf.enterContext();
    try {
        final ScriptableObject topScope = ctx.initStandardObjects();
        topScope.put("str", topScope, str_);
        topScope.put("text", topScope, text_);
        topScope.put("expected", topScope, expected_);
        assertEquals(begin_ + end_, text_.replaceAll(str_, ""));
        try {
            ctx.evaluateString(topScope, src_, "test script", 0, null);
        } catch (final JavaScriptException e) {
            assertTrue(e.getMessage().indexOf("Expected >") == 0);
        }
    } finally {
        Context.exit();
    }
}
Also used : ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) Context(net.sourceforge.htmlunit.corejs.javascript.Context) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) WebClient(com.gargoylesoftware.htmlunit.WebClient) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JavaScriptException(net.sourceforge.htmlunit.corejs.javascript.JavaScriptException) Test(org.junit.Test)

Example 15 with JavaScriptEngine

use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.

the class WorkerJob method loadAndExecute.

void loadAndExecute(final WebClient webClient, final String url, final Context context, final boolean checkMimeType) throws IOException {
    final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage();
    final URL fullUrl = page.getFullyQualifiedUrl(url);
    final WebRequest webRequest = new WebRequest(fullUrl);
    final WebResponse response = webClient.loadWebResponse(webRequest);
    if (checkMimeType && !MimeType.isJavascriptMimeType(response.getContentType())) {
        throw Context.reportRuntimeError("NetworkError: importScripts response is not a javascript response");
    }
    final String scriptCode = response.getContentAsString();
    final JavaScriptEngine javaScriptEngine = (JavaScriptEngine) webClient.getJavaScriptEngine();
    final DedicatedWorkerGlobalScope thisScope = this;
    final ContextAction<Object> action = cx -> {
        final Script script = javaScriptEngine.compile(page, thisScope, scriptCode, fullUrl.toExternalForm(), 1);
        // script might be null here e.g. if there is a syntax error
        if (script != null) {
            return javaScriptEngine.execute(page, thisScope, script);
        }
        return null;
    };
    final ContextFactory cf = javaScriptEngine.getContextFactory();
    if (context != null) {
        action.run(context);
    } else {
        final JavaScriptJob job = new WorkerJob(cf, action, "loadAndExecute " + url);
        owningWindow_.getWebWindow().getJobManager().addJob(job, page);
    }
}
Also used : AbstractJavaScriptConfiguration(com.gargoylesoftware.htmlunit.javascript.configuration.AbstractJavaScriptConfiguration) WindowOrWorkerGlobalScopeMixin(com.gargoylesoftware.htmlunit.javascript.host.WindowOrWorkerGlobalScopeMixin) URL(java.net.URL) Script(net.sourceforge.htmlunit.corejs.javascript.Script) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) FF(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF) EDGE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE) ClassConfiguration(com.gargoylesoftware.htmlunit.javascript.configuration.ClassConfiguration) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) FF_ESR(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) MimeType(com.gargoylesoftware.htmlunit.util.MimeType) Context(net.sourceforge.htmlunit.corejs.javascript.Context) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) CHROME(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME) IOException(java.io.IOException) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) JsxClass(com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass) List(java.util.List) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) IE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE) WindowOrWorkerGlobalScope(com.gargoylesoftware.htmlunit.javascript.host.WindowOrWorkerGlobalScope) Log(org.apache.commons.logging.Log) WebClient(com.gargoylesoftware.htmlunit.WebClient) LogFactory(org.apache.commons.logging.LogFactory) Undefined(net.sourceforge.htmlunit.corejs.javascript.Undefined) Function(net.sourceforge.htmlunit.corejs.javascript.Function) JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL(com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) EventTarget(com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget) BasicJavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.BasicJavaScriptJob) Script(net.sourceforge.htmlunit.corejs.javascript.Script) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) URL(java.net.URL) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) BasicJavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.BasicJavaScriptJob) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Aggregations

JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)25 WebClient (com.gargoylesoftware.htmlunit.WebClient)12 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)12 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)10 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)10 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)9 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)9 ContextFactory (net.sourceforge.htmlunit.corejs.javascript.ContextFactory)9 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)8 IOException (java.io.IOException)8 Context (net.sourceforge.htmlunit.corejs.javascript.Context)8 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)7 ContextAction (net.sourceforge.htmlunit.corejs.javascript.ContextAction)7 JsxClass (com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass)6 EventTarget (com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget)6 List (java.util.List)6 Function (net.sourceforge.htmlunit.corejs.javascript.Function)6 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)5 PostponedAction (com.gargoylesoftware.htmlunit.javascript.PostponedAction)5 JavaScriptJob (com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob)5