WebUnit returns a js file not the html
Brought to you by:
henryju
Hi there,
Many thanks for jWebUnit - it looks good among
the multitude of web testing apps springing up!
We are coming across a slight snag with the
javaScript however. I have been successfully able
to execute a click of a button, but when I check for
contents of the resulting page jWebUnit seems to
be testing against one of my static javaScript files
rather than the resulting web page! Not
surprisingly, my expected text is not there...
Do you have any idea why this might be?
Cheers,
Mike
Logged In: YES
user_id=619947
Mike,
Thanks for the feedback! Javascript support is relatively
new in HttpUnit, which is the tool we are wrappering to
handle request / response to the target application. This
is the area where there will undoubtedly be the most
surprises ;-).
We do have a test for button click javascript in our test
tree - I believe all the called function does is set a form
input element to a certain value, then we test after the
click to make sure the value changed.
Can you send us the html snippet for your button, along with
the declaration of the javascript in that page, and the
javascript itself? I don't believe we test triggering of
javascript functions that are in a separate file. I'll
check the HttpUnit board and see if anyone has reported a
problem like that.
Jim
Logged In: YES
user_id=662315
Hi There - Ok I've done you a test site... can't see any
way to upload a file here so here is the source. Sorry if
I'm just being blind! When you run the test case
WebResponse becomes the nav.js file rather than
next.html when you click the 'next' button. Not sure if
this is jWebUnit or HttpUnit I'm afraid!
--------------------------------------------
index.html:
<html>
<head>
<script src="nav.js"
type="text/javascript" language="javascript"></script>
</head>
<body>
<h1>Javascript Test</h1>
<form>
<input type="button"
onclick="next()" value="Next" id="next">
</form>
</body>
</html>
-------------------------------------------
next.html:
<html>
<head>
<script src="nav.js"
type="text/javascript" language="javascript"></script>
</head>
<body>
<h1>Next</h1>
<p>Here is the text we expect</p>
</body>
</html>
--------------------------------------------
nav.js:
function next()
{
window.location='next.html';
return true;
}
--------------------------------------------
ScriptWebTest.java
import net.sourceforge.jwebunit.WebTestCase;
/**
* @author Michael Brown
*/
public class ScriptWebTestCase extends WebTestCase
{
/**
* Constructor for LoginWebTestCase.
* @param arg0
*/
public ScriptWebTestCase(String arg0)
{
super(arg0);
}
/**
* Constructor for LoginWebTestCase.
*/
public ScriptWebTestCase()
{
super();
}
getTestContext().setBaseUrl
("http://localhost/test/");
}
public void testNext() throws Exception
{
beginAt("index.html");
clickButton("next");
assertTextPresent("Here is the text we expect");
}
}
--------------------------------------------
Logged In: YES
user_id=619947
Hi,
Thanks.
Yup this may be an HttpUnit or Rhino issue (HttpUnit uses
Rhino as its javascript engine - layer upon layer here). I
found this post on the HttpUnit support forum from 11/7
which has not yet been responded to:
https://sourceforge.net/forum/forum.php?thread_id=760961&forum_id=20295
Her symptom is different though - undefined function rather
than response being the target javascript file.
Got your snippet and will try to recreate within our test tree.
Jim
Logged In: YES
user_id=662315
Hmm, yes that got me thinking. If you try it as a link it
works fine...
HTML:
<a onclick="next()" href="#">next</a>
Test:
clickLinkWithText("next");
Logged In: YES
user_id=662315
... except Explorer seems to throw a wobbly if you use a
function called next() on a button (!) - use gotoNext()
instead.
Logged In: YES
user_id=619947
Mike,
Incorporated your html into a new test in our test tree. It
passes (javascript in file processed properly on button
click). This makes me wonder if you might have some
different versions of components jWebUnit uses from what is
included with the jWebUnit 1.1 lib. Were you using HttpUnit
on your project already? Or an earlier version of jWebUnit?
Make sure that the classpath for your tests uses the jars
included with 1.1 (HttpUnit 1.5 and nekoHtml). You can
remove the jTidy.jar as well - HttpUnit 1.5 prefers nekoHtml
as its low-level parser.
The test I added is in our CVS tree:
http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/jwebunit/jWebUnit/test/net/sourceforge/jwebunit/JavaScriptEventsTest.java?rev=1.2&content-type=text/vnd.viewcvs-markup
It is the last test method: testJavaScriptInFile. You
should be able to update from our CVS repository and run
this test succesfully for yourself .
This uses the PseudoServer from HttpUnit to act as the web
server for the tests.
Jim
Logged In: YES
user_id=662315
Hi Jim,
Ah the test is not quite right. The framework correctly
goes to the next.html page, but picks up the external
script rather than the html. You need to have the
external script in the next.html to pick this bug up.
defineResource("next.html",
"<html><head><script src=\&quot;nav.js\&quot;
type=\&quot;text/javascript\&quot;
language=\&quot;javascript\&quot;></script></head>" etc etc...
Let me know if that works for you - I can't seem to get
the PseudoServer to work here!
Cheers,
Mike
Logged In: YES
user_id=619947
Ah, I see.
Recreated in test. Very weird - you'd think if it wasn't
handling the javascript src= properly that it would mess up
the index.html as well. Will step up through HttpUnit code
now that it is recreated and try to find out what is up.
PseudoServer is fiddly. It came from HttpUnit, but we had
to replace it in jWebUnit to get it to work - that may be
your problem (classpath). Be sure it is using
com.meterware.pseudoserver.PseudoServer from the jWebUnit
test tree, and not from any HttpUnit jar or project tree.
At least one other person posted that they were able to run
AllTests class and everything passed, so PseudoServer must
have run for them. Oh yeah, also try the JUnit Text UI
instead of Swing UI - sometimes you can get some fun
classloading issues when the tests are launched as a
separate process.
Later,
Jim
Logged In: YES
user_id=597548
Mike,
Spoke disparagingly of HttpUnit too soon, this is our bug
rather than Russel's.
We'll get a new build up soon with the fix - that is a
pretty significant oops.
If you want to build the fix yourself, it is in
HttpUnitDialog.clickButton method:
public void clickButton(String buttonId) {
try {
getButton(buttonId).click();
resp = wc.getCurrentPage(); //Add this line
} catch (Exception e) {
throw new
RuntimeException(ExceptionUtility.stackTraceToString(e));
}
}
Jim
Logged In: YES
user_id=662315
Hi Jim,
Well I can't fault that for support thank you very much!
Looking forward to the new release to really put it
through its paces :)
Mike
Logged In: YES
user_id=597548
Fixed with version 1.1.1.