1

I am trying to write some test cases in HttpUnit to test a website that has a lot of Javascript in it. The HttpUnit works great - provided I have Javascript disabled through HttpUnitOptions.setJavaScriptEnabled(false)!

However, the website that I am testing has a number of fields that are hidden until certain other fields are populated - I suspect this must be Javascript, but I am not certain because I do not have access to the website source code, just the raw html. This presents me with a dilemma - I cannot use HttpUnit to populate all of the fields I need, because some of them are missing from the html until previous fields are populated. For example, if I pick an option from dropdown menu A, the page will refresh and suddenly a dropdown menu B appears.

I'm not sure what exactly is causing this problem - I have next to no experience with Javascript, so I'm not sure if it's even a Javascript problem at all. But I would very much like to input data into these hidden fields. Any suggestions?

Below is the code (more or less) that I am using:

public class TestSuite {
  public static void main(String[] args) throws SAXException, IOException {

    //Load home page
    HttpUnitOptions.setDefaultCharacterSet("UTF-8");
    HttpUnitOptions.setExceptionsThrownOnScriptError(false);
    WebConversation wc = new WebConversation();
    WebResponse currentPage = wc.getResponse("http://testingwebsite.aspx");

    //Click a link and fill out the resulting form
    currentPage = currentPage.getLinkWith("Apply");
    WebForm form = currentPage.getFormWithName("aspnetForm");
    form.setParameter("DropDownCategory");
    form.setParameter("TextBoxIDNumber");
    ...
    form.setParameter("DropDownPackage"); //Populating this field causes DropDownPackageOptions to appear

    //Submit the form - after submit, hidden field DropDownPackageOptions IS IN THE HTML
    SubmitButton submit = form.getSubmitButton("ButtonSubmit");
    currentPage = form.submit(submit);

    //Error message is displayed on html page, saying hidden form is required.
    //Fill in the hidden field, which is in html and should be present
    //Exception thrown at next line:
    form.setParameter("DropDownPackageOptions"); //This is hidden until DropDownPackage is selected

    //Submit the form again
    submit = form.getSubmitButton("ButtonSubmit");
    currentPage = form.submit(submit);

    System.out.println(currentPage.getText());
  }
}

Below is the stack trace for the exception that is thrown as a result of running this code:

Exception in thread "main" com.meterware.httpunit.WebForm$NoSuchParameterException: No parameter 
named 'DropDownPackageOptions' is defined in the form
  at com.meterware.httpunit.WebForm.setParameter(WebForm.java:633)
  at com.meterware.httpunit.WebForm.setParameter(WebForm.java:623)
  at TestSuite.main(TestSuite.java:34)

Finally, here is the html file after the initial submit. The only difference between the html before and after the first submit is that the DropDownPackageOptions parameter is missing before the initial submit.

<html xmlns="http://www.w3.org/1999/xhtml">

<head id="head1"><meta http-equiv="X-UA-Compatible" content="IE-EmulateIE7" /><title>
  Title
</title>
  <script src="../Scripts/General.js" type="text/javascript"></script>
  <link href="../css/Style.css" rel="stylesheet" type="text/css" /><link href="../css/HomePage.css" rel="stylesheet" type="text/css" /></head>

<body>
  <form name="aspnetForm" method="post" action="Apply.aspx" id="aspnetForm">
<input type="hidden" name="view" id="view" value = "/..... (huge string of random chars)" />
<input type="hidden" name="event" id="event" value = "/...... (huge string of random chars)" />
<div id="header">
  ...
</div>

<div id="frame">
  <div class="heading">Category: </div>
  <select name="DropDownCategory" id="DropDownCategory">
    <option value="">--</option>
    <option selected="selected" value="1">Category1</option>
    <option value="2">Category2</option>
    <option value="3">Category3</option>
  </select>

  ... (additional form fields)

  <div class="heading">Package: </div>
  <select name="DropDownPackage" id="DropDownPackage">
    <option value="selected" value="">--</option>
    <option value="1">Pkg 1</option>
    <option value="2">Pkg 2</option>
    ...
  </select>

  <div class="heading">Package Options: </div>
  <select name="DropDownPackageOptions" id="DropDownPackageOptions">
    <option selected="selected" value="">--</option>
    <option value="2">Opt 2</option>
    <option value="3">Opt 3</option>
    ...
  </select>

  <br /><input type="submit" name="ButtonSubmit" value="Submit" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(&quot;ButtonSubmit&quot;, &quot;&quot;, true, &quot;&quot;, &quot;&quot;, false, false))" id="ButtonSubmit" />
</div>
...
Victoria
  • 11
  • 2
  • you might want to check the behaviour of the site with a debugger like Firebug. This way you can get a better feeling for the dynamics of the page. There is some JavaScript support in HttpUnit - for my own websites I was always able to add the missing JavaScript features to httpunit step by step. If you'd like to submit a patch /extension just contact me - I am one of the httpunit committers. – Wolfgang Fahl Sep 23 '14 at 16:52

1 Answers1

0

Semms like your code is referencing a field (DropDownPackageOptions) that is not present in the form. It would help if you also posted the HTML.

Andres
  • 10,561
  • 4
  • 45
  • 63