/** * Test that a JS job using setTimeout is processed on GAE. * * @throws IOException if fails to get page. * @throws FailingHttpStatusCodeException if fails to get page. */ @Test public void setTimeout() throws FailingHttpStatusCodeException, IOException { final String html = "<html>\n" + " <body>\n" + " <script>\n" + " setTimeout(\"alert('hello')\", 0);" + " setTimeout(\"alert('hello again')\", 200);" + " </script>\n" + " </body>\n" + "</html>"; final WebClient client = new WebClient(); final List<String> collectedAlerts = new ArrayList<String>(); client.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); final MockWebConnection conn = new MockWebConnection(); conn.setDefaultResponse(html); client.setWebConnection(conn); client.getPage(FIRST_URL); int executedJobs = client.getJavaScriptEngine().pumpEventLoop(20); assertEquals(Arrays.asList("hello"), collectedAlerts); assertEquals(1, executedJobs); executedJobs = client.getJavaScriptEngine().pumpEventLoop(100); assertEquals(Arrays.asList("hello"), collectedAlerts); assertEquals(0, executedJobs); executedJobs = client.getJavaScriptEngine().pumpEventLoop(200); assertEquals(Arrays.asList("hello", "hello again"), collectedAlerts); assertEquals(1, executedJobs); }
/** * Test that a JS job using setInterval is processed on GAE. * * @throws IOException if fails to get page. * @throws FailingHttpStatusCodeException if fails to get page. */ @Test public void setInterval() throws FailingHttpStatusCodeException, IOException { final String html = "<html>\n" + " <body>\n" + " <script>\n" + " setInterval(\"alert('hello')\", 100);" + " </script>\n" + " </body>\n" + "</html>"; final WebClient client = new WebClient(); final List<String> collectedAlerts = new ArrayList<String>(); client.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); final MockWebConnection conn = new MockWebConnection(); conn.setDefaultResponse(html); client.setWebConnection(conn); client.getPage(FIRST_URL); assertEquals(0, collectedAlerts.size()); // pump but not long enough int executedJobs = client.getJavaScriptEngine().pumpEventLoop(50); assertEquals(0, collectedAlerts.size()); // pump a bit more executedJobs = client.getJavaScriptEngine().pumpEventLoop(100); assertEquals(Arrays.asList("hello"), collectedAlerts); assertEquals(1, executedJobs); // pump even more executedJobs = client.getJavaScriptEngine().pumpEventLoop(250); assertEquals(Arrays.asList("hello", "hello", "hello"), collectedAlerts); assertEquals(2, executedJobs); }
/** * Attaches a visual (GUI) debugger to the specified client. * * @param client the client to which the visual debugger is to be attached * @see <a href="http://www.mozilla.org/rhino/debugger.html">Mozilla Rhino Debugger * Documentation</a> */ public static void attachVisualDebugger(final WebClient client) { final ScopeProvider sp = null; final HtmlUnitContextFactory cf = client.getJavaScriptEngine().getContextFactory(); final Main main = Main.mainEmbedded(cf, sp, "HtmlUnit JavaScript Debugger"); final SourceProvider sourceProvider = new SourceProvider() { public String getSource(final DebuggableScript script) { String sourceName = script.getSourceName(); if (sourceName.endsWith("(eval)")) { return null; // script is result of eval call. Rhino already knows the source and we // don't } if (sourceName.startsWith("script in ")) { sourceName = StringUtils.substringBetween(sourceName, "script in ", " from"); for (final WebWindow ww : client.getWebWindows()) { final WebResponse wr = ww.getEnclosedPage().getWebResponse(); if (sourceName.equals(wr.getWebRequest().getUrl().toString())) { return wr.getContentAsString(); } } } return null; } }; main.setSourceProvider(sourceProvider); }
/** * Tests asynchronous use of XMLHttpRequest, using Mozilla style object creation. * * @throws Exception if the test fails */ @Test public void testAsyncUse() throws Exception { final URL secondUrl = new URL(FIRST_URL, "/second/"); final String html = "<html>\n" + " <head>\n" + " <title>XMLHttpRequest Test</title>\n" + " <script>\n" + " var request;\n" + " function testAsync() {\n" + " if (window.XMLHttpRequest)\n" + " request = new XMLHttpRequest();\n" + " else if (window.ActiveXObject)\n" + " request = new ActiveXObject('Microsoft.XMLHTTP');\n" + " request.onreadystatechange = onReadyStateChange;\n" + " alert(request.readyState);\n" + " request.open('GET', '/second/', true);\n" + " request.send('');\n" + " }\n" + " function onReadyStateChange() {\n" + " alert(request.readyState);\n" + " if (request.readyState == 4)\n" + " alert(request.responseText);\n" + " }\n" + " </script>\n" + " </head>\n" + " <body onload='testAsync()'>\n" + " </body>\n" + "</html>"; final String xml = "<xml2>\n" + "<content2>sdgxsdgx</content2>\n" + "<content2>sdgxsdgx2</content2>\n" + "</xml2>"; final WebClient client = new WebClient(); final List<String> collectedAlerts = new ArrayList<String>(); client.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); final MockWebConnection conn = new MockWebConnection(); conn.setResponse(FIRST_URL, html); conn.setResponse(secondUrl, xml, "text/xml"); client.setWebConnection(conn); client.getPage(FIRST_URL); final int executedJobs = client.getJavaScriptEngine().pumpEventLoop(1000); final String[] alerts = { String.valueOf(XMLHttpRequest.STATE_UNINITIALIZED), String.valueOf(XMLHttpRequest.STATE_LOADING), String.valueOf(XMLHttpRequest.STATE_LOADING), String.valueOf(XMLHttpRequest.STATE_LOADED), String.valueOf(XMLHttpRequest.STATE_INTERACTIVE), String.valueOf(XMLHttpRequest.STATE_COMPLETED), xml }; assertEquals(Arrays.asList(alerts).toString(), collectedAlerts.toString()); assertEquals(1, executedJobs); }