当前位置: 首页 > 工具软件 > HtmlUnit > 使用案例 >

HtmlUnit简单使用

苏胤
2023-12-01
package com.potevio.telecom.test;

import java.io.IOException;
import java.net.MalformedURLException;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

import junit.framework.TestCase;

/**
 * @description HtmlUnit工具的简单使用测试
 * @author ZhouJingxian
 *
 */
public class Test_HtmlUnit extends TestCase{
	
	/**
	 * 测试主页标题
	 * @throws FailingHttpStatusCodeException
	 * @throws MalformedURLException
	 * @throws IOException
	 */
	public void testHomePage() throws FailingHttpStatusCodeException, MalformedURLException, IOException{

		final WebClient webClient = new WebClient();
		final HtmlPage startPage = webClient.getPage("http://htmlunit.sourceforge.net");
//		assertEquals("HtmlUnit - Welcome to HtmlUnit", startPage.getTitleText());
		System.out.println("title:"+startPage.getTitleText());
		
	}
	
	/**
	 * 测试浏览器
	 * @throws Exception
	 */
	public void testHomePage_Firefox() throws Exception {
	    //火狐2/3
//		final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_2);
	    
		//IE6/7
		final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER_7); 
		
		final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
		
//	    assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());
	    System.out.println("title:"+page.getTitleText());
	}

/**
 * 两个测试类的输出都如下:
 * title:HtmlUnit - Welcome to HtmlUnit
 */
}

 

 类似资料: