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