1

多くのJavascriptを含むWebサイトをテストするために、HttpUnitでいくつかのテストケースを作成しようとしています。HttpUnitOptions.setJavaScriptEnabled(false) で Javascript を無効にしていれば、HttpUnit はうまく機能します。

ただし、私がテストしているWebサイトには、他の特定のフィールドが入力されるまで非表示になっているフィールドがいくつかあります. 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>
...
4

1 に答える 1

0

コードがフォームに存在しないフィールド (DropDownPackageOptions) を参照しているようです。HTMLも載せていただけると助かります。

于 2014-04-25T19:35:09.863 に答える