From: Shanbhag, G. <ga...@am...> - 2005-06-10 02:17:16
|
This looks more of a Rhino issue than a htmlunit issue. I am having problems using the document element inside a iframe=20 through Javascript. Is there any workaround to this problem? Thanks, Gautam Heres the code and the output contains the html I used. import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import org.apache.commons.httpclient.cookie.CookiePolicy; import org.apache.commons.httpclient.params.DefaultHttpParams; import org.apache.commons.httpclient.params.HttpMethodParams; import com.gargoylesoftware.htmlunit.BrowserVersion; import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.HtmlButton; import com.gargoylesoftware.htmlunit.html.HtmlButtonInput; import com.gargoylesoftware.htmlunit.html.HtmlPage; public class TestHtmlUnit { public static void main(String args[]) { final WebClient webClient =3D new = WebClient(BrowserVersion.INTERNET_EXPLORER_6_0); webClient.setJavaScriptEnabled(true); System.setProperty("apache.commons.httpclient.cookiespec", = CookiePolicy.BROWSER_COMPATIBILITY); = DefaultHttpParams.getDefaultParams().setBooleanParameter(HttpMethodParams= .SINGLE_COOKIE_HEADER, true); =09 try { final HtmlPage page =3D (HtmlPage)webClient.getPage( new = URL("file:///c:/gautam/gs.html") ); String out =3D page.getWebResponse().getContentAsString(); System.out.println("Page:"+out); HtmlButtonInput hb=3D = (HtmlButtonInput)page.getHtmlElementById("dee"); hb.click(); =09 } catch (FailingHttpStatusCodeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } =09 =09 } =09 =09 } Output: Page: <html> <head> <script language=3D"JavaScript" > function dada() { var iframe=3Ddocument.getElementById("gautam"); iframe.contentWindow.document.write("hello"); } </script> </head> <body> <IFRAME id=3D"gautam" src=3D"" width=3D"800" height=3D"200"></IFRAME> <input type=3D"button" id=3D"dee" onclick=3D"dada()"> </body> </html> Exception in thread "main" =3D=3D=3D=3D=3D=3D=3D EXCEPTION START = =3D=3D=3D=3D=3D=3D=3D=3D EcmaError: lineNumber=3D[4] column=3D[0] lineSource=3D[null] = name=3D[TypeError] sourceName=3D[Embedded script] message=3D[TypeError: = Cannot read property "document" from undefined (Embedded script#4)] com.gargoylesoftware.htmlunit.ScriptException: TypeError: Cannot read = property "document" from undefined (Embedded script#4) at = com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(Ja= vaScriptEngine.java:340) at = com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPo= ssible(HtmlPage.java:759) at = com.gargoylesoftware.htmlunit.html.ClickableElement.click(ClickableElemen= t.java:107) at amazon.gautams.test.TestHtmlUnit.main(TestHtmlUnit.java:32) Enclosed exception:=20 org.mozilla.javascript.EcmaError: TypeError: Cannot read property = "document" from undefined (Embedded script#4) at = org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:32= 40) at = org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:32= 30) at = org.mozilla.javascript.ScriptRuntime.typeError(ScriptRuntime.java:3246) at = org.mozilla.javascript.ScriptRuntime.typeError2(ScriptRuntime.java:3265) at = org.mozilla.javascript.ScriptRuntime.undefReadError(ScriptRuntime.java:32= 78) at = org.mozilla.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:131= 9) at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2744) at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2164) at = org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:= 140) at = org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:304) at = org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2769) at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2145) at = org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:= 140) at = com.gargoylesoftware.htmlunit.javascript.host.EventHandler.call(EventHand= ler.java:88) at = com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine.callFunction(Ja= vaScriptEngine.java:335) at = com.gargoylesoftware.htmlunit.html.HtmlPage.executeJavaScriptFunctionIfPo= ssible(HtmlPage.java:759) at = com.gargoylesoftware.htmlunit.html.ClickableElement.click(ClickableElemen= t.java:107) at amazon.gautams.test.TestHtmlUnit.main(TestHtmlUnit.java:32) =3D=3D CALLING JAVASCRIPT =3D=3D function () { [native code, arity=3D0] } =3D=3D=3D=3D=3D=3D=3D EXCEPTION END =3D=3D=3D=3D=3D=3D=3D=3D =20 -----Original Message----- From: htm...@li... = [mailto:htm...@li...] On Behalf Of Brad = Clarke Sent: Thursday, June 09, 2005 9:18 AM To: htm...@li... Subject: Re: [Htmlunit-user] Problem using htmlunit/rhino with dom I have no idea what you're trying to do but SimpleScriptable (and = everything under the javascript package) is internal code. From the = package.html: "Classes in this package are specific for the javascript engine - users = of HtmlUnit shouldn't need anything in this package." Then there's the fact that creating a SimpleScriptable directly doesn't = seem like it would do much good anyway (it should probably be an = abstract class but since it isn't really public API...). Brad C --- "Ulaszek, Ryan" <Rya...@in...> wrote: > Hello, >=20 > I am trying to load some java script buisness logic into a java app=20 > using rhino and ran into a problem with calls being made to=20 > setInterval which is a DOM function. I am trying to use html units=20 > SimpleScriptable but am not sure on how to use it. I am hoping that=20 > using that object will provide DOM support. Is this true? This is=20 > what I am trying to do with rhino. Anyone have suggestions on how to=20 > do this with htmlunit? Any help is greatly appreciated. >=20 > Thanks >=20 > public void getCalcs(Context cx) > { > ScriptableObject shell =3D new ScriptableObject(); =20 >=20 > Object fObj2 =3D shell .get("setFormField", shell ); > if (!(fObj2 instanceof Function)) { > System.out.println("field is undefined or not a = function."); > } else { > Object functionArgs[] =3D { "muniIntNonAz", "3","azAgi" }; > Function f2 =3D (Function)fObj2; > Object result =3D f2.call(cx, shell , shell , = functionArgs); > String report =3D Context.toString(result); > System.out.println(report); > } > } >=20 > I tried this >=20 > public void getCalcs(Context cx) > { > SimpleScriptable shell =3D new SimpleScriptable(); =20 >=20 > Object fObj2 =3D shell .get("setFormField", shell ); > if (!(fObj2 instanceof Function)) { > System.out.println("field is undefined or not a = function."); > } else { > Object functionArgs[] =3D { "muniIntNonAz", "3","azAgi" }; > Function f2 =3D (Function)fObj2; > Object result =3D f2.call(cx, shell , shell , = functionArgs); > String report =3D Context.toString(result); > System.out.println(report); > } > } >=20 >=20 > Ryan Ulaszek | QA Software Automation Engineer: | Intuit, Inc.=20 > 858-525-9560 >=20 >=20 ------------------------------------------------------- This SF.Net email is sponsored by: NEC IT Guy Games. How far can you = shotput a projector? How fast can you ride your desk chair down the = office luge track? If you want to score the big prize, get to know the little guy. =20 Play to win an NEC 61" plasma display: http://www.necitguy.com/?r=3D20 = _______________________________________________ Htmlunit-user mailing list Htm...@li... https://lists.sourceforge.net/lists/listinfo/htmlunit-user |