How to Use HtmlUnit to simulate page form submission

Source: Internet
Author: User

Sometimes, you need to verify the web Background pressure during testing. If you want testers to enter and submit forms continuously, it may be difficult to solve this problem.

 

For example, if you have a web project whose homepage index. jsp:

[Html]
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>
 
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Base href = "<% = basePath %>">

<Title> My JSP 'index. jsp 'starting page </title>
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
<! --
<Link rel = "stylesheet" type = "text/css" href = "styles.css">
-->
</Head>

<Body>
<Form action = "<% = path %>/LoginServ" method = "post" name = "loginForm" id = "loginForm">
<Table width = "250" border = "0">
<Tr>
<Td> account: </td> <input type = "text" name = "username" id = "username"/> </td>
</Tr>
<Tr>
<Td> password: </td> <input type = "password" name = "pass" id = "pass"/> </td>
</Tr>
<Tr>
<Td colspan = 2> <input type = "submit" value = "login" name = "testlogin"/> </td>
</Tr>
</Table>
</Form>
</Body>
</Html>

<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<%
String path = request. getContextPath ();
String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/";
%>

<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Base href = "<% = basePath %>">

<Title> My JSP 'index. jsp 'starting page </title>
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
<! --
<Link rel = "stylesheet" type = "text/css" href = "styles.css">
-->
</Head>
 
<Body>
<Form action = "<% = path %>/LoginServ" method = "post" name = "loginForm" id = "loginForm">
<Table width = "250" border = "0">
<Tr>
<Td> account: </td> <input type = "text" name = "username" id = "username"/> </td>
</Tr>
<Tr>
<Td> password: </td> <input type = "password" name = "pass" id = "pass"/> </td>
</Tr>
<Tr>
<Td colspan = 2> <input type = "submit" value = "login" name = "testlogin"/> </td>
</Tr>
</Table>
</Form>
</Body>
</Html>

The background servlet is:

[Java]
Package com. test;
 
Import java. io. IOException;
Import java. io. PrintWriter;
 
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
 
Public class LoginServ extends HttpServlet {
 
/**
* Constructor of the object.
*/
Public LoginServ (){
Super ();
}
 
/**
* Destruction of the servlet. <br>
*/
Public void destroy (){
Super. destroy (); // Just puts "destroy" string in log
// Put your code here
}
 
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @ Param request the request send by the client to the server
* @ Param response the response send by the server to the client
* @ Throws ServletException if an error occurred
* @ Throws IOException if an error occurred
*/
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
 
This. doPost (request, response );
}
 
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @ Param request the request send by the client to the server
* @ Param response the response send by the server to the client
* @ Throws ServletException if an error occurred
* @ Throws IOException if an error occurred
*/
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
 
String username = request. getParameter ("username ");
String pass = request. getParameter ("pass ");
Username = new String (username. getBytes ("ISO-8859-1"), "UTF-8 ");
System. out. println ("user logon, account = [" + username + "], password = [" + pass + "]");

}
 
/**
* Initialization of the servlet. <br>
*
* @ Throws ServletException if an error occurs
*/
Public void init () throws ServletException {
// Put your code here
}
 
}

Package com. test;

Import java. io. IOException;
Import java. io. PrintWriter;

Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;

Public class LoginServ extends HttpServlet {

/**
* Constructor of the object.
*/
Public LoginServ (){
Super ();
}

/**
* Destruction of the servlet. <br>
*/
Public void destroy (){
Super. destroy (); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @ Param request the request send by the client to the server
* @ Param response the response send by the server to the client
* @ Throws ServletException if an error occurred
* @ Throws IOException if an error occurred
*/
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {

This. doPost (request, response );
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @ Param request the request send by the client to the server
* @ Param response the response send by the server to the client
* @ Throws ServletException if an error occurred
* @ Throws IOException if an error occurred
*/
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {

String username = request. getParameter ("username ");
String pass = request. getParameter ("pass ");
Username = new String (username. getBytes ("ISO-8859-1"), "UTF-8 ");
System. out. println ("user logon, account = [" + username + "], password = [" + pass + "]");

}

/**
* Initialization of the servlet. <br>
*
* @ Throws ServletException if an error occurs
*/
Public void init () throws ServletException {
// Put your code here
}

}

After the project is deployed and run, we compile the test code and simulate the form to submit the data:

[Java]
Package com. test;
 
Import com.gargoylesoftware.html unit. WebClient;
Import com.gargoylesoftware.htmlunit.html. HtmlForm;
Import com.gargoylesoftware.htmlunit.html. HtmlPage;
Import com.gargoylesoftware.htmlunit.html. HtmlPasswordInput;
Import com.gargoylesoftware.htmlunit.html. HtmlSubmitInput;
Import com.gargoylesoftware.htmlunit.html. HtmlTextInput;
 
Public class Test {
Public static void submittingForm () throws Exception {
Final WebClient webClient = new WebClient ();
// 1. Obtain a page to be tested
Final HtmlPage page1 = webClient. getPage ("http: // localhost: 8081/HtmlUnitTest ");
// 2. Obtain the form on the page
Final HtmlForm form = page1.getForms (). get (0 );
// 3. Obtain the elements on the page
Final HtmlSubmitInput button = form. getInputByName ("testlogin ");
Final HtmlTextInput textField = form. getInputByName ("username ");
Final HtmlPasswordInput pass = form. getInputByName ("pass ");
// 4. assign values to elements
TextField. setValueAttribute ("I am Zhang San ");
Pass. setValueAttribute ("routon ");
// 5. Submit
Button. click ();
}
Public static void main (String [] args ){
Try {
SubmittingForm ();
} Catch (Exception e ){
E. printStackTrace ();
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.