1 38 package com.gargoylesoftware.htmlunit; 39 40 import java.util.ArrayList; 41 import java.util.Arrays; 42 import java.util.List; 43 44 import com.gargoylesoftware.htmlunit.html.HtmlElement; 45 import com.gargoylesoftware.htmlunit.html.HtmlPage; 46 47 55 public class ScriptEngineTest extends WebTestCase { 56 60 public ScriptEngineTest( final String name ) { 61 super(name); 62 } 63 64 65 71 public void testScriptTags_AllLocalContent() throws Exception { 72 final String content 73 = "<html>" 74 + "<head><title>foo</title>" 75 + "<script>One</script>" + "<script language='javascript'>Two</script>" 77 + "<script type='text/javascript'>Three</script>" 78 + "<script type='text/perl'>Four</script>" + "</head>" 80 + "<body>" 81 + "<p>hello world</p>" 82 + "</body></html>"; 83 final List collectedScripts = new ArrayList(); 84 loadPageAndCollectScripts(content, collectedScripts); 85 86 final List expectedScripts = Arrays.asList( new String[]{ 88 "One", "Two", "Three"} ); 89 90 assertEquals( expectedScripts, collectedScripts ); 91 } 92 93 94 private HtmlPage loadPageAndCollectScripts( 95 final String html, final List collectedScripts ) 96 throws Exception { 97 98 final WebClient client = new WebClient(); 99 client.setScriptEngine( new ScriptEngine(client) { 100 public void initialize(final HtmlPage page) { 101 } 102 public Object execute( 103 final HtmlPage htmlPage, final String sourceCode, 104 final String sourceName, final HtmlElement htmlElement ) { 105 collectedScripts.add( sourceCode ); 106 return null; 107 } 108 public Object callFunction( 109 final HtmlPage htmlPage, final Object javaScriptFunction, 110 final Object thisObject, final Object [] args, 111 final HtmlElement htmlElement ) { 112 return null; 113 } 114 public String toString( 115 final HtmlPage htmlPage, final Object javaScriptObject ) { 116 return null; 117 } 118 public boolean isScriptRunning() { 119 return false; 120 } 121 } ); 122 123 final MockWebConnection webConnection = new MockWebConnection( client ); 124 125 webConnection.setDefaultResponse( html ); 126 client.setWebConnection( webConnection ); 127 128 final HtmlPage page = (HtmlPage) client.getPage(new WebRequestSettings(URL_GARGOYLE, SubmitMethod.POST)); 129 return page; 130 } 131 } 132 | Popular Tags |