An example of HttpUnit learning

Source: Internet
Author: User
Tags gettext
Import java.io.IOException;

Import java.net.MalformedURLException;

Import org.xml.sax.SAXException;
Import Com.meterware.httpunit.GetMethodWebRequest;
Import com.meterware.httpunit.HttpUnitOptions;
Import Com.meterware.httpunit.PostMethodWebRequest;
Import com.meterware.httpunit.WebConversation;
Import Com.meterware.httpunit.WebForm;
Import Com.meterware.httpunit.WebLink;
Import Com.meterware.httpunit.WebRequest;

Import Com.meterware.httpunit.WebResponse;
			public class Httpunittest {public static void main (string[] args) {try {//httpunittest.testgethtmlcontent ();
			Httpunittest.testgetmethod ();
			Httpunittest.testpostmethod ();
		Httpunittest.testformsubmit ();
		catch (Exception e) {e.printstacktrace ();  
		} public static void Testgethtmlcontent () throws IOException, saxexception{System.out.println ("Get Web content directly:");
        Httpunitoptions.setscriptingenabled (FALSE);  
       Establish a webconversation instance webconversation WC = new Webconversation (); Sends a request to the specified URL to obtain a response WebResponse WR = Wc.getresponse ("http://www.baidu.com.cn");  
	The GetText method is used to obtain the corresponding whole content//SYSTEM.OUT.PRINTLN the obtained content to print on the console System.out.println (Wr.gettext ()); /** * Use Get method to obtain page content * @throws malformedurlexception * @throws IOException * @throws Saxexcepti  On */public static void Testgetmethod () throws Malformedurlexception, IOException, saxexception  
        {System.out.println ("Send data to the server and get the page content:");  
        Establish a webconversation instance webconversation WC = new Webconversation ();  
        Makes a request to the specified URL WebRequest req = new Getmethodwebrequest ("http://localhost:8080/test.html");  
        Add parameters to the request Req.setparameter ("query", "carbon tetrachloride");  
        Gets the response object WebResponse resp = wc.getresponse (req);  
  
    The GetText method is used to obtain the corresponding whole content//SYSTEM.OUT.PRINTLN the obtained content to print on the console System.out.println (Resp.gettext ()); } 
    
    /* * * Use the Post method to get the page content * @throws malformedurlexception * @throws IOException * @throws saxexception  
        */public static void Testpostmethod () throws Malformedurlexception, IOException, saxexception {  
        SYSTEM.OUT.PRINTLN ("Send data to the server using post and get the page content:");  
        Establish a webconversation instance webconversation WC = new Webconversation ();  
        Makes a request to the specified URL WebRequest req = new Postmethodwebrequest ("Http://baidu.com/lii");  
        Add parameter Req.setparameter ("user_name", "test") to the request;  
        Req.setparameter ("Password", "111111");  
  
        Gets the response object WebResponse resp = wc.getresponse (req);  
    The GetText method is used to obtain the corresponding whole content//SYSTEM.OUT.PRINTLN the obtained content to print on the console System.out.println (Resp.gettext ()); /** * Get page link and simulate click * @throws malformedurlexception * @throws IOException * @throws Saxex Ception */public static void Testclicklink () thRows Malformedurlexception, IOException, saxexception {System.out.println ("Get the contents of the link on the page to the page:");  
        Establish a webconversation instance webconversation WC = new Webconversation ();  
        Gets the response object WebResponse resp = wc.getresponse ("http://www.265.com/");  
        Get the page link object weblink link = resp.getlinkwith ("Baidu");  
        Simulate user Click event Link.click ();  
  
        Gets the current response object WebResponse Nextlink = Wc.getcurrentpage ();  
  
    The GetText method is used to obtain the corresponding whole content//SYSTEM.OUT.PRINTLN the obtained content to print on the console System.out.println (Nextlink.gettext ()); /** * Test WebForm processing form and submission capability/public static void Testformsubmit () {Httpunitoptions.setscriptingena
		Bled (false);
		Webconversation WC = new Webconversation ();
		WebRequest request = new Postmethodwebrequest ("http://baidu.com/li/");
		WebResponse response = null;
			try {response = Wc.getresponse (request);
	Get the form form in HTML and httpunit him into a WebForm object		WebForm form = response.getforms () [0];
			The WebForm object provides a Getparametervalue method that obtains the corresponding value based on the name in the form, without having to control the type of the element.
			Processing of the form Form.setparameter ("username", "le");
			Form.setparameter ("Password", "le");
			Submit the form to obtain a new response response = Form.submit ();
			System.out.println (Response.gettext ());
			System.out.println ("----------------------------");  
	        Get the page link object weblink link = response.getlinkwith ("News Center");  
	        Simulate user Click event Link.click ();  
	        Gets the current response object WebResponse Nextlink = Wc.getcurrentpage ();  
			
		The GetText method is used to obtain the corresponding whole content//SYSTEM.OUT.PRINTLN the obtained content to print on the console System.out.println (Nextlink.gettext ());
		catch (IOException e) {e.printstacktrace ();
		catch (Saxexception e) {e.printstacktrace ();
 }
	}
	
}

Common statements:

Initialpage.getlinkwithid ("Go"). Click ();
Wc.getresponse (Gethostpath () + "/textpage.txt"). Getreceivedpage (). GetTitle ();
Structuredpage.getdom ()
Wc.getclientproperties (). Setoverridecontexttype ("text/html");
Simplepage.gettitle ());
Simplepage.getcharacterset ()
New Getmethodwebrequest ("File:" + File.getabsolutepath ())
Addresourceheader ("simplepage.html", "REFRESH:2; Url=nextpage.html ");
Wc.getclientproperties (). Setautorefresh (True);
Simplepage.getmetatagcontent ("Name", "Robots"));
Simplepage.getmetatagcontent ("name", "keywords"));
Simplepage.getmetatagcontent ("Http-equiv", "Expires")
Simplepage.getexternalstylesheet ()
Simplepage.getelementwithid ("Aform")
Simplepage.getelementswithname ("Aform")
Simplepage.getelementswithattribute ("Class", "I")
Wc.getresponse ("/nolinks.html"). Getlinks ()
Simplepage.getlinkwith ("no link")
Getlinkwithimagetext ("Next-->")
Httpunitoptions.setimagestreatedasalttext (TRUE);
Simplepage.getlinkwith ("Next-->")
Simplepage.getfirstmatchinglink (weblink.match_url_string, "/other.html")
Simplepage.getlinkwithname ("Nextlink")
Simplepage.getelementwithid ("Activeid")
Initialpage.getlinkwithimagetext ("Blah blah")
Nextpage.gettitle ()
Link.click ()
Link.gettarget ()). GetTitle ()
Wc.getresponse (Link.getrequest ())
Request.getrequestparameternames ()
Httpunitoptions.setlogginghttpheaders (FALSE);
Httpunitoptions.setscriptingenabled (FALSE);
Simplepage.getimages ()
Webimage image = Simplepage.getimagewithalttext ("one")
Simplepage.getimagewithsource ("Junk.png")
Link.geturlstring ()
Imagerequest.geturl (). Toexternalform ()
Wc.getframenames ()
Wc.getframecontents ("_top")
E.getresponsecode ()
E.getresponsemessage ()
Response.gettext ()
Response.getcontenttype ()
Wc.getcookievalue ("name")
Wc.getcookienames ()
Wc.getcookiedetails ("Age")
Wc.putcookie ("Emptyvalue", "Non-empty");
Wc.getclientproperties (). Setuseragent ("Me Alone");
Wc.setheaderfield ("Junky", "Mozilla 6");
Wc.setauthorization ("User", "password");
Wc.setproxyserver ("localhost", Gethostport (), "User", "password");
WebForm form = Wr.getformwithid ("main");
Form.setparameter ("name", "Master");
Form.submit ();
Submitbutton button = Form.getsubmitbutton ("Save", Button.Click ()
Page.getformwithid ("form"). Getsubmitbutton ("Update")
Page.getformwithname ("Nobody")
Form.getparameternames ()
Form.hasparameternamed ("third")
Form.getparametervalue ("name")
WebForm form = _wc.getresponse (Gethostpath () + "/oneform.html"). Getforms () [0];
WebRequest request = Form.getrequest ();
Request.setparameter ("name", "Master");
Form.reset ()
Response.gettextblocks () Get paragraph
blockelement paragraph = response.gettextblocks () [1];
Paragraph.getlinks ()
Paragraph.getlinkwithid ("HttpUnit")
Response.gettextblocks () [0].getlinkwithid ("HttpUnit")
Paragraph.getfirstmatchinglink (Weblink.match_contained_text, "home page")
Paragraph.getlinkwithid ("HttpUnit"). Getrequest (). GetURL (). Toexternalform ()
Request.geturl (). Toexternalform ()
Table.getrowcount ()
Table.getcolumncount ()
Table.getcellastext (2, 0)
Page.gettablewithid ("table")
Table.gettablecellwithid ("Id2")
Form.getmethod ()
Form.getaction ()
Form.getscriptableobject (). Setaction ("/tell")
Form.getsubmitbuttons ()
Form.getbuttonwithid ("doit")

Reference article: http://www.myexception.cn/software/753419.html
http://blog.csdn.net/zhongweijian/ article/details/7619299

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.