Junit4 Learning (5) Use httpunit to test html

Source: Internet
Author: User
Test 1: Test whether the webpage exists:

To test whether a webpage exists, simply use the getresponse () method of webconversation. For example:

Webconversation = new webconversation ();

Webconversation. getresponse (

Too many bytes have been written "http: // localhost: 8080/httpunit /");

If the webpage cannot be found, httpnotfoundexception will be triggered. Because it is not an asserted error, an error will be generated in JUnit.

Test 2: Get, post:

You can use getmethodwebrequest or postmethodwebrequest to send a get or POST request separately. For example:

Webconversation = new webconversation ();

Webrequest request = new getmethodwebrequest ("http: // localhost: 8080/httpunit /");

Webresponse response = webconversation. getresponse (request );

To increase the number of parameters in a request, you can use the setparamter () method, for example:

Request. setparameter ("username", "taobaoge ");

Test 3: Obtain table information:

You can obtain the relevant HTML information from webresponse. Assume that the webpage contains the following table:

Book name

Design Pattern)

Software Version

None

Book version

Version 2

Modification time

2004/12/26

The followingProgramDemonstrate how to obtain table-related information for testing:

Webconversation = new webconversation ();

Webresponse response = webconversation. getresponse ("http: // localhost: 8080/httpunit/tabletest. jsp ");

Webtable = response. gettables () [0];

Assertequals (2, webtable. getcolumncount ());

Tablecell cell = webtable. gettablecell (2, 0 );

Assertequals ("book version", cell. gettext ());

Test 4: Follow hyperlink:

There are many hyperlinks in the webpage. We can use httpunit to simulate hyperlink clicks. For example, if a hyperlink in the webpage is as follows:

<A href = "httpunitabc. jsp"> httpunit ABC </a>

You can use the following program to quarrel with the link, and then simulate a click action to follow the hyperlink:

Webconversation = new webconversation ();

Webresponse response = webconversation. getresponse ("http: // localhost: 8080/httpunit /");

Weblink link = response. getlinkwith ("httpunit ABC ");

Webrequest clickrequest = link. getrequest ();

Webresponse linkpage = webconversation. getresponse (clickrequest );

Test 5: Test COOKIE:

If the tested webpage requires cookie information, you can use the addcookie () method of webconversation to send cookie information.

To the webpage, for example:

Webconversation = new webconversation ();

Webconversation. addcookie ("user", "taobaoge ");

Webresponse response = webconversation. getresponse ("http: // localhost: 8080/httpunit /");

If the webpage contains a cookie, you can use getcookievalue () to obtain the cookie information contained in the webpage. If the webpage includes the following scriptlet:

<%

Cookie = new cookie ("customerid", "12345 ");

Response. addcookie (cookie );

%>

You can use the following method to test the returned cookie information:

Assertequals ("taobaoge", webconversation. getcookievalue ("user "));

Test 6: authorization:

If your webpage has preset basic HTTP verification, you can use the setauthorization () method of webconversation to set verification information. For example:

Webconversation. setauthorization ("Justin", "123456 ");

Test 7: Set Proxy:

Sometimes, the target webpage you test may be connected through a proxy server. You can set the system attribute to set the proxy for httpunit. For example:

System. getproperties (). Put ("proxyset", "true ");

System. getproperties (). Put ("proxyhost", "proxy.ntu.edu.tw ");

System. getproperties (). Put ("proxyport", "8080 ");

In this way, httpunit will send requests and accept corresponding requests through the specified proxy server.

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.