Search code examples
javajspjunithttp-unit

How to enter text into input via httpUnit


I've got a class assignment to test using httpUnit a use case that involves entering text into a textbox even though it is not wrapped in a form tag. Without a form to look inside, how do I send text or set the value? GetElementWithId doesn't allow you to do either of those. I can make any changes to the code I want in addition to writing the tests.

Here is the jsp code used to render the html:

<script type="text/javascript">
 $(function(){

    $("#searchBox").keyup(function(){
        $("#userSearch").attr("src","patientSearch.jsp?forward=<%=StringEscapeUtils.escapeHtml("" + (request.getParameter("forward") ))%>&q="+$("#searchBox").val()+"&allowDeactivated="+$("#allowDeactivated:checked").val())
    });
    $("#oldSearch").hide(); 

 });
</script>
<h2> Select a Patient</h2>
<b>Search by name or MID:</b><br/>
<div style="border: 1px solid Gray; padding:5px;float:left;">
    <input id="searchBox" name="searchBox" style="width: 250px;" type="text" value="<%= StringEscapeUtils.escapeHtml("" + ( firstName )) %>" />
    <br />
    <input id="allowDeactivated" type="checkbox" />
    Show deactivated patients
</div>

Solution

  • Here is the hack I used to get it to work...

    WebForm form = curPage.getForms()[0];
    form.getScriptableObject().setParameterValue("UID_PATIENTID", "1");
    Button btn = form.getButtons()[1];
    btn.click();
    curPage = wc.getCurrentPage();
    

    The form I'm changing is hidden therefore not what a real "user" would do but it lets my test pass. If someone knows how I can do this a better way, I'd really like to know.