Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

HttpUnit - call a javascript functin

807591Feb 4 2008 — edited Jun 2 2008
Hi to all,
I'm an italian student and i have some problem with HttpUnit package.
I must test a web application and i implement this java code to download a pdf file:
TableCell cell = table.getTableCell(4, 0);

WebLink link = cell.getLinks()[0];

WebResponse resp1 = link.click();
		
WebLink[] links = resp1.getLinks();
for (int i=0; i<links.length; i++){
	if (links.getText().equals("Download document")){

String strOnClick = links[i].getAttribute("onclick");
links[i].getScriptableDelegate().doEvent(strOnClick);
System.out.println(strOnClick);
}
}


With the print I have *"download('428D8B70-9xxxxxxxxx')"* , so I arrive where I want.
I want to download a pdf file from a link like this:
<a href="#" onclick="download('428D8B70-9xxxxxxxxx')">Download document</a>
The html page isn't wrote by me and i can't change it.
The error is:
com.meterware.httpunit.HttpNotFoundException: Error on HTTP request: 404 Not Found [http://localhost:7777/speed/common/docviewer?id=428D8B70-xxxxxxxxxxxxxxxxxx-xxxxx-xxx-xx-xx1&download=true]
	at org.mozilla.javascript.WrappedException.<init>(WrappedException.java:56)
	at org.mozilla.javascript.WrappedException.wrapException(WrappedException.java:111)
	at org.mozilla.javascript.ScriptableObject.setBySetter(ScriptableObject.java:296)
	at org.mozilla.javascript.ScriptableObject.put(ScriptableObject.java:256)
	at com.meterware.httpunit.javascript.JavaScript$JavaScriptEngine.put(JavaScript.java:273)
	at org.mozilla.javascript.ScriptableObject.putProperty(ScriptableObject.java:1476)
	at org.mozilla.javascript.ScriptRuntime.setProp(ScriptRuntime.java:842)
	at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2046)
	at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2163)
	at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:58)
	at com.meterware.httpunit.javascript.JavaScript$JavaScriptEngine.performEvent(JavaScript.java:172)
	at com.meterware.httpunit.scripting.ScriptableDelegate.doEvent(ScriptableDelegate.java:56)
	at Asp_GeDoc.StartScript.search(StartScript.java:162)
	at Asp_GeDoc.Shell.main(Shell.java:36)

Exception in thread "main" java.lang.RuntimeException: Event 'download('428D8B70-xxxxxxxxxxxxxxxxx')' failed: com.meterware.httpunit.HttpNotFoundException: Error on HTTP request: 404 Not Found [http://localhost:7777/speed/common/docviewer?id=428D8B70-xxxxxxxxxxxxxxxxxx-xxxxx-xxx-xx-xx1&download=true]
	at com.meterware.httpunit.javascript.JavaScript$JavaScriptEngine.handleScriptException(JavaScript.java:199)
	at com.meterware.httpunit.javascript.JavaScript$JavaScriptEngine.performEvent(JavaScript.java:175)
	at com.meterware.httpunit.scripting.ScriptableDelegate.doEvent(ScriptableDelegate.java:56)
	at Asp_GeDoc.StartScript.search(StartScript.java:162)
	at Asp_GeDoc.Shell.main(Shell.java:36)
I can't use HtmlUnit.

Can anyone help me, please?

P.S.
Sorry for my english, I don't know very well :P

Comments

mlk
PallaBall wrote:
I'm an italian student and i have some problem with HttpUnit package.
I must test a web application and i implement this java code to download a pdf file:
Error on HTTP request: 404 Not Found
[http://localhost:7777/speed/common/docviewer?id=428D8B70-xxxxxxxxxxxxxxxxxx-xxxxx-xxx-xx-xx1&download=true]
Does http://localhost:7777/speed/common/docviewer exist?
807591
Yes, it exist...
If I go to the web application with my computer I can do all action.

If you have another solution to run a javascript function tell me....

Edited by: PallaBall on Feb 4, 2008 4:54 PM
mlk
PallaBall wrote:
If you have another solution to run a javascript function tell me....
It looks like the JavaScript function IS running. If it was not it would not attempt to retrieve http://localhost:7777/speed/common/docviewer?id=428D8B70-xxxxxxxxxxxxxxxxxx-xxxxx-xxx-xx-xx1&download=true
807591
This raises a question in my mind: how do you unit test JavaScript? Is there a JSUnit?
807591
I have change my code:
links.getScriptableDelegate().doEvent("download");				
resp = webConversation.getCurrentPage();
System.out.println(resp.getURL().toString());
The string "download" is the function and now I haven't any error or exception, but the url is the same, it isn't change....

I don't know how I can resolve this problem. :(                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
mlk
BigDaddyLoveHandles wrote:
This raises a question in my mind: how do you unit test JavaScript? Is there a JSUnit?
Yes. Never used it (don't do JS). http://www.jsunit.net/

The other option would be to use that funky looking Google Web Toolkit. That looks as if you would write your JS in Java, and unit test it with jUnit.
807591
How can I use it?
Can you give me an example?
Thks
---------
Edit:
may be that i have found a solution...
There is a mozilla's project, it's name is Rhino, with I can run a javascript function in a java class.
Attention, in HttpUnit pakaje you have js.jar, may be that there are some errors and your java code isn't validated. I have change HttpUnit's js.jar with Rhino's js.jar and now I haven't compile error.
My next point is to try my new java code...

Edited by: PallaBall on Feb 6, 2008 7:33 PM

Edited by: PallaBall on Feb 6, 2008 8:28 PM
807591
Where I'll get that rhino's js.jar file
1 - 8
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jun 30 2008
Added on Feb 4 2008
8 comments
696 views