htmlunitフレームワーク付き簡単な処理テストdemo


package com.htmlunit.test;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImageInput;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.gargoylesoftware.htmlunit.html.HtmlTableCell;
import com.gargoylesoftware.htmlunit.html.HtmlTableRow;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

public class ProccessInfoTest_0 {

	public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException {
		WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
		
		webClient.setThrowExceptionOnFailingStatusCode(false);
		webClient.setThrowExceptionOnScriptError(false);
		webClient.setJavaScriptEnabled(false);

	    HtmlPage page = webClient.getPage("http://......./index.jsp");

	    HtmlForm form = page.getFormByName("myform");

	    HtmlImageInput button = form.getInputByName("loginit");
	    HtmlTextInput name = form.getInputByName("userName");
	    HtmlPasswordInput password = form.getInputByName("password");

	    // Change the value of the text field
	    name.setValueAttribute(".....");
	    password.setValueAttribute("......");

	    // Now submit the form by clicking the button and get back the second page.
	    page = (HtmlPage) button.click();
	    
	    webClient.setJavaScriptEnabled(true);
	    
	    String url2 = "";
	    url2 = "http://....../processInfo.htm";
	    HtmlPage framepage = webClient.getPage(url2);
	    
//	    System.out.println(framepage.asXml());
	    
//	    System.err.println("**********************************************************");
	    
	    
//	    webClient.setJavaScriptEnabled(true);
	    HtmlPage treepage = (HtmlPage)framepage.getFrameByName("treeframe").getEnclosedPage();
//	    System.out.println(treepage.asXml());
//	    webClient.setJavaScriptEnabled(true);
	    
	    HtmlElement body = treepage.getBody();
		HtmlTable table = (HtmlTable)body.getElementById("mytable");
		List<HtmlElement> list = table.getElementsByTagName("tr");
		List newlist = new ArrayList();
		for (int i = 0; i < list.size(); i++) {
			HtmlElement element = list.get(i);
			
			String patch = "/html/body/table/tbody/tr/td/table/tbody/form/tr["+i+"]";
			List<?> foundElements = treepage.getByXPath(patch);
			if(foundElements.size()!=0){
				HtmlElement temp = (HtmlElement)foundElements.get(0);
				String tempstr = temp.asText().trim().replace("
", "").replace("\r", ""); if(tempstr!=null && !"".equals(tempstr)) newlist.add(tempstr.replace("\t", "#")); System.out.println(i+"."+tempstr); } } System.err.println("*************************************"); // System.err.println("=============================================================="); List<?> items = treepage.getByXPath("/html/body/table/tbody/tr/td/table/tbody/form/tr"); if(items.size()>0){ // webClient.setJavaScriptEnabled(true); // List<HtmlElement> item = (List<HtmlElement>)items.get(1); HtmlTableRow tr = (HtmlTableRow) items.get(1); HtmlTableCell td = tr.getCell(3); System.out.println(td.asXml()); // webClient.setJavaScriptEnabled(true); // item.click(); td.click(); } // System.err.println("=============================messageframe================================="); HtmlPage messageframe = (HtmlPage)framepage.getFrameByName("messageframe").getEnclosedPage(); // System.out.println(messageframe.asXml()); // System.out.println(messageframe.asText()); webClient.setJavaScriptEnabled(false); System.err.println("=============================contentArea================================="); HtmlPage contentArea = (HtmlPage)messageframe.getFrameByName("contentArea").getEnclosedPage(); HtmlPage content = (HtmlPage)contentArea.getFrameByName("content").getEnclosedPage(); System.out.println(content.asXml()); } }

 
実行スクリプトの例:
     webClient.setJavaScriptEnabled(true);     page.executeJavaScript("document.myform.submit()");