@Override public void runTest(TestRun run) throws IOException { if (++runs == 50) { runs = 0; cleanUp(); init(); } try { page.set(webClient.get().getPage("about:blank")); } catch (IOException e) { throw new RuntimeException(e); } HtmlPage pageRef = page.get(); pageRef.executeJavaScript(readFile(new File(run.getBaseDirectory(), run.getFileName()))); boolean decodeStack = Boolean.parseBoolean(System.getProperty(TeaVMTestRunner.JS_DECODE_STACK, "true")); File debugFile = decodeStack ? new File(run.getBaseDirectory(), run.getFileName() + ".teavmdbg") : null; RhinoResultParser resultParser = new RhinoResultParser(debugFile); AsyncResult asyncResult = new AsyncResult(); Function function = (Function) page.get().executeJavaScript(readResource("teavm-htmlunit-adapter.js")) .getJavaScriptResult(); Object[] args = new Object[] { decodeStack ? createStackDecoderFunction(resultParser) : null, new NativeJavaObject(function, asyncResult, AsyncResult.class) }; pageRef.executeJavaScriptFunction(function, function, args, page.get()); resultParser.parseResult((Scriptable) asyncResult.getResult(), run.getCallback()); }
/** {@inheritDoc} */ @Override protected void runJavaScript(final HtmlPage page) { if (script_ == null) { return; } page.executeJavaScript(script_, "JavaScriptStringJob", 1); }
/** * <p>Executes the specified JavaScript code within the page. The usage would be similar to what can * be achieved to execute JavaScript in the current page by entering "javascript:...some JS code..." * in the URL field of a native browser.</p> * <p><b>Note:</b> the provided code won't be executed if JavaScript has been disabled on the WebClient * (see {@link com.gargoylesoftware.htmlunit.WebClientOptions#isJavaScriptEnabled()}.</p> * @param sourceCode the JavaScript code to execute * @return a ScriptResult which will contain both the current page (which may be different than * the previous page) and a JavaScript result object */ public ScriptResult executeJavaScript(final String sourceCode) { return executeJavaScript(sourceCode, "injected script", 1); }
/** {@inheritDoc} */ @Override protected void runJavaScript(final HtmlPage page) { if (script_ == null) { return; } page.executeJavaScript(script_, "JavaScriptStringJob", 1); }
/** * <p>Executes the specified JavaScript code within the page. The usage would be similar to what can * be achieved to execute JavaScript in the current page by entering "javascript:...some JS code..." * in the URL field of a native browser.</p> * <p><b>Note:</b> the provided code won't be executed if JavaScript has been disabled on the WebClient * (see {@link com.gargoylesoftware.htmlunit.WebClientOptions#isJavaScriptEnabled()}.</p> * @param sourceCode the JavaScript code to execute * @return a ScriptResult which will contain both the current page (which may be different than * the previous page) and a JavaScript result object */ public ScriptResult executeJavaScript(final String sourceCode) { return executeJavaScript(sourceCode, "injected script", 1); }
WebClient htmlunit = new WebClient(); HtmlPage page = htmlunit.getPage("http://www.google.com"); page = page.executeJavaScript("<JS code here>").getNewPage(); //manipulate the JS code and re-excute page = page.executeJavaScript("<manipulated JS code here>").getNewPage(); //manipulate the JS code and re-excute page = page.executeJavaScript("<manipulated JS code here>").getNewPage();
import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlPage; public class Test { public static void main(String[] args) throws Exception { WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6); HtmlPage page = webClient.getPage("http://www.iana.org/"); String javaScriptCode = "inArray([1,2],3)"; Object result = page.executeJavaScript(javaScriptCode).getJavaScriptResult(); System.out.println(result); } }
private String compileAndCache(String input) { ScriptResult scriptResult = htmlPage.get().executeJavaScript(String.format("CoffeeScript.compile(\"%s\");", input)); String result = (String) scriptResult.getJavaScriptResult(); cache.put(input, result); return result; }
WebClient wc = new WebClient(BrowserVersion.FIREFOX_3_6); HtmlPage page = wc.getPage("http://urlhere"); page.executeJavaScript("JS code here");
/** * Runs the sanity check. * @throws Exception if anything goes wrong */ private static void runSanityCheck() throws Exception { try (WebClient webClient = new WebClient()) { final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net/index.html"); page.executeJavaScript("document.location"); System.out.println("Sanity check complete."); } }
/** * Runs the sanity check. * @throws Exception if anything goes wrong */ private static void runSanityCheck() throws Exception { final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net/index.html"); page.executeJavaScript("document.location"); System.out.println("Sanity check complete."); }
/** * Runs the sanity check. * @throws Exception if anything goes wrong */ private static void runSanityCheck() throws Exception { final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net/index.html"); page.executeJavaScript("document.location"); System.out.println("Sanity check complete."); }
/** * Runs the sanity check. * @throws Exception if anything goes wrong */ private static void runSanityCheck() throws Exception { final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net/index.html"); page.executeJavaScript("document.location"); System.out.println("Sanity check complete."); }
/** * Runs the sanity check. * @throws Exception if anything goes wrong */ private static void runSanityCheck() throws Exception { try (WebClient webClient = new WebClient()) { final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net/index.html"); page.executeJavaScript("document.location"); System.out.println("Sanity check complete."); } }
/** * <p class="changed_added_4_0">Run JavaScript expression in page context and returns result</p> * @param script * @return */ public Object runScript(String script){ ScriptResult scriptResult = page.executeJavaScript(script); page = (HtmlPage) scriptResult.getNewPage(); return scriptResult.getJavaScriptResult(); }
WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); webClient.setJavaScriptEnabled(true); HtmlPage page = webClient.getPage("http://www.google.com/ncr"); ScriptResult scriptResult = page.executeJavaScript("document.title"); System.out.println(scriptResult.getJavaScriptResult());
WebClient htmlunit = new WebClient(); HtmlPage page = htmlunit.getPage("http://www.google.com"); //executing the javascript and getting the new page page = page.executeJavaScript("<JS code here>").getNewPage();
try (final WebClient webClient = new WebClient(BrowserVersion.CHROME)) { String url = "http://localhost/test.html"; HtmlPage htmlPage = webClient.getPage(url); NativeArray array = (NativeArray) htmlPage.executeJavaScript("arMailRuMessages").getJavaScriptResult(); for (int i = 0; i < array.getLength(); i++) { NativeObject object = (NativeObject) array.get(i); String id = (String) object.get("id"); System.out.println(id); NativeObject correspondents = (NativeObject) object.get("correspondents"); NativeArray from = (NativeArray) correspondents.get("from"); System.out.println(((NativeObject) from.get(0)).get("name")); } }
@Override protected HtmlPage initialValue() { MockWebConnection webConnection = new MockWebConnection(); WebClient webClient = new WebClient(); webClient.setWebConnection(webConnection); try { HtmlPage page = webClient.getPage(WebClient.URL_ABOUT_BLANK); page.executeJavaScript(ioUtilsWrapper.toString("/vendor/js/coffee-script.js")); return page; } catch (IOException e) { throw new RuntimeException(e); } } };
/** * Tests that an anonymous object can be bound. */ public void testAnonymousBind() throws Exception { WebClient wc = new WebClient(); HtmlPage page = wc.getPage(new URL(url, "/bindAnonymous")); page.executeJavaScript("v.xyz('hello');"); assertEquals("hello",anonymousValue); }