package com.web.main;import com.meterware.httpunit.PostMethodWebRequest;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;public class HttpUnitSelect { public static void main(String[] args) {
new Webeeh().mldonkey_d2();
} public void changeSelectValue() { try {
WebConversation wc = new WebConversation(); // 如果我把index.jsp的值改为index.html,这个程序是工作正常的。
WebRequest request = new PostMethodWebRequest(
"http://localhost:8080/login/index.jsp");
WebResponse response = wc.getResponse(request);
WebForm form = response.getForms()[0]; // 这个是想改变下拉框的值,虽然返回值(b)为值,但是下拉框的值还是为null.
boolean b = form
.getScriptableObject()
.doEvent(
"javascript_:document.getElementById('aselect1').selectedIndex=2;"); form.setParameter("username", "1");
form.setParameter("password", "1");
System.out.println(form.getParameterValue("aselect1"));
// form.setParameter("aselect1", "a"); //如果把这句的注释去掉就会报错
for (String a : form.getParameterNames()) {
System.out.println(a + "==" + form.getParameterValue(a));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}//下面是index.jsp的内容<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/ecmascript">
function chkSubmit() {
if(document.getElementById("username1").value == "1" && document.getElementById("password1").value == "1") {
document.getElementById("myform").action="welcome.jsp";
document.getElementById("myform").submit();
return true;
} else {
return false;
}
}function changeSelect() {
var obj = document.getElementById("aselect1");
obj.selectedIndex=2;
}</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="myform"  method="post">
  <select name="aselect1" id="aselect1">
    <option value="a" id="aaa">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
  </select>
  <input type="text" name="username"   id="username1"  onclick="changeSelect();"/>
  <input type="password" name="password"  id="password1" />
  <input type="submit" onclick="javascript:chkSubmit();" value="button" name="button"  id="button"/>
</form>
<div id="welcome1">welcome1 to here</div>
</body>
</html>请各位高手帮助.