使用Javascript隐藏的HttpUnit和参数

时间:2014-04-25 19:30:59

标签: java javascript http-unit

我正在尝试在HttpUnit中编写一些测试用例来测试一个包含大量Javascript的网站。 HttpUnit工作得很好 - 只要我通过HttpUnitOptions.setJavaScriptEnabled(false)禁用了Javascript!

然而,我正在测试的网站有许多隐藏的字段,直到某些其他字段被填充 - 我怀疑这必须是Javascript,但我不确定,因为我无法访问网站源代码,只是原始的HTML。这让我陷入两难境地 - 我无法使用HttpUnit来填充我需要的所有字段,因为其中一些字段在html中缺失,直到填充了以前的字段。例如,如果我从下拉菜单A中选择一个选项,页面将刷新并突然出现下拉菜单B.

我不确定究竟是什么导致了这个问题 - 我几乎没有使用Javascript的经验,所以我不确定它是否甚至是一个Javascript问题。但我非常希望将数据输入这些隐藏的字段。有什么建议吗?

以下是我正在使用的代码(或多或少):

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());
  }
}

以下是运行此代码时抛出的异常的堆栈跟踪:

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)

最后,这是初始提交后的html文件。第一次提交之前和之后的html之间的唯一区别是在初始提交之前丢失了DropDownPackageOptions参数。

<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>
...

1 个答案:

答案 0 :(得分:0)

似乎您的代码引用了表单中不存在的字段(DropDown PackageOptions)。如果您还发布了HTML,那将会有所帮助。