IntroductionThe dependencies page lists all the jars that you will need to have in your classpath.
The class com.gargoylesoftware.htmlunit.WebClient is the main starting point. This simulates a web browser and will be used to execute all of the tests.
Most unit testing will be done within a framework like JUnit so all the examples here will assume that we are using that.
In the first sample, we create the web client and have it load the homepage from the HtmlUnit website. We then verify that this page has the correct title. Note that getPage() can return different types of pages based on the content type of the returned data. In this case we are expecting a content type of text/html so we cast the result to an com.gargoylesoftware.htmlunit.html.HtmlPage.
@Test
public void homePage() throws Exception {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());
final String pageAsXml = page.asXml();
assertTrue(pageAsXml.contains("<body class=\"composite\">"));
final String pageAsText = page.asText();
assertTrue(pageAsText.contains("Support for the HTTP and HTTPS protocols"));
webClient.closeAllWindows();
}Imitating a specific browserOften you will want to simulate a specific browser. This is done by passing a com.gargoylesoftware.htmlunit.BrowserVersion into the WebClient constructor. Constants have been provided for some common browsers but you can create your own specific version by instantiating a BrowserVersion.
@Test
public void homePage_Firefox() throws Exception {
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_2);
final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());
webClient.closeAllWindows();
}Specifying this BrowserVersion will change the user agent header that is sent up to the server and will change the behavior of some of the JavaScript.
Finding a specific elementOnce you have a reference to an HtmlPage, you can search for a specific HtmlElement by one of 'get' methods, or by using XPath.
Below is an example of finding a 'div' by an ID, and getting an anchor by name:
@Test
public void getElements() throws Exception {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://some_url");
final HtmlDivision div = page.getHtmlElementById("some_div_id");
final HtmlAnchor anchor = page.getAnchorByName("anchor_name");
webClient.closeAllWindows();
}XPath is the suggested way for more complex searches, a brief tutorial can be found in W3Schools
@Test
public void xpath() throws Exception {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
//get list of all divs
final List<?> divs = page.getByXPath("//div");
//get div which has a 'name' attribute of 'John'
final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@name='John']").get(0);
webClient.closeAllWindows();
}Using a proxy serverThe last WebClient constructor allows you to specify proxy server information in those cases where you need to connect through one.
@Test
public void homePage_proxy() throws Exception {
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_2, "http://myproxyserver", myProxyPort);
//set proxy username and password
final DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) webClient.getCredentialsProvider();
credentialsProvider.addProxyCredentials("username", "password");
final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());
webClient.closeAllWindows();
}Specifying this BrowserVersion will change the user agent header that is sent up to the server and will change the behavior of some of the JavaScript.
Submitting a formFrequently we want to change values in a form and submit the form back to the server. The following example shows how you might do this.
@Test
public void submittingForm() throws Exception {
final WebClient webClient = new WebClient();
// Get the first page
final HtmlPage page1 = webClient.getPage("http://some_url");
// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
final HtmlForm form = page1.getFormByName("myform");
final HtmlSubmitInput button = form.getInputByName("submitbutton");
final HtmlTextInput textField = form.getInputByName("userid");
// Change the value of the text field
textField.setValueAttribute("root");
// Now submit the form by clicking the button and get back the second page.
final HtmlPage page2 = button.click();
webClient.closeAllWindows();
}
The class com.gargoylesoftware.htmlunit.WebClient is the main starting point. This simulates a web browser and will be used to execute all of the tests.
Most unit testing will be done within a framework like JUnit so all the examples here will assume that we are using that.
In the first sample, we create the web client and have it load the homepage from the HtmlUnit website. We then verify that this page has the correct title. Note that getPage() can return different types of pages based on the content type of the returned data. In this case we are expecting a content type of text/html so we cast the result to an com.gargoylesoftware.htmlunit.html.HtmlPage.
@Test
public void homePage() throws Exception {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());
final String pageAsXml = page.asXml();
assertTrue(pageAsXml.contains("<body class=\"composite\">"));
final String pageAsText = page.asText();
assertTrue(pageAsText.contains("Support for the HTTP and HTTPS protocols"));
webClient.closeAllWindows();
}Imitating a specific browserOften you will want to simulate a specific browser. This is done by passing a com.gargoylesoftware.htmlunit.BrowserVersion into the WebClient constructor. Constants have been provided for some common browsers but you can create your own specific version by instantiating a BrowserVersion.
@Test
public void homePage_Firefox() throws Exception {
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_2);
final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());
webClient.closeAllWindows();
}Specifying this BrowserVersion will change the user agent header that is sent up to the server and will change the behavior of some of the JavaScript.
Finding a specific elementOnce you have a reference to an HtmlPage, you can search for a specific HtmlElement by one of 'get' methods, or by using XPath.
Below is an example of finding a 'div' by an ID, and getting an anchor by name:
@Test
public void getElements() throws Exception {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://some_url");
final HtmlDivision div = page.getHtmlElementById("some_div_id");
final HtmlAnchor anchor = page.getAnchorByName("anchor_name");
webClient.closeAllWindows();
}XPath is the suggested way for more complex searches, a brief tutorial can be found in W3Schools
@Test
public void xpath() throws Exception {
final WebClient webClient = new WebClient();
final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
//get list of all divs
final List<?> divs = page.getByXPath("//div");
//get div which has a 'name' attribute of 'John'
final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@name='John']").get(0);
webClient.closeAllWindows();
}Using a proxy serverThe last WebClient constructor allows you to specify proxy server information in those cases where you need to connect through one.
@Test
public void homePage_proxy() throws Exception {
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_2, "http://myproxyserver", myProxyPort);
//set proxy username and password
final DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) webClient.getCredentialsProvider();
credentialsProvider.addProxyCredentials("username", "password");
final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());
webClient.closeAllWindows();
}Specifying this BrowserVersion will change the user agent header that is sent up to the server and will change the behavior of some of the JavaScript.
Submitting a formFrequently we want to change values in a form and submit the form back to the server. The following example shows how you might do this.
@Test
public void submittingForm() throws Exception {
final WebClient webClient = new WebClient();
// Get the first page
final HtmlPage page1 = webClient.getPage("http://some_url");
// Get the form that we are dealing with and within that form,
// find the submit button and the field that we want to change.
final HtmlForm form = page1.getFormByName("myform");
final HtmlSubmitInput button = form.getInputByName("submitbutton");
final HtmlTextInput textField = form.getInputByName("userid");
// Change the value of the text field
textField.setValueAttribute("root");
// Now submit the form by clicking the button and get back the second page.
final HtmlPage page2 = button.click();
webClient.closeAllWindows();
}
- htmlunit-2.8.rar (9.5 MB)
- 下载次数: 9
发表评论
-
菜鸟 Spring 源码解读 推荐流程
2012-01-11 09:18 5128Spring源代码解析(一):IOC容器:http://www ... -
深入剖析Classloader(一)--类的主动使用与被动使用
2011-12-27 22:13 1095我们知道java运行的是这样的,首先java编译器将我们的源代 ... -
Java中连接字符串时是使用+号还是使用StringBuilder?
2011-12-26 14:04 923字符串是Java程序中最常用的一种数据结构之一。在Java中的 ... -
转一篇有关Java的内存泄露的文章(受益哦)
2011-07-20 09:28 7741 引言 Java的一个 ... -
Tomcat内存溢出的原因
2011-07-19 09:41 730Tomcat内存溢出的原因 在生产环境中tomcat内 ... -
深入研究java.lang.ThreadLocal类
2011-07-13 09:39 685一、概述 ThreadLocal是什么呢?其实Thread ... -
jboss中实现跨war包session同步
2011-06-12 23:28 1290跨war包session同步解决方 ... -
开源框架spring详解-----AOP的深刻理解
2011-05-26 22:13 1251开源框架spring详解-----AOP的深刻理解 AOP的 ... -
struts2核心工作流程与工作原理
2011-05-26 15:35 12891. Struts2架构图 这是S truts2官方站点提供的 ... -
Spring注入方式及用到的注解 -----@Component,@Service,@Controller,@Repository
2011-05-26 15:04 1227注入方式: 把DAO实现 ... -
Java中的native关键字浅析(Java Native Interface)
2011-05-21 23:13 741JNI是Java Native Interface的 ... -
Volatile 变量
2011-04-26 17:01 656Java 语言中的 volatile 变量可以被看作是一种 “ ... -
Java对象的强、软、弱和虚引用
2011-04-26 16:04 6281.Java对象的强、软、 ... -
Web 应用程序常见漏洞 CSRF 的入侵检测与防范
2011-04-23 15:00 1120简介: 互联网的安全问题一直存在,并且在可预见的未来中没有消弭 ... -
详解XSS跨站脚本攻击
2011-04-23 13:46 1143一、什么是XSS攻击 XSS ... -
CSRF攻击原理解析
2011-04-22 10:29 12830×00. 前言 在Web程序中 ... -
selenium 初步体检之富文本框操作
2011-04-20 20:10 1542public class LoginTest extends ... -
webx
2011-03-05 17:54 1019webx 学习笔记。 -
Java读带有BOM的UTF-8文件乱码解决方法
2011-03-02 11:12 2465Java default io reader does not ... -
java sftp tools
2011-02-24 13:30 1517import java.io.File; import jav ...
相关推荐
HTMLUnit是一个强大的Java库,它模拟了一个无头Web浏览器,主要用于自动化测试和网页抓取。在版本2.23的zip文件中,我们主要关注HTMLUnit的核心功能和它如何帮助开发者处理HTML内容。 HTMLUnit的核心是基于Jakarta ...
HTMLUnit是一款功能强大的Java库,它模拟了一个无头Web浏览器,允许开发者在没有真实浏览器环境的情况下执行JavaScript,处理Ajax请求,以及与网页进行交互。这个库对于自动化测试、数据抓取和网页爬虫项目非常有用...
HTMLUnit是一个Java库,它提供了一个无头浏览器模拟器,用于自动化Web页面的导航和交互。这个库在测试、数据抓取以及不需图形用户界面的场景中非常有用。"htmlUnit所需jar包"的标题表明这是一个包含HTMLUnit运行所需...
HTMLUnit是一个Java库,它提供了一个无头浏览器模拟器,用于自动化Web页面的测试和交互。这个"htmlunit基本jar包"包含了运行HTMLUnit所需的所有核心组件,无需通过Maven来管理和依赖。这意味着用户可以直接在项目中...
HTMLUnit是一个功能强大的Java库,它模拟了一个无头Web浏览器,允许开发者进行自动化网页测试和网络爬虫工作。这个库的核心在于它能够解析HTML、执行JavaScript,并与网页上的元素进行交互,而无需真实地打开一个...
HTMLUnit是一个Java库,模拟一个无头Web浏览器,主要用于自动化测试和网页抓取。它能够解析HTML、执行JavaScript,并返回用户可以操作的DOM元素。在Java应用中使用HTMLUnit,通常需要依赖一系列的JAR(Java Archive...
HTMLUnit是一个Java库,它提供了一个无头浏览器模拟器,用于自动化网页测试和网页抓取。这个库允许开发者在没有实际浏览器的情况下与HTML页面进行交互,执行JavaScript,并获取页面加载后的结果。它对于进行功能测试...
HTMLUnit是一个Java库,它提供了一个无头浏览器模拟器,用于自动化Web页面的导航和交互。这个库在进行网络爬虫或自动化测试时非常有用,因为它可以解析HTML、执行JavaScript,甚至处理AJAX请求,而无需打开实际的...
HTMLUnit是一个Java库,它提供了一个无头浏览器的模拟,允许开发者在没有真实浏览器环境的情况下进行Web客户端测试和自动化。这个“htmlunit-2.25”压缩包包含的就是HTMLUnit库的版本2.25,是进行无头网页交互的关键...
HTMLUnit是Java编程语言中的一款无头Web浏览器库,它模拟了浏览器的行为,但不涉及图形用户界面。这个工具主要用于自动化测试和网络爬虫,因为它能够解析HTML、执行JavaScript,并与网页上的各种元素进行交互。在...
通过HTMLUnit,开发者可以编写程序来模拟用户在浏览器上的操作,如点击链接、填写表单、执行JavaScript等,而无需真正打开一个浏览器实例。 使用HTMLUnit获取执行JavaScript后的HTML文档,首先需要理解HTMLUnit的...
HTMLUnit是一个Java库,它提供了一个无头浏览器模拟器,用于自动化Web页面的测试和抓取。这个库的核心功能是能够解析、渲染和执行JavaScript,从而使得开发者可以在没有实际浏览器环境的情况下,对网页进行功能测试...
HTMLUnit是一个Java库,它模拟了一个无头Web浏览器,用于自动化网页测试和抓取。它提供了JavaScript支持,能够处理AJAX请求,使开发者能够在没有实际图形界面的情况下与网页交互。这个"htmlunit-2.1.5源码"包包含了...
"htmlunit-2.20.zip"压缩包包含了HTMLUnit库的2.20版本,这个版本是针对Java JDK 1.7设计的,这意味着它与Java 7兼容。对于那些需要在旧项目中使用或者依赖Java 7环境的开发者来说,这是一个重要的考虑因素。值得...
HTMLUnit是一个基于Java的无头Web客户端库,它模拟了一个完整的浏览器,但不涉及实际的图形用户界面。这个“htmlunit-2.19-bin”压缩包包含了HTMLUnit的二进制版本,允许开发者在Java应用程序中进行网页自动化测试和...
HtmlUnit 是一个强大的Java库,它模拟了一个无头Web浏览器,允许开发者进行自动化测试和网页抓取。在HtmlUnit 2.8版本中,你可以利用它来执行JavaScript、处理Ajax请求,以及与网页上的各种元素交互,而无需实际运行...
HtmlUnit是Java编程语言中的一款无头浏览器模拟库,它允许开发者在没有真实浏览器环境的情况下进行Web应用程序的测试和自动化。最新版HtmlUnit 2.22提供了对现代Web技术的强大支持,包括JavaScript、Ajax以及各种...
赠送jar包:htmlunit-2.33.jar; 赠送原API文档:htmlunit-2.33-javadoc.jar; 赠送源代码:htmlunit-2.33-sources.jar; 赠送Maven依赖信息文件:htmlunit-2.33.pom; 包含翻译后的API文档:htmlunit-2.33-javadoc-...
HTMLUnit是一款强大的无头Web客户端库,主要用于网页的单元测试。它模拟了一个浏览器,能够执行JavaScript,解析HTML,处理CSS,以及与网页上的各种元素进行交互。这个“htmlunit-2.3.zip”压缩包包含了运行和使用...
HTMLUnit是一个Java库,它提供了一个无头浏览器模拟器,用于自动化Web页面的测试和抓取。这个库允许开发者在不依赖于实际图形用户界面(GUI)的情况下与HTML页面进行交互,例如点击链接、填写表单和执行JavaScript。...