public String getParsedPage() { List<String> alertHandler = new LinkedList<String>(); ; WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24); // CHROME); webClient.setAjaxController(new MyNicelyResynchronizingAjaxController()); webClient.getOptions().setJavaScriptEnabled(true); webClient.getOptions().setTimeout(3500); webClient.getOptions().setThrowExceptionOnScriptError(true); webClient.getOptions().setCssEnabled(true); webClient.getOptions().isRedirectEnabled(); webClient.setAlertHandler( new CollectingAlertHandler(alertHandler)); // 将JavaScript中alert标签产生的数据保存在一个链表中 // webClient.getOptions().setThrowExceptionOnScriptError(false); HtmlPage page = null; JavaScriptEngine engine = new JavaScriptEngine(webClient); webClient.setJavaScriptEngine(engine); try { page = webClient.getPage(data); } catch (FailingHttpStatusCodeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } if (page != null) { return page.asXml(); } return null; }
/** * get the ajax url from the click button * * @param clickOfXpath:页面待点击按钮的xpath表达式 * @param index * @return List<String>:链表的第一个信息是页面的title,以后的信息是所有的ajax的url */ public static List<String> getAjaxUrl(String targetUrl, String clickOfXpath, int index) throws FailingHttpStatusCodeException, MalformedURLException, IOException { // TARGET_URL = // "http://app.flyme.cn/apps/public/detail?package_name=com.myzaker.zaker_phone_smartbar"; List<String> urls = new LinkedList<String>(); // 每次ajax请求时都会创建一个AjaxController对象,在该对象中可以查看ajax请求的地址 MyNicelyResynchronizingAjaxController ajaxController = new MyNicelyResynchronizingAjaxController(); List alertHandler = new LinkedList(); // 模拟一个浏览器 WebClient webClient = new WebClient(BrowserVersion.FIREFOX_24); // HtmlUnitDriver // 设置webClient的相关参数 webClient.getOptions().setJavaScriptEnabled(true); webClient.getOptions().setCssEnabled(false); webClient.setAjaxController(ajaxController); webClient.getOptions().setTimeout(35000); webClient.getOptions().setThrowExceptionOnScriptError(false); webClient.setAlertHandler( new CollectingAlertHandler(alertHandler)); // 将JavaScript中alert标签产生的数据保存在一个链表中 // 模拟浏览器打开一个目标网址 HtmlPage rootPage = webClient.getPage(targetUrl); urls.add(rootPage.getTitleText()); urls.add(ajaxController.getVisitUrl()); // System.out.println("url1:" + url); HtmlElement elementA = (HtmlElement) rootPage.getByXPath(clickOfXpath).get(index); Page page = elementA.click(); urls.add(ajaxController.getVisitUrl()); return urls; }
/** * 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); }
/** * 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); }
private void testEventBubblingReturns( final String onclick1, final String onclick2, final String onclick3, final boolean changesPage) throws Exception { final String html1 = "<html><head><title>First</title></head><body>\n" + "<div onclick='alert(\"d\"); " + onclick1 + "'>\n" + "<span onclick='alert(\"s\"); " + onclick2 + "'>\n" + "<a href='" + URL_SECOND + "' id='a' onclick='alert(\"a\"); " + onclick3 + "'>go</a>\n" + "</span>\n" + "</div>\n" + "</body></html>"; final String html2 = "<html><head><title>Second</title></head><body></body></html>"; final WebClient client = getWebClient(); final List<String> collectedAlerts = new ArrayList<String>(); client.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); final MockWebConnection webConnection = new MockWebConnection(); webConnection.setResponse(URL_FIRST, html1); webConnection.setResponse(URL_SECOND, html2); client.setWebConnection(webConnection); final HtmlPage page = client.getPage(URL_FIRST); final HtmlAnchor anchor = page.getHtmlElementById("a"); final HtmlPage secondPage = anchor.click(); assertEquals(new String[] {"a", "s", "d"}, collectedAlerts); if (changesPage) { assertNotSame(page, secondPage); } else { assertSame(page, secondPage); } }
/** @exception Exception if an error occurs */ @Test @Alerts("hello") public void bigJavaScript() throws Exception { final StringBuilder html = new StringBuilder( "<html><head>\n" + "<script src='two.js'></script>\n" + "<link rel='stylesheet' type='text/css' href='three.css'/>\n" + "</head>\n" + "<body onload='test()'></body></html>"); final StringBuilder javaScript = new StringBuilder("function test() {\n" + "alert('hello');\n" + "}"); final StringBuilder css = new StringBuilder("body {color: blue}"); final long maxInMemory = getWebClient().getOptions().getMaxInMemory(); for (int i = 0; i < maxInMemory; i++) { html.append(' '); javaScript.append(' '); css.append(' '); } BigJavaScriptServlet1.CONTENT_ = html.toString(); BigJavaScriptServlet2.CONTENT_ = javaScript.toString(); BigJavaScriptServlet3.CONTENT_ = css.toString(); final int initialTempFiles = getTempFiles(); final Map<String, Class<? extends Servlet>> map = new HashMap<>(); map.put("/one.html", BigJavaScriptServlet1.class); map.put("/two.js", BigJavaScriptServlet2.class); map.put("/three.css", BigJavaScriptServlet3.class); startWebServer(".", null, map); try (final WebClient client = getWebClient()) { final CollectingAlertHandler alertHandler = new CollectingAlertHandler(); client.setAlertHandler(alertHandler); final HtmlPage page = client.getPage("http://localhost:" + PORT + "/one.html"); ((HTMLBodyElement) page.getBody().getScriptObject()).getCurrentStyle(); assertEquals(getExpectedAlerts(), alertHandler.getCollectedAlerts()); assertEquals(initialTempFiles + 1, getTempFiles()); } assertEquals(initialTempFiles, getTempFiles()); }
/** * Test that frames are loaded (issue #3544647). * * @throws Exception if the test fails. */ @Test public void frameShouldBeLoaded() throws Exception { final String html = "<html><body>\n" + "<iframe src='" + FIRST_SECOND + "'></iframe>\n" + "</body></html>"; final String frame = "<html><body><script>alert('in frame');</script></body></html>"; final WebClient client = new WebClient(); final List<String> collectedAlerts = new ArrayList<String>(); client.setAlertHandler(new CollectingAlertHandler(collectedAlerts)); final MockWebConnection conn = new GAEMockWebConnection(client); conn.setResponse(FIRST_URL, html); conn.setResponse(FIRST_SECOND, frame); client.setWebConnection(conn); client.getPage(FIRST_URL); assertEquals(Arrays.asList("in frame"), collectedAlerts); }
public static void main(String[] args) throws Exception { Logger.getLogger("com.gargoylesoftware").setLevel(Level.OFF); String app = System.getProperty("application.url", "http://*****:*****@tests.list").openStream(), "utf-8")); String marker = in.readLine(); if (!marker.equals("---")) { throw new RuntimeException("Oops"); } root = new File(in.readLine()); selenium = in.readLine(); tests = new ArrayList<String>(); String line; while ((line = in.readLine()) != null) { tests.add(line); } in.close(); } catch (Exception e) { System.out.println("~ The application does not start. There are errors: " + e); System.exit(-1); } // Let's tweak WebClient WebClient firephoque = new WebClient(BrowserVersion.INTERNET_EXPLORER_8); firephoque.setPageCreator( new DefaultPageCreator() { @Override public Page createPage(WebResponse wr, WebWindow ww) throws IOException { Page page = createHtmlPage(wr, ww); return page; } }); firephoque.setThrowExceptionOnFailingStatusCode(false); firephoque.setAlertHandler( new AlertHandler() { public void handleAlert(Page page, String string) { try { Window window = (Window) page.getEnclosingWindow().getScriptObject(); window.custom_eval( "parent.selenium.browserbot.recordedAlerts.push('" + string.replace("'", "\\'") + "');"); } catch (Exception e) { e.printStackTrace(); } } }); firephoque.setConfirmHandler( new ConfirmHandler() { public boolean handleConfirm(Page page, String string) { try { Window window = (Window) page.getEnclosingWindow().getScriptObject(); Object result = window.custom_eval( "parent.selenium.browserbot.recordedConfirmations.push('" + string.replace("'", "\\'") + "');" + "var result = parent.selenium.browserbot.nextConfirmResult;" + "parent.selenium.browserbot.nextConfirmResult = true;" + "result"); return (Boolean) result; } catch (Exception e) { e.printStackTrace(); return false; } } }); firephoque.setPromptHandler( new PromptHandler() { public String handlePrompt(Page page, String string) { try { Window window = (Window) page.getEnclosingWindow().getScriptObject(); Object result = window.custom_eval( "parent.selenium.browserbot.recordedPrompts.push('" + string.replace("'", "\\'") + "');" + "var result = !parent.selenium.browserbot.nextConfirmResult ? null : parent.selenium.browserbot.nextPromptResult;" + "parent.selenium.browserbot.nextConfirmResult = true;" + "parent.selenium.browserbot.nextPromptResult = '';" + "result"); return (String) result; } catch (Exception e) { e.printStackTrace(); return ""; } } }); firephoque.setThrowExceptionOnScriptError(false); // Go! int maxLength = 0; for (String test : tests) { String testName = test.replace(".class", "").replace(".test.html", "").replace(".", "/").replace("$", "/"); if (testName.length() > maxLength) { maxLength = testName.length(); } } System.out.println("~ " + tests.size() + " test" + (tests.size() != 1 ? "s" : "") + " to run:"); System.out.println("~"); firephoque.openWindow(new URL(app + "/@tests/init"), "headless"); boolean ok = true; for (String test : tests) { long start = System.currentTimeMillis(); String testName = test.replace(".class", "").replace(".test.html", "").replace(".", "/").replace("$", "/"); System.out.print("~ " + testName + "... "); for (int i = 0; i < maxLength - testName.length(); i++) { System.out.print(" "); } System.out.print(" "); URL url; if (test.endsWith(".class")) { url = new URL(app + "/@tests/" + test); } else { url = new URL( app + "" + selenium + "?baseUrl=" + app + "&test=/@tests/" + test + ".suite&auto=true&resultsUrl=/@tests/" + test); } firephoque.openWindow(url, "headless"); firephoque.waitForBackgroundJavaScript(5 * 60 * 1000); int retry = 0; while (retry < 5) { if (new File(root, test.replace("/", ".") + ".passed.html").exists()) { System.out.print("PASSED "); break; } else if (new File(root, test.replace("/", ".") + ".failed.html").exists()) { System.out.print("FAILED ! "); ok = false; break; } else { if (retry++ == 4) { System.out.print("ERROR ? "); ok = false; break; } else { Thread.sleep(1000); } } } // int duration = (int) (System.currentTimeMillis() - start); int seconds = (duration / 1000) % 60; int minutes = (duration / (1000 * 60)) % 60; if (minutes > 0) { System.out.println(minutes + " min " + seconds + "s"); } else { System.out.println(seconds + "s"); } } firephoque.openWindow( new URL(app + "/@tests/end?result=" + (ok ? "passed" : "failed")), "headless"); }