简体   繁体   English

javascript隐藏的HttpUnit和参数

[英]HttpUnit and Parameters Hidden with Javascript

I am trying to write some test cases in HttpUnit to test a website that has a lot of Javascript in it. 我试图在HttpUnit中编写一些测试用例,以测试其中包含很多Javascript的网站。 The HttpUnit works great - provided I have Javascript disabled through HttpUnitOptions.setJavaScriptEnabled(false)! HttpUnit的效果很好-只要我通过HttpUnitOptions.setJavaScriptEnabled(false)禁用了Javascript!

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. 但是,我正在测试的网站上有许多字段被隐藏,直到某些其他字段被填充为止-我怀疑这必须是Javascript,但我不确定,因为我无法访问网站源代码,只能访问原始代码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. 这给我带来了一个难题–我无法使用HttpUnit填充我需要的所有字段,因为在填充以前的字段之前,html中缺少其中的一些字段。 For example, if I pick an option from dropdown menu A, the page will refresh and suddenly a dropdown menu B appears. 例如,如果我从下拉菜单A中选择一个选项,则页面将刷新,然后突然出现下拉菜单B。

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. 我不确定到底是什么导致了此问题-我几乎没有Java脚本的经验,所以我不确定这是否根本就是Javascript问题。 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. 最后,这是初次提交后的html文件。 The only difference between the html before and after the first submit is that the DropDownPackageOptions parameter is missing before the initial submit. 第一次提交之前和之后的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>
...

Semms like your code is referencing a field (DropDownPackageOptions) that is not present in the form. 像您的代码这样的语义引用了表单中不存在的字段(DropDownPackageOptions)。 It would help if you also posted the HTML. 如果您还发布了HTML,将会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM