On 10-05-31 05:04 PM, Freddy Daoud wrote:
Hi Daryl,
As you said, it might get tricky to test the response like this.
AJAX-based testing is outside the scope of the Stripes mock
framework; however, I'd recommend HtmlUnit[1] and do higher-level
AJAX/JavaScript testing, i.e. test for the ultimate result on the
page that you want to verify. It works great for such things. For
example, you can verify such behavior as the classic "selecting
an item from a select box populates another select box with AJAX",
even if the select box is not present on the initial page.
Cheers,
Freddy
Using HtmlUnit will require testing from within a container won't it ?
Not necessarily a bad thing, but I've always liked the way Stripes lets
you test outside of your container. Thanks though, maybe I'll try it out.
BTW, I did find a way to do this with Rhino after a bit of whacking
around yesterday. It's kind of ugly (and probably needs a try-finally
block to close the context) , but could probably be encapsulated nicely
with a utility class for easier use and made more robust to handle
different JavaScriptResolutions.
Here's a code example.
public void testLogin_fail() throws Exception {
trip.execute(LOGIN_EVENT);
AjaxLoginOrMenuActionBean actionBean =
trip.getActionBean(AjaxLoginOrMenuActionBean.class);
// get the js output and convert to something we can evaluate
String output = trip.getResponse().getOutputString();
output = output.replaceAll("\n", "");
output = "var jsonData = eval('" + output + "')";
// evaluate and get the error from the js map
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
cx.evaluateString(scope, output, "<cmd>", 1, null);
Scriptable jsonData = (Scriptable) scope.get("jsonData", scope);
String myError = (String) jsonData.get("error", scope);
// check it
assertEquals("FAIL_INCORRECT_UN_PW", myError);
}
This test will pass if the following were used to return the
JavaScriptResolution.
Map <String, String> returnObj = new HashMap<String, String>();
returnObj.put("error", "FAIL_INCORRECT_UN_PW";
return new JavaScriptResolution(returnObj);
Here's a handy reference on Embedding Rhino that got me to that point.
http://www.mozilla.org/rhino/tutorial.html
Daryl
------------------------------------------------------------------------------
_______________________________________________
Stripes-users mailing list
Stripes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/stripes-users