1 16 17 package org.apache.struts.faces.sysclient; 18 19 import java.io.IOException ; 20 import java.net.URL ; 21 import java.util.Iterator ; 22 23 import junit.framework.Test; 24 import junit.framework.TestCase; 25 import junit.framework.TestSuite; 26 27 import org.apache.commons.httpclient.HttpState; 28 29 import com.gargoylesoftware.htmlunit.ElementNotFoundException; 30 import com.gargoylesoftware.htmlunit.WebClient; 31 import com.gargoylesoftware.htmlunit.html.HtmlAnchor; 32 import com.gargoylesoftware.htmlunit.html.HtmlBody; 33 import com.gargoylesoftware.htmlunit.html.HtmlElement; 34 import com.gargoylesoftware.htmlunit.html.HtmlForm; 35 import com.gargoylesoftware.htmlunit.html.HtmlHead; 36 import com.gargoylesoftware.htmlunit.html.HtmlPage; 37 import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput; 38 39 40 41 46 47 public abstract class AbstractTestCase extends TestCase { 48 49 50 52 53 58 public AbstractTestCase(String name) { 59 60 super(name); 61 62 } 63 64 65 67 68 71 protected HttpState httpState = null; 72 73 74 77 protected HtmlPage page = null; 78 79 80 86 protected URL url = null; 87 88 89 92 protected WebClient webClient = null; 93 94 95 97 98 101 public void setUp() throws Exception { 102 103 String systest = System.getProperty("systest"); 105 url = new URL (systest + "/"); 106 107 webClient = new WebClient(); 109 httpState = webClient.getWebConnection().getStateForUrl(url("/")); 110 111 } 112 113 114 117 public static Test suite() { 118 119 return (new TestSuite(AbstractTestCase.class)); 120 121 } 122 123 124 127 public void tearDown() { 128 129 httpState = null; 130 page = null; 131 url = null; 133 webClient = null; 134 135 } 136 137 138 139 141 142 146 protected HtmlBody body() throws Exception { 147 148 Iterator elements = page.getAllHtmlChildElements(); 149 while (elements.hasNext()) { 150 HtmlElement element = (HtmlElement) elements.next(); 151 if (element instanceof HtmlBody) { 152 return ((HtmlBody) element); 153 } 154 } 155 return (null); 156 157 } 158 159 160 167 protected HtmlElement element(String id) throws Exception { 168 169 try { 170 return (page.getHtmlElementById(id)); 171 } catch (ElementNotFoundException e) { 172 return (null); 173 } 174 175 } 176 177 178 185 protected HtmlForm form(String id) throws Exception { 186 187 Iterator forms = page.getAllForms().iterator(); 188 while (forms.hasNext()) { 189 HtmlForm form = (HtmlForm) forms.next(); 190 if (id.equals(form.getAttributeValue("id"))) { 191 return (form); 192 } 193 } 194 return (null); 195 196 } 197 198 199 203 protected HtmlHead head() throws Exception { 204 205 Iterator elements = page.getAllHtmlChildElements(); 206 while (elements.hasNext()) { 207 HtmlElement element = (HtmlElement) elements.next(); 208 if (element instanceof HtmlHead) { 209 return ((HtmlHead) element); 210 } 211 } 212 return (null); 213 214 } 215 216 217 226 protected HtmlPage link(HtmlAnchor anchor) throws IOException { 227 228 HtmlPage page = (HtmlPage) anchor.click(); 229 this.page = page; 230 return page; 231 232 } 233 234 235 245 protected HtmlPage page(String path) throws Exception { 246 247 HtmlPage page = (HtmlPage) webClient.getPage(url(path)); 248 253 this.page = page; 254 return (page); 255 256 } 257 258 259 268 protected HtmlPage submit(HtmlSubmitInput submit) throws IOException { 269 270 HtmlPage page = (HtmlPage) submit.click(); 271 this.page = page; 272 return page; 273 274 } 275 276 277 281 protected String title() throws Exception { 282 283 return (page.getTitleText().trim()); 284 285 } 286 287 288 297 protected URL url(String path) throws Exception { 298 299 if (path.charAt(0) != '/') { 300 throw new IllegalArgumentException ("Context path '" + path + 301 "' does not start with '/'"); 302 } 303 return new URL (url, path.substring(1)); 304 305 } 306 307 308 } 309 | Popular Tags |